-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_gif_migration.go
578 lines (478 loc) · 16.3 KB
/
gf_gif_migration.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_gif_lib
import (
"fmt"
"time"
"strings"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"github.com/gloflow/gloflow/go/gf_extern_services/gf_aws"
"github.com/gloflow/gloflow/go/gf_apps/gf_images_lib/gf_images_core"
"github.com/gloflow/gloflow/go/gf_core"
)
//--------------------------------------------------
// TEMPORARY - only used for a little while, until all GIF format images are also
// created in their GIF record form. new versions of crawler and chrome_ext
// logic has proper creation of both image and gif DB records, but old
// image only DB records need to be processed
func InitImgToGIFmigration(p_images_store_local_dir_path_str string,
p_s3_bucket_name_str string,
p_s3_info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) {
gf_domain_str := "gloflow.com"
fmt.Println(gf_domain_str)
/*migrate__fix_gif_urls(p_images_store_local_dir_path_str,
gf_domain_str,
p_s3_bucket_name_str,
p_s3_client,
p_s3_uploader,
p_mongodb_coll,
pLogFun)*/
/*migrate__create_gifs_from_images(p_images_store_local_dir_path_str,
p_s3_bucket_name_str,
p_s3_client,
p_s3_uploader,
p_mongodb_coll,
pLogFun)*/
/*// DELETE_DUPLICATES
go func() {
for {
pipe := p_mongodb_coll.Pipe([]bson.M{
bson.M{"$match":bson.M{
"t":"gif",
},
},
bson.M{"$group":bson.M{
"_id": "$origin_url_str",
"count_int":bson.M{"$sum":1},
"ids_lst": bson.M{"$push":"$id_str",},
},
},
})
results_lst := []map[string]interface{}{}
err := pipe.All(&results_lst)
for _,r_map := range results_lst {
if r_map["count_int"].(int) > 1 {
for _,id_str := range r_map["ids_lst"].([]string) {
}
}
}
}
}*/
//--------------------------------------------------
/*// ADD_GIFS_FLOW_NAME_TO_GIF_IMGS
go func() {
for {
//get all images that dont have flowsNamesLst
var imgs_lst []gf_images_core.GF_Image
err := p_mongodb_coll.Find(bson.M{
"t": "img",
"format_str": "gif",
"flowsNamesLst":bson.M{"$nin":[]string{"gifs",}},
}).All(&imgs_lst)
//all img's are migrated
if fmt.Sprint(err) == "not found" {
return
}
if err != nil {
pLogFun("ERROR",fmt.Sprint(err))
continue
}
//update each one with flowsNamesLst field, and add flow_name_str to that list
for _,img := range imgs_lst {
err := p_mongodb_coll.Update(bson.M{
"t": "img",
"id_str": img.Id_str,
"format_str":"gif",
},
bson.M{"$push":bson.M{"flowsNamesLst":"gifs",},})
if err != nil {
pLogFun("ERROR",fmt.Sprint(err))
continue
}
}
}
}()*/
}
//--------------------------------------------------
// FIX_GIF_URLS - try to fetch first frame image of a GIF, and if fails
// regenerate GIF preview frames.
func migrateFixGIFurls(p_images_store_local_dir_path_str string,
p_gf_domain_str string,
p_media_domain_str string,
p_s3_bucket_name_str string,
p_s3_info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) {
go func() {
fmt.Println("ENTERING LOOP 1")
for {
time.Sleep(time.Second * 5)
fmt.Println("FIX_GIF_URLS ----------- >>>")
ctx := context.Background()
pipeline := mongo.Pipeline{
{
{"$match", bson.M{
"t": "gif",
"$or": []bson.M{
// IMPORTANT!! - valid_bool is a new field. on most docs it doesnt exist yet,
// but on some it does. check that it exists, and if it does check
// that its false (only invalid docs are fixed)
bson.M{"valid_bool": bson.M{"$exists": false,},},
bson.M{"valid_bool": false},
}}},
},
{
{"$sample", bson.M{"size": 1}},
},
}
/*pipe := pRuntimeSys.Mongo_coll.Pipe([]bson.M{
bson.M{"$match": bson.M{
"t": "gif",
"$or": []bson.M{
// IMPORTANT!! - valid_bool is a new field. on most docs it doesnt exist yet,
// but on some it does. check that it exists, and if it does check
// that its false (only invalid docs are fixed)
bson.M{"valid_bool": bson.M{"$exists": false,},},
bson.M{"valid_bool": false},
},
},
},
bson.M{"$sample": bson.M{
"size": 1,
},
},
})*/
cursor, err := pRuntimeSys.Mongo_coll.Aggregate(ctx, pipeline)
if err != nil {
_ = gf_core.MongoHandleError("failed to run FIX_GIF_URLS migration aggregation_pipeline to get a single GIF",
"mongodb_aggregation_error",
nil,
err, "gf_gif_lib", pRuntimeSys)
continue
}
defer cursor.Close(ctx)
/*var old_gif Gf_gif
err := pipe.One(&old_gif)
if err != nil {
_ = gf_core.MongoHandleError("failed to run FIX_GIF_URLS migration aggregation_pipeline to get a single GIF",
"mongodb_aggregation_error", nil, err, "gf_gif_lib", pRuntimeSys)
continue
}*/
cursor.Next(ctx)
var old_gif GFgif
err = cursor.Decode(&old_gif)
if err != nil {
_ = gf_core.MongoHandleError("failed to run FIX_GIF_URLS migration aggregation_pipeline to get a single GIF",
"mongodb_cursor_decode",
nil,
err, "gf_gif_lib", pRuntimeSys)
continue
}
//-----------------------
// FETCH_GF_URL
fmt.Println(" > origin_page_url - "+old_gif.Origin_page_url_str)
fmt.Println(" > origin_url - "+old_gif.Origin_url_str)
fmt.Println(" > gf_url - "+old_gif.Gf_url_str)
// IMPORTANT!! - old_gif.Gf_url_str in is form - "/images/d/gif"
gif__full_gf_url_str := fmt.Sprintf("http://%s%s",p_gf_domain_str,old_gif.Gf_url_str)
headers_map := map[string]string{}
user_agent_str := "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
gf_http_fetch, gfErr := gf_core.HTTPfetchURL(gif__full_gf_url_str,
headers_map,
user_agent_str,
context.Background(),
pRuntimeSys)
// IMPORTANT!! - http response body must be closed, regardless of if its used or not (by goland docs)
defer gf_http_fetch.Resp.Body.Close()
// IMPORTANT!! - common error for malformed url's is:
// "parse /images/d/gif/%!!(MISSING)s(*string=0xc4201f4040).gif: invalid URL escape "%!!(MISSING)s""
// so for them GIF's are rebuilt as well.
if gfErr !=nil && strings.HasPrefix(fmt.Sprint(gfErr.Error),"parse") {
rg__gfErr := migrateRebuildGIF(&old_gif,
p_images_store_local_dir_path_str,
p_media_domain_str,
p_s3_bucket_name_str,
p_s3_info,
pRuntimeSys)
if rg__gfErr != nil {
continue
}
}
if gfErr != nil {
continue
}
// FAILED_TO_FETCH_GF_URL
if !(gf_http_fetch.Status_code_int >= 200 && gf_http_fetch.Status_code_int < 400) {
// REBUILD_GIF
rg__gfErr := migrateRebuildGIF(&old_gif,
p_images_store_local_dir_path_str,
p_media_domain_str,
p_s3_bucket_name_str,
p_s3_info,
pRuntimeSys)
if rg__gfErr != nil {
continue
}
}
//-----------------------
// FETCH_FIRST_PREVIEW_FRAME
frame_url_str := old_gif.PreviewFramesS3urlsLst[0]
headers_map = map[string]string{}
user_agent_str = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
fpf__gf_http_fetch, gfErr := gf_core.HTTPfetchURL(frame_url_str,
headers_map,
user_agent_str,
context.Background(),
pRuntimeSys)
// IMPORTANT!! - common error for malformed url's is:
// "parse /images/d/gif/%!!(MISSING)s(*string=0xc4201f4040).gif: invalid URL escape "%!!(MISSING)s""
// so for them GIF's are rebuilt as well.
if gfErr != nil && strings.HasPrefix(fmt.Sprint(gfErr.Error),"parse") {
rg__gfErr := migrateRebuildGIF(&old_gif,
p_images_store_local_dir_path_str,
p_media_domain_str,
p_s3_bucket_name_str,
p_s3_info,
pRuntimeSys)
if rg__gfErr != nil {
continue
}
}
if gfErr != nil {
continue
}
// IMPORTANT!! - http response body must be closed, regardless of if its used or not (by goland docs)
defer fpf__gf_http_fetch.Resp.Body.Close()
//-----------------------
}
}()
}
//--------------------------------------------------
func migrateCreateGIFsFromImages(p_images_store_local_dir_path_str string,
p_media_domain_str string,
p_s3_bucket_name_str string,
p_s3_info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) {
//--------------------------------------------------
// CREATE_GIFS_FROM_IMAGES - for all 'img' DB objects with format 'gif', process it
// and create a 'gif' DB object
go func() {
fmt.Println("ENTERING LOOP 2")
for {
time.Sleep(time.Second * 5)
fmt.Println("CREATE_GIFS_FROM_IMAGES ----------- >>>")
//---------------------
// IMPORTANT!! - get a truly random img with GIF format
ctx := context.Background()
pipeline := mongo.Pipeline{
{
{"$match", bson.M{
"t": "img",
"format_str": "gif",
}},
},
{
{"$sample", bson.M{"size": 1}},
},
}
/*pipe := pRuntimeSys.Mongo_coll.Pipe([]bson.M{
bson.M{"$match":bson.M{
"t": "img",
"format_str": "gif",
},
},
bson.M{"$sample": bson.M{
"size": 1,
},
},
})*/
cursor, err := pRuntimeSys.Mongo_coll.Aggregate(ctx, pipeline)
if err != nil {
_ = gf_core.MongoHandleError("failed to run CREATE_GIFS_FROM_IMAGES migration aggregation_pipeline to get a single GIF",
"mongodb_aggregation_error",
nil,
err, "gf_gif_lib", pRuntimeSys)
continue
}
defer cursor.Close(ctx)
/*var img gf_images_core.Gf_image
err := pipe.One(&img)
if err != nil {
_ = gf_core.MongoHandleError("failed to run CREATE_GIFS_FROM_IMAGES migration aggregation_pipeline to get a single GIF",
"mongodb_aggregation_error", nil, err, "gf_gif_lib", pRuntimeSys)
continue
}*/
cursor.Next(ctx)
var img gf_images_core.GFimage
err = cursor.Decode(&img)
if err != nil {
_ = gf_core.MongoHandleError("failed to run CREATE_GIFS_FROM_IMAGES migration aggregation_pipeline to get a single GIF",
"mongodb_cursor_decode",
nil,
err, "gf_gif_lib", pRuntimeSys)
continue
}
//---------------------
var gif GFgif
err = pRuntimeSys.Mongo_coll.FindOne(ctx, bson.M{
"t": "gif",
"origin_url_str": img.Origin_url_str,
"title_str": bson.M{"$exists": 1,}, // IMPORTANT!! - new field added
"origin_page_url_str": bson.M{"$exists": 1,}, // IMPORTANT!! - new field added
"tags_lst": bson.M{"$exists": 1,}, // IMPORTANT!! - new field added
}).Decode(&gif)
// IMPORTANT!! - a "gif" object was not found in the DB for an "img"
// with "gif" format. so a new gif is created
if err == mongo.ErrNoDocuments {
fmt.Println("=================================")
fmt.Println("")
fmt.Println(" MIGRATING DB IMG->GIF - "+img.Origin_url_str)
fmt.Println("")
fmt.Println("=================================")
// IMPORTANT!! - emtpy because its not being used here (new GF_Image not created)
// p_create_new_db_img_bool is set to 'false'.
image_client_type_str := ""
flowsNamesLst, gfErr := migrateGetFlowsNames(img.IDstr, pRuntimeSys)
if gfErr != nil {
continue
}
ctx := context.Background()
// IMPORTANT!! - image is re-fetched and GIF is processed in full
_, _, gfErr = Process("", // p_gf_image_id_str
img.Origin_url_str, // p_image_source_url_str,
img.Origin_page_url_str,
p_images_store_local_dir_path_str,
image_client_type_str,
flowsNamesLst,
false, // p_create_new_db_img_bool
p_media_domain_str,
p_s3_bucket_name_str,
p_s3_info,
ctx,
pRuntimeSys)
if gfErr != nil {
continue
}
continue
}
if err != nil {
continue
}
}
}()
}
//--------------------------------------------------
func migrateRebuildGIF(p_old_gif *GFgif,
p_images_store_local_dir_path_str string,
p_media_domain_str string,
p_s3_bucket_name_str string,
p_s3_info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//----------------
// PROCESS_GIF
// IMPORTANT!! - emtpy because its not being used here (new GF_Image not created)
// p_create_new_db_img_bool is set to 'false'.
image_client_type_str := ""
flowsNamesLst, gfErr := migrateGetFlowsNames(p_old_gif.GFimageIDstr, pRuntimeSys)
if gfErr != nil {
return gfErr
}
ctx := context.Background()
new_gif, gfErr := ProcessAndUpload("", // p_gf_image_id_str
p_old_gif.Origin_url_str, // p_image_source_url_str
p_old_gif.Origin_page_url_str, // p_image_origin_page_url_str
p_images_store_local_dir_path_str,
image_client_type_str,
flowsNamesLst,
false, // p_create_new_db_img_bool
p_media_domain_str,
p_s3_bucket_name_str,
p_s3_info,
ctx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//----------------
// UPDATE_GIF_TO_OLD_CREATION_TIME - so that when sorted lists of GIFs from the DB are fetched
// these newly created GIFs are returned in proper positon.
_, err := pRuntimeSys.Mongo_coll.UpdateMany(ctx, bson.M{
"t": "gif",
"id_str": new_gif.Id_str,
},
bson.M{
"$set": bson.M{"creation_unix_time_f": p_old_gif.Creation_unix_time_f},
})
if err != nil {
gfErr := gf_core.MongoHandleError("failed to update a new migrated GIF with an old creation_unix_time_f (of the old GIF) in mongodb",
"mongodb_update_error",
map[string]interface{}{
"old_gif_id_str":p_old_gif.Id_str,
"new_gif_id_str":new_gif.Id_str,
},
err, "gf_gif_lib", pRuntimeSys)
return gfErr
}
//----------------
// DELETE_OLD_GIF - the one that was rebuilt
dbMongoDelete(p_old_gif.Id_str, pRuntimeSys)
//----------------
return nil
}
//--------------------------------------------------
func migrateGetFlowsNames(p_gif__gf_image_id_str gf_images_core.GFimageID,
pRuntimeSys *gf_core.RuntimeSys) ([]string, *gf_core.GFerror) {
var flowsNamesLst []string
// IMPORTANT!! - GIF is not linked to a particular GF_Image
if p_gif__gf_image_id_str != "" {
ctx := context.Background()
var gf_img gf_images_core.GFimage
err := pRuntimeSys.Mongo_coll.FindOne(ctx, bson.M{"t": "img", "id_str": p_gif__gf_image_id_str,}).Decode(&gf_img)
// err := pRuntimeSys.Mongo_coll.Find(bson.M{"t": "img", "id_str": p_gif__gf_image_id_str,}).One(&gf_img)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to find images with GIF id_str",
"mongodb_find_error",
map[string]interface{}{"gif__gf_image_id_str": p_gif__gf_image_id_str,},
err, "gf_gif_lib", pRuntimeSys)
return nil, gfErr
}
// IMPORTANT!! - gf_img.FlowsNamesLst is a new field, allowing images to belong to multiple
// flows. before there was only the Flow_name_str field.
// so in the beginning most GF_Image's will not have "flowsNamesLst" set in the DB,
// and will contain the default value when loaded by mgo (empty list)
if len(gf_img.FlowsNamesLst) > 0 {
flowsNamesLst = gf_img.FlowsNamesLst
hasGIFflowBool := false
for _, s := range flowsNamesLst {
// flows list might contain "gifs" tag
if s == "gifs" {
hasGIFflowBool = true
}
}
// only add "gifs" flow if it doesnt exist
if !hasGIFflowBool {
flowsNamesLst = append(flowsNamesLst, "gifs")
}
} else {
flowsNamesLst = []string{"gifs",}
}
} else {
flowsNamesLst = []string{"gifs",}
}
return flowsNamesLst,nil
}