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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dateutil.parser
2
+
3
+
4
+ rating_map = {
5
+ "general": ["safe"],
6
+ "sensitive": ["sensitive"],
7
+ "questionable": ["nsfw"],
8
+ "explicit": ["explicit", "nsfw"],
9
+ "g": ["safe"],
10
+ "s": ["sensitive"],
11
+ "q": ["nsfw"],
12
+ "e": ["explicit", "nsfw"],
13
+ }
14
+
15
+ special_tags = {
16
+ "1girl",
17
+ "2girls",
18
+ "3girls",
19
+ "4girls",
20
+ "5girls",
21
+ "6+girls",
22
+ "multiple_girls",
23
+ "1boy",
24
+ "2boys",
25
+ "3boys",
26
+ "4boys",
27
+ "5boys",
28
+ "6+boys",
29
+ "multiple_boys",
30
+ "male_focus",
31
+ "1other",
32
+ "2others",
33
+ "3others",
34
+ "4others",
35
+ "5others",
36
+ "6+others",
37
+ "multiple_others",
38
+ }
39
+
40
+
41
+
42
+ meta_keywords_black_list_path = "meta_tag_black_list.txt"
43
+ with open(meta_keywords_black_list_path, "r") as f:
44
+ meta_keywords_black_list = f.read().splitlines()
45
+
46
+
47
+
48
+ fav_count_percentile_full = {
49
+ "general": {
50
+ 5: 1, 10: 1, 15: 2, 20: 3, 25: 3, 30: 4, 35: 5,
51
+ 40: 6, 45: 7, 50: 8, 55: 9, 60: 10, 65: 12,
52
+ 70: 14, 75: 16, 80: 18, 85: 22, 90: 27, 95: 37
53
+ },
54
+ "g": {
55
+ 5: 1, 10: 1, 15: 2, 20: 3, 25: 3, 30: 4, 35: 5,
56
+ 40: 6, 45: 7, 50: 8, 55: 9, 60: 10, 65: 12,
57
+ 70: 14, 75: 16, 80: 18, 85: 22, 90: 27, 95: 37
58
+ },
59
+ "sensitive": {
60
+ 5: 1, 10: 2, 15: 4, 20: 5, 25: 6, 30: 8, 35: 9,
61
+ 40: 11, 45: 13, 50: 15, 55: 17, 60: 19, 65: 22,
62
+ 70: 26, 75: 30, 80: 36, 85: 44, 90: 56, 95: 81
63
+ },
64
+ "s": {
65
+ 5: 1, 10: 2, 15: 4, 20: 5, 25: 6, 30: 8, 35: 9,
66
+ 40: 11, 45: 13, 50: 15, 55: 17, 60: 19, 65: 22,
67
+ 70: 26, 75: 30, 80: 36, 85: 44, 90: 56, 95: 81
68
+ },
69
+ "questionable": {
70
+ 5: 4, 10: 8, 15: 11, 20: 14, 25: 18, 30: 21, 35: 25,
71
+ 40: 29, 45: 33, 50: 38, 55: 43, 60: 49, 65: 56,
72
+ 70: 65, 75: 75, 80: 88, 85: 105, 90: 132, 95: 182
73
+ },
74
+ "q": {
75
+ 5: 4, 10: 8, 15: 11, 20: 14, 25: 18, 30: 21, 35: 25,
76
+ 40: 29, 45: 33, 50: 38, 55: 43, 60: 49, 65: 56,
77
+ 70: 65, 75: 75, 80: 88, 85: 105, 90: 132, 95: 182
78
+ },
79
+ "explicit": {
80
+ 5: 4, 10: 9, 15: 13, 20: 18, 25: 22, 30: 27, 35: 33,
81
+ 40: 39, 45: 45, 50: 52, 55: 60, 60: 69, 65: 79,
82
+ 70: 92, 75: 106, 80: 125, 85: 151, 90: 190, 95: 262
83
+ },
84
+ "e": {
85
+ 5: 4, 10: 9, 15: 13, 20: 18, 25: 22, 30: 27, 35: 33,
86
+ 40: 39, 45: 45, 50: 52, 55: 60, 60: 69, 65: 79,
87
+ 70: 92, 75: 106, 80: 125, 85: 151, 90: 190, 95: 262
88
+ },
89
+ }
90
+
91
+ score_percentile_full = {
92
+ "general": {
93
+ 5: 0,
94
+ 10: 1,
95
+ 15: 2,
96
+ 20: 3,
97
+ 25: 3,
98
+ 30: 4,
99
+ 35: 5,
100
+ 40: 5,
101
+ 45: 6,
102
+ 50: 7,
103
+ 55: 8,
104
+ 60: 9,
105
+ 65: 11,
106
+ 70: 12,
107
+ 75: 14,
108
+ 80: 16,
109
+ 85: 19,
110
+ 90: 24,
111
+ 95: 33,
112
+ },
113
+ "sensitive": {
114
+ 5: 0,
115
+ 10: 1,
116
+ 15: 2,
117
+ 20: 3,
118
+ 25: 4,
119
+ 30: 5,
120
+ 35: 6,
121
+ 40: 7,
122
+ 45: 8,
123
+ 50: 9,
124
+ 55: 11,
125
+ 60: 12,
126
+ 65: 15,
127
+ 70: 17,
128
+ 75: 20,
129
+ 80: 25,
130
+ 85: 31,
131
+ 90: 41,
132
+ 95: 62,
133
+ },
134
+ "questionable": {
135
+ 5: 2,
136
+ 10: 4,
137
+ 15: 5,
138
+ 20: 7,
139
+ 25: 9,
140
+ 30: 11,
141
+ 35: 14,
142
+ 40: 16,
143
+ 45: 19,
144
+ 50: 23,
145
+ 55: 26,
146
+ 60: 31,
147
+ 65: 36,
148
+ 70: 42,
149
+ 75: 49,
150
+ 80: 59,
151
+ 85: 73,
152
+ 90: 93,
153
+ 95: 134,
154
+ },
155
+ "explicit": {
156
+ 5: 2,
157
+ 10: 4,
158
+ 15: 7,
159
+ 20: 10,
160
+ 25: 13,
161
+ 30: 17,
162
+ 35: 20,
163
+ 40: 25,
164
+ 45: 29,
165
+ 50: 35,
166
+ 55: 41,
167
+ 60: 48,
168
+ 65: 56,
169
+ 70: 66,
170
+ 75: 78,
171
+ 80: 94,
172
+ 85: 115,
173
+ 90: 148,
174
+ 95: 211,
175
+ },
176
+ "g": {
177
+ 5: 0,
178
+ 10: 1,
179
+ 15: 2,
180
+ 20: 3,
181
+ 25: 3,
182
+ 30: 4,
183
+ 35: 5,
184
+ 40: 5,
185
+ 45: 6,
186
+ 50: 7,
187
+ 55: 8,
188
+ 60: 9,
189
+ 65: 11,
190
+ 70: 12,
191
+ 75: 14,
192
+ 80: 16,
193
+ 85: 19,
194
+ 90: 24,
195
+ 95: 33,
196
+ },
197
+ "s": {
198
+ 5: 0,
199
+ 10: 1,
200
+ 15: 2,
201
+ 20: 3,
202
+ 25: 4,
203
+ 30: 5,
204
+ 35: 6,
205
+ 40: 7,
206
+ 45: 8,
207
+ 50: 9,
208
+ 55: 11,
209
+ 60: 12,
210
+ 65: 15,
211
+ 70: 17,
212
+ 75: 20,
213
+ 80: 25,
214
+ 85: 31,
215
+ 90: 41,
216
+ 95: 62,
217
+ },
218
+ "q": {
219
+ 5: 2,
220
+ 10: 4,
221
+ 15: 5,
222
+ 20: 7,
223
+ 25: 9,
224
+ 30: 11,
225
+ 35: 14,
226
+ 40: 16,
227
+ 45: 19,
228
+ 50: 23,
229
+ 55: 26,
230
+ 60: 31,
231
+ 65: 36,
232
+ 70: 42,
233
+ 75: 49,
234
+ 80: 59,
235
+ 85: 73,
236
+ 90: 93,
237
+ 95: 134,
238
+ },
239
+ "e": {
240
+ 5: 2,
241
+ 10: 4,
242
+ 15: 7,
243
+ 20: 10,
244
+ 25: 13,
245
+ 30: 17,
246
+ 35: 20,
247
+ 40: 25,
248
+ 45: 29,
249
+ 50: 35,
250
+ 55: 41,
251
+ 60: 48,
252
+ 65: 56,
253
+ 70: 66,
254
+ 75: 78,
255
+ 80: 94,
256
+ 85: 115,
257
+ 90: 148,
258
+ 95: 211,
259
+ },
260
+ }
261
+
262
+ score_percentile_after_5m = {
263
+ "general": {
264
+ 5: 1,
265
+ 10: 1,
266
+ 15: 2,
267
+ 20: 3,
268
+ 25: 4,
269
+ 30: 5,
270
+ 35: 5,
271
+ 40: 6,
272
+ 45: 7,
273
+ 50: 8,
274
+ 55: 10,
275
+ 60: 11,
276
+ 65: 13,
277
+ 70: 14,
278
+ 75: 17,
279
+ 80: 19,
280
+ 85: 23,
281
+ 90: 28,
282
+ 95: 38,
283
+ },
284
+ "sensitive": {
285
+ 5: 2,
286
+ 10: 4,
287
+ 15: 7,
288
+ 20: 9,
289
+ 25: 11,
290
+ 30: 13,
291
+ 35: 16,
292
+ 40: 18,
293
+ 45: 21,
294
+ 50: 24,
295
+ 55: 28,
296
+ 60: 31,
297
+ 65: 36,
298
+ 70: 41,
299
+ 75: 48,
300
+ 80: 56,
301
+ 85: 67,
302
+ 90: 84,
303
+ 95: 118,
304
+ },
305
+ "questionable": {
306
+ 5: 4,
307
+ 10: 9,
308
+ 15: 14,
309
+ 20: 19,
310
+ 25: 24,
311
+ 30: 29,
312
+ 35: 34,
313
+ 40: 40,
314
+ 45: 45,
315
+ 50: 52,
316
+ 55: 59,
317
+ 60: 67,
318
+ 65: 75,
319
+ 70: 86,
320
+ 75: 98,
321
+ 80: 114,
322
+ 85: 136,
323
+ 90: 168,
324
+ 95: 228,
325
+ },
326
+ "explicit": {
327
+ 5: 5,
328
+ 10: 11,
329
+ 15: 16,
330
+ 20: 22,
331
+ 25: 28,
332
+ 30: 34,
333
+ 35: 40,
334
+ 40: 47,
335
+ 45: 54,
336
+ 50: 63,
337
+ 55: 72,
338
+ 60: 82,
339
+ 65: 94,
340
+ 70: 108,
341
+ 75: 124,
342
+ 80: 145,
343
+ 85: 174,
344
+ 90: 216,
345
+ 95: 296,
346
+ },
347
+ "g": {
348
+ 5: 1,
349
+ 10: 1,
350
+ 15: 2,
351
+ 20: 3,
352
+ 25: 4,
353
+ 30: 5,
354
+ 35: 5,
355
+ 40: 6,
356
+ 45: 7,
357
+ 50: 8,
358
+ 55: 10,
359
+ 60: 11,
360
+ 65: 13,
361
+ 70: 14,
362
+ 75: 17,
363
+ 80: 19,
364
+ 85: 23,
365
+ 90: 28,
366
+ 95: 38,
367
+ },
368
+ "s": {
369
+ 5: 2,
370
+ 10: 4,
371
+ 15: 7,
372
+ 20: 9,
373
+ 25: 11,
374
+ 30: 13,
375
+ 35: 16,
376
+ 40: 18,
377
+ 45: 21,
378
+ 50: 24,
379
+ 55: 28,
380
+ 60: 31,
381
+ 65: 36,
382
+ 70: 41,
383
+ 75: 48,
384
+ 80: 56,
385
+ 85: 67,
386
+ 90: 84,
387
+ 95: 118,
388
+ },
389
+ "q": {
390
+ 5: 4,
391
+ 10: 9,
392
+ 15: 14,
393
+ 20: 19,
394
+ 25: 24,
395
+ 30: 29,
396
+ 35: 34,
397
+ 40: 40,
398
+ 45: 45,
399
+ 50: 52,
400
+ 55: 59,
401
+ 60: 67,
402
+ 65: 75,
403
+ 70: 86,
404
+ 75: 98,
405
+ 80: 114,
406
+ 85: 136,
407
+ 90: 168,
408
+ 95: 228,
409
+ },
410
+ "e": {
411
+ 5: 5,
412
+ 10: 11,
413
+ 15: 16,
414
+ 20: 22,
415
+ 25: 28,
416
+ 30: 34,
417
+ 35: 40,
418
+ 40: 47,
419
+ 45: 54,
420
+ 50: 63,
421
+ 55: 72,
422
+ 60: 82,
423
+ 65: 94,
424
+ 70: 108,
425
+ 75: 124,
426
+ 80: 145,
427
+ 85: 174,
428
+ 90: 216,
429
+ 95: 296,
430
+ },
431
+ }
432
+
433
+
434
+
435
+ def year_tag(
436
+ created_at
437
+ ) -> str:
438
+ year = 0
439
+ try:
440
+ date = dateutil.parser.parse(created_at)
441
+ year = date.year
442
+ except:
443
+ pass
444
+ if 2005 <= year <= 2010:
445
+ year_tag = "old"
446
+ elif year <= 2014:
447
+ year_tag = "early"
448
+ elif year <= 2017:
449
+ year_tag = "mid"
450
+ elif year <= 2020:
451
+ year_tag = "recent"
452
+ elif year <= 2024:
453
+ year_tag = "newest"
454
+ else:
455
+ return None
456
+
457
+ return year_tag
458
+
459
+
460
+ def rating_tag(
461
+ rating
462
+ ) -> str:
463
+ if (tag := rating_map.get(rating, None)) is not None:
464
+ return tag[0]
465
+ else:
466
+ return None
467
+
468
+
469
+ def quality_tag(
470
+ id,
471
+ fav_count,
472
+ rating,
473
+
474
+ percentile_map: dict[str, dict[int, int]] = fav_count_percentile_full,
475
+ ) -> tuple[list[str], list[str]]:
476
+ if int(id) > 7800000:
477
+ # Don't add quality tag for posts which are new.
478
+ return None
479
+ else:
480
+ percentile_map = fav_count_percentile_full
481
+ rating = rating
482
+ score = fav_count
483
+ percentile = percentile_map[rating]
484
+
485
+ if score > percentile[95]:
486
+ quality_tag = "masterpiece"
487
+ elif score > percentile[85]:
488
+ quality_tag = "best quality"
489
+ elif score > percentile[75]:
490
+ quality_tag = "great quality"
491
+ elif score > percentile[50]:
492
+ quality_tag = "good quality"
493
+ elif score > percentile[25]:
494
+ quality_tag = "normal quality"
495
+ elif score > percentile[10]:
496
+ quality_tag = "low quality"
497
+ else:
498
+ quality_tag = "worst quality"
499
+
500
+
501
+
502
+ return quality_tag
503
+
504
+ def tags_filter(tag, black_list: list[str]) -> bool:
505
+ return not any(keyword in tag for keyword in black_list)
506
+
507
+
508
+ def meta_tags_filter(tags) -> list[str]:
509
+
510
+
511
+ # NOTE: we only filter out meta tags with these keywords
512
+ # Which is definitely not related to the content of image
513
+ return [tag for tag in tags if tags_filter(tag, meta_keywords_black_list)]
514
+
515
+ def extract_special_tags(tag_list) -> tuple[list[str], list[str]]:
516
+ special = []
517
+ general = []
518
+ for tag in tag_list:
519
+ if tag in special_tags:
520
+ special.append(tag)
521
+ else:
522
+ general.append(tag)
523
+ return special, general
524
+
525
+
526
+
527
+