/
client.go
432 lines (396 loc) · 9.75 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package crowdin
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
cache "github.com/Code-Hex/go-generics-cache"
"github.com/ansel1/merry/v2"
"github.com/bcc-code/mediabank-bridge/log"
"github.com/go-resty/resty/v2"
"github.com/samber/lo"
)
// Config for the client
type Config struct {
Token string
ProjectIDs []int
}
// Client for crowdin interactions
type Client struct {
c *resty.Client
config Config
q *sqlc.Queries
readonly bool
}
// ErrRequestFailed error for failed requests
var ErrRequestFailed = merry.Sentinel("Request failed")
func ensureSuccess(res *resty.Response) error {
if res.IsError() {
return merry.Wrap(ErrRequestFailed, merry.WithHTTPCode(res.StatusCode()), merry.WithMessage(res.String()))
}
return nil
}
// New client for requests
func New(config Config, queries *sqlc.Queries, readonly bool) *Client {
c := resty.New().
SetBaseURL("https://api.crowdin.com/api/v2/").
SetAuthToken(config.Token)
return &Client{
c: c,
config: config,
q: queries,
readonly: readonly,
}
}
func getItem[t any](client *Client, endpoint string, id int) (item t, err error) {
req := client.c.R()
req.SetResult(Object[t]{})
query := fmt.Sprintf("/%s/%d", endpoint, id)
res, err := req.Get(query)
if err != nil {
log.L.Error().Err(err).
Str("endpoint", endpoint).
Str("id", strconv.Itoa(id)).
Msg("Failed to retrieve item")
return
}
err = ensureSuccess(res)
if err == nil {
item = res.Result().(*Object[t]).Data
}
return
}
func getItems[t any](client *Client, endpoint string, limit int, offset int, queryParams map[string]string) (items []t, err error) {
req := client.c.R()
req.SetResult(Result[[]Object[t]]{})
if queryParams == nil {
queryParams = map[string]string{}
}
queryParams["limit"] = strconv.Itoa(limit)
queryParams["offset"] = strconv.Itoa(offset)
req.SetQueryParams(queryParams)
query := fmt.Sprintf("/%s", endpoint)
res, err := req.Get(query)
if err != nil {
log.L.Error().Err(err).
Str("endpoint", endpoint).
Msg("Failed to retrieve items")
return
}
err = ensureSuccess(res)
if err == nil {
items = lo.Map(res.Result().(*Result[[]Object[t]]).Data, func(i Object[t], _ int) t {
return i.Data
})
}
return
}
func createItem[t any](client *Client, endpoint string, item t) (i t, err error) {
req := client.c.R()
req.SetBody(item)
req.SetResult(Object[t]{})
res, err := req.Post(endpoint)
if err != nil {
log.L.Error().Err(err).
Str("endpoint", endpoint).
Msg("Failed to create item")
return
}
err = ensureSuccess(res)
if err != nil {
return
} else {
i = res.Result().(*Object[t]).Data
}
return
}
// SimpleTranslation contains info about the translation object as stored in the database
type SimpleTranslation struct {
ID string
ParentID string
Values map[string]string
Language string
Changed bool
}
func convertTsToStrings(ts []SimpleTranslation, prefix string, contextFactory func(parentID string) string) []String {
return lo.Reduce(ts, func(stringObjects []String, t SimpleTranslation, _ int) []String {
var values = t.Values
for key, value := range values {
if value != "" {
str := String{
Identifier: fmt.Sprintf("%s-%s-%s", prefix, t.ParentID, key),
Text: value,
IsHidden: false,
}
if contextFactory != nil {
str.Context = contextFactory(t.ParentID)
}
stringObjects = append(stringObjects, str)
}
}
return stringObjects
}, []String{})
}
var directoryCache = cache.New[int, Directory]()
func (c *Client) getDirectoryForProject(project Project) (d Directory, err error) {
if dir, ok := directoryCache.Get(project.ID); ok {
return dir, nil
}
directories, err := c.getDirectories(project.ID)
if err != nil {
return
}
var directory *Directory
for _, dir := range directories {
v := dir
if dir.Name == "content" {
directory = &v
}
}
if directory == nil {
dir, err := createItem(c, fmt.Sprintf("projects/%d/directories", project.ID), Directory{
Name: "content",
})
if err != nil {
return d, err
}
directory = &dir
}
directoryCache.Set(project.ID, *directory, cache.WithExpiration(time.Minute*5))
return *directory, nil
}
func (c *Client) getFileForCollection(project Project, directoryId int, collection string) (file File, found bool, err error) {
files, err := c.getFiles(project.ID, directoryId)
if err != nil {
return
}
if len(files) > 0 {
for _, f := range files {
if f.Title == collection {
found = true
file = f
break
}
}
}
return
}
func prepareStr(source string) string {
return source
//reg := regexp.MustCompile("[^a-zA-Z\\d]+")
//return reg.ReplaceAllString(source, "")
}
func strEqual(source string, compare string) bool {
trimSource := prepareStr(source)
trimCompare := prepareStr(compare)
return trimSource == trimCompare
}
func dbString(source string) string {
return strings.Replace(source, "\"\"", "\"", -1)
}
func crowdinString(source string) string {
return strings.Replace(source, "\"", "\"\"", -1)
}
func dbLanguage(language string) string {
if strings.Contains(language, "-") {
return strings.Split(language, "-")[0]
}
return language
}
var projectCache = cache.New[int, Project]()
func (c *Client) getProject(projectId int) (i Project, err error) {
if p, ok := projectCache.Get(projectId); ok {
return p, nil
}
i, err = getItem[Project](c, "projects", projectId)
if err != nil {
return
}
projectCache.Set(projectId, i, cache.WithExpiration(time.Minute*5))
return
}
// Sync synchronizes translations from Crowdin to Handler and Handler to Crowdin
func (c *Client) Sync(ctx context.Context) error {
log.L.Debug().Msg("Translation sync: Started")
projectIds := c.config.ProjectIDs
for _, id := range projectIds {
project, err := c.getProject(id)
if err != nil {
return err
}
directory, err := c.getDirectoryForProject(project)
if err != nil {
return err
}
crowdinTranslations, err := c.List(ctx)
if err != nil {
return err
}
options := Options{
Translations: crowdinTranslations,
Project: project,
DirectoryID: directory.ID,
}
err = c.syncEpisodes(ctx, options)
if err != nil {
return err
}
err = c.syncSeasons(ctx, options)
if err != nil {
return err
}
err = c.syncShows(ctx, options)
if err != nil {
return err
}
err = c.syncSections(ctx, options)
if err != nil {
return err
}
err = c.syncPages(ctx, options)
if err != nil {
return err
}
err = c.syncLinks(ctx, options)
if err != nil {
return err
}
err = c.syncTopics(ctx, options)
if err != nil {
return err
}
err = c.syncLessons(ctx, options)
if err != nil {
return err
}
err = c.syncTasks(ctx, options)
if err != nil {
return err
}
err = c.syncAlternatives(ctx, options)
if err != nil {
return err
}
err = c.syncAchievements(ctx, options)
if err != nil {
return err
}
err = c.syncAchievementGroups(ctx, options)
if err != nil {
return err
}
err = c.syncSurveys(ctx, options)
if err != nil {
return err
}
err = c.syncSurveyQuestions(ctx, options)
if err != nil {
return err
}
err = c.syncFAQs(ctx, options)
if err != nil {
return err
}
err = c.syncFAQCategories(ctx, options)
if err != nil {
return err
}
err = c.syncGames(ctx, options)
if err != nil {
return err
}
err = c.syncPlaylists(ctx, options)
if err != nil {
return err
}
err = c.syncEvents(ctx, options)
if err != nil {
return err
}
err = c.syncCalendarEntries(ctx, options)
if err != nil {
return err
}
err = c.syncMediaItems(ctx, options)
if err != nil {
return err
}
}
log.L.Debug().Msg("Translation sync: Done")
return nil
}
// TranslationSource is an object which contains fields that can be translated
type TranslationSource interface {
GetCollection() string
GetItemID() int
GetLanguage() string
// GetValues returns a field mapped dictionary with the translation source as value, identifier as key
GetValues() map[string]string
}
// SaveTranslations stores updated translations from the objects, if they are changed
func (c *Client) SaveTranslations(objects []TranslationSource) error {
log.L.Debug().Int("count", len(objects)).Msg("Syncing translations with Crowdin")
for _, projectId := range c.config.ProjectIDs {
project, err := c.getProject(projectId)
if err != nil {
return err
}
directory, err := c.getDirectoryForProject(project)
if err != nil {
return err
}
fileIdByCollection := map[string]int{}
for _, o := range objects {
if project.SourceLanguageId != o.GetLanguage() {
continue
}
collection := o.GetCollection()
fileId, ok := fileIdByCollection[collection]
if !ok {
file, _, err := c.getFileForCollection(project, directory.ID, collection)
if err != nil {
return err
}
fileId = file.ID
fileIdByCollection[collection] = file.ID
}
sourceStrings, err := c.getStrings(project.ID, fileId)
if err != nil {
return err
}
for field, value := range o.GetValues() {
if value == "" {
continue
}
identifier := fmt.Sprintf("%s-%d-%s", collection, o.GetItemID(), field)
s, found := lo.Find(sourceStrings, func(s String) bool {
return s.Identifier == identifier
})
if found {
if s.Text != value {
s.Text = value
log.L.Debug().Str("identifier", s.Identifier).Msg("Updating string")
_, err := c.setString(project.ID, s)
if err != nil {
return err
}
}
} else {
log.L.Debug().Str("identifier", identifier).Msg("Creating string")
_, err = c.addString(project.ID, fileId, String{
FileID: fileId,
Identifier: identifier,
Text: value,
})
if err != nil {
return err
}
}
}
}
}
log.L.Debug().Msg("Saved translations")
return nil
}