-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot 3.0.py
635 lines (547 loc) · 21.6 KB
/
bot 3.0.py
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import sqlite3
import time
import datetime
import requests
from settings import token, nickname
"""
class Validator:
# ALLOW_TYPE = ['tv', 'ova', 'ona', 'movie', 'special']
ALLOW_TYPE_OP_ED = ['ending', 'opening', 'preview']
def __init__(self, anime_info, video_info, allow_type=None):
if allow_type is None:
self.ALLOW_TYPE = ['tv', 'ova', 'ona', 'movie', 'special']
else:
self.ALLOW_TYPE = allow_type
self.ALLOW_TYPE = allow_type
self.anime_info = anime_info
self.video_info = video_info
self.validators = [self._is_aired_episode(), self._check_duration(), self._is_allow_type(),
self._is_active(), self._has_kind(), self._has_lang(), self._check_number_of_episode()]
def validate(self) -> bool:
print('_validate')
if self._has_mal_id():
if self._is_404() or self._is_anons(): # fixme op/ed/pv не пройдут
return False
if all(self.validators):
return True
return False
def _is_404(self) -> bool:
print('_is_404')
if self.anime_info.get('code', 200) == 404:
return True
return False
def _is_anons(self) -> bool:
print('_is_anons')
if self.anime_anons:
return True
return False
def _is_aired_episode(self) -> bool:
print('_is_aired_episode')
if self.episodes_aired + 2 < int(float(self.episode)) and self.episodes_aired != 0:
return False
return True
def _check_duration(self) -> bool:
print('_check_duration')
if self.episode_duration != '0' and self.episode_duration != 0:
duration_in_minutes = self.anime_duration * 60
if float(self.episode_duration) < (duration_in_minutes - (duration_in_minutes / 3)):
return False
else:
return True
return False
def _is_allow_type(self) -> bool:
print('_is_allow_type')
if self.episode_type not in self.ALLOW_TYPE:
return False
return True
def _is_active(self) -> bool:
print('_is_active')
if self.episode_active != 1:
return False
return True
def _has_mal_id(self) -> bool:
print('_has_mal_id')
if self.episode_mal_id == 0:
return False
return True
def _has_kind(self) -> bool:
print('_has_kind')
if self.episode_kind == '':
return False
return True
def _check_kind(self) -> bool:
print('_check_kind')
if self.anime_kind != self.episode_type:
return False
return True
def _has_lang(self) -> bool:
print('_has_lang')
if self.episode_lang == '':
return False
return True
def _check_number_of_episode(self) -> bool:
print('_check_number_of_episode')
if self.episode == '' or self.episode == '0':
return False
elif float(self.episode) / int(float(self.episode)) != 1:
return False
return True
class Anime:
def __init__(self, anime_info):
self.anime_info = anime_info
@property
def get_kind(self) -> str:
return self.anime_info['kind']
@property
def get_duration(self) -> str:
return self.anime_info['duration']
@property
def is_anons(self) -> bool:
return self.anime_info['anons']
@property
def is_aired(self) -> bool:
return self.anime_info['episodes_aired']
class Episode:
def __init__(self, video_info):
self.video_info = video_info
self.kind = self.video_info['typeKind']
self.lang = self.video_info['typeLang']
self.quality = self.video_info['qualityType']
self.type = self.video_info['episode']['episodeType']
@property
def get_id(self):
return self.video_info['id']
@property
def get_quality(self):
return self.quality
@property
def get_author(self):
return self.video_info['authorsSummary']
@property
def get_url(self):
return self.video_info['embedUrl']
@property
def get_number(self):
return self.video_info['episode']['episodeInt']
@property
def get_duration(self):
return self.video_info['duration']
@property
def get_type(self):
return self.type
@property
def get_kind(self):
return self.kind
@property
def get_lang(self):
return self.lang
@property
def get_mal_id(self):
return self.video_info['series']['myAnimeListId']
@property
def is_active(self):
return self.video_info['isActive']
def set_lang(self, new_lang):
self.lang = new_lang
def set_kind(self, new_kind):
self.kind = new_kind
def set_type(self, new_type):
self.type = new_type
def set_quality(self, new_quality):
self.quality = new_quality
class Loader:
USER_AGENT = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
def __init__(self, url, payload=None):
self.url = url
self.payload = payload
def fetch_data(self):
response = requests.get(self.url, params=self.payload, headers=self.USER_AGENT).json()
return response
class New_Bot:
SMORET_ANIME_API_URL = "https://smotret-anime.ru/api/translations/?"
SHIKIMORI_API_URL = 'https://shikimori.org/api/animes/'
def recent(self):
translations = Loader(self.SMORET_ANIME_API_URL, payload={'feed': 'recent', 'limit': 50}).fetch_data()['data']
for translation in translations:
episode = Episode(translation)
time.sleep(0.7)
anime = Loader(self.SHIKIMORI_API_URL + episode.get_mal_id).fetch_data()
is_valid = Validator(anime, episode).validate()
@staticmethod
def before_add_to_db(translation):
print('_before_add_to_db')
if translation.get_kind == 'sub':
translation.set_kind('subtitles')
elif translation.get_kind == 'raw':
translation.set_kind('raw')
elif translation.get_kind == 'voice':
translation.set_kind('fandub')
else:
translation.set_kind('unknown')
if translation.get_lang == 'ru':
translation.set_lang('russian')
elif translation.get_lang == 'en':
translation.set_lang('english')
elif translation.get_lang == 'ja' or translation.get_lang == 'jp':
translation.set_lang('original')
else:
translation['typeLang'] = 'unknown'
if translation.get_quality == 'tv':
translation.set_quality('tv')
elif translation.get_quality == 'bd':
translation.set_quality('bd')
elif translation.get_quality == 'dvd':
translation.set_quality('dvd')
else:
translation.set_quality('unknown')
if translation.get_type == 'opening':
translation.set_type('op')
elif translation.get_type == 'ending':
translation.set_type('ed')
elif translation.get_type == 'preview':
translation.set_type('pv')
else:
translation.set_type('other')
return translation
def run(self):
while True:
try:
print('ok')
print(datetime.datetime.now())
print('_______________________')
except Exception:
print('Exception')
continue
"""
class Bot(object):
ALLOW_TYPE = ['tv', 'ova', 'ona', 'movie', 'special']
ALLOW_TYPE_OP_ED = ['ending', 'opening', 'preview']
SMORET_ANIME_API_URL = "https://smotret-anime.ru/api/translations/?"
USER_AGENT = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
HEADERS = {'X-User-Nickname': nickname,
'X-User-Api-Access-Token': token,
'content-type': 'application/json'}
def __init__(self):
self.conn = sqlite3.connect('anime365.db')
def __del__(self):
self.conn.close()
def _fetch_updates_from_smotret_anime(self):
print('_fetch_updates_from_smotret_anime')
payload = {
'feed': 'updatedDateTime',
# 'pretty': '1',
# 'limit': '1000'
}
r = requests.get(self.SMORET_ANIME_API_URL, params=payload, headers=self.USER_AGENT).json()
return r['data']
def check_changes(self):
print('check_changes')
c = self.conn.cursor()
for x in self._fetch_updates_from_smotret_anime():
c.execute("SELECT * FROM recent_translations WHERE (anime365_id LIKE ?)", (x['id'],))
db = c.fetchone()
if db:
if x['episode']['episodeType'] not in self.ALLOW_TYPE \
or int(x['episode']['episodeInt']) != db[6] \
or x['series']['myAnimeListId'] != db[1] or x['isActive'] != 1:
c.execute('INSERT INTO delete_broken (anime_id, video_id, url, deleted) VALUES (?, ?, ?, ?)',
(db[1],
db[11],
db[5],
0))
self.conn.commit()
c.execute("DELETE FROM recent_translations WHERE anime365_id=?", (x['id'],))
self.conn.commit()
if self._validate(x):
translation = self._before_add_to_db(x)
self._add_to_db(translation)
else:
translation = self._before_add_to_db(x)
self._add_to_db(translation, broken=1)
def _add_op_ed_to_db(self, translation):
print('_add_op_ed_to_db')
c = self.conn.cursor()
c.execute('INSERT INTO op_ed (anime_id, \
video_id, \
kind, \
url, \
title) \
VALUES (?, ?, ?, ?, ?)',
(translation['series']['myAnimeListId'],
translation['id'],
translation['episode']['episodeType'],
translation['url'],
translation['title']))
self.conn.commit()
def _add_to_db(self, translation, broken=0):
print('_add_to_db')
c = self.conn.cursor()
c.execute('INSERT INTO recent_translations (mal_id, \
typeKind, \
typeLang,\
authorsSummary,\
url,\
episodeInt, \
anime365_id, \
uploaded, \
qualityType, \
broken) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(translation['series']['myAnimeListId'],
translation['typeKind'],
translation['typeLang'],
translation['authorsSummary'],
translation['url'],
translation['episode']['episodeInt'],
translation['id'],
0,
translation['qualityType'],
broken))
self.conn.commit()
def _fetch_translations_from_smotret_anime(self):
print('_fetch_translations_from_smotret_anime')
payload = {
'feed': 'recent',
# 'pretty': '1',
# 'limit': '1000'
}
r = requests.get(self.SMORET_ANIME_API_URL, params=payload, headers=self.USER_AGENT).json()
return r['data']
def _fetch_anime_info_from_shikimori(self, translation):
print('_fetch_anime_info_from_shikimori')
req = 'https://shikimori.org/api/animes/{anime_id}'.format(anime_id=translation['series']['myAnimeListId'])
response = requests.get(req, headers=self.HEADERS).json()
return response
def get_translations(self):
print('get_translations')
c = self.conn.cursor()
for translation in self._fetch_translations_from_smotret_anime():
c.execute("SELECT * \
FROM recent_translations \
WHERE (anime365_id LIKE ?)", (translation['id'],))
db = c.fetchall()
if len(db) > 0:
pass
else:
if self._validate(translation):
translation = self._before_add_to_db(translation)
self._add_to_db(translation)
elif self._is_allow_type_for_op_ed(translation):
translation = self._before_add_to_db(translation)
self._add_op_ed_to_db(translation)
else:
translation = self._before_add_to_db(translation)
self._add_to_db(translation, broken=1)
def _validate(self, translation):
print('_validate')
if self._has_mal_id(translation):
time.sleep(0.7)
anime_info = self._fetch_anime_info_from_shikimori(translation)
if self._is_404(anime_info) or self._is_anons(anime_info):
return False
if self._is_aired_episode(translation, anime_info) and \
self._check_duration(translation, anime_info) and \
self._is_allow_type(translation) and \
self._is_active(translation) and \
self._has_kind(translation) and \
self._has_lang(translation) and \
self._check_number_of_episode(translation):
return True
return False
@staticmethod
def _before_add_to_db(translation):
print('_before_add_to_db')
if translation['typeKind'] == 'sub':
translation['typeKind'] = 'subtitles'
elif translation['typeKind'] == 'raw':
translation['typeKind'] = 'raw'
elif translation['typeKind'] == 'voice':
translation['typeKind'] = 'fandub'
else:
translation['typeKind'] = 'unknown'
if translation['typeLang'] == 'ru':
translation['typeLang'] = 'russian'
elif translation['typeLang'] == 'en':
translation['typeLang'] = 'english'
elif translation['typeLang'] == 'jp':
translation['typeLang'] = 'original'
else:
translation['typeLang'] = 'unknown'
if translation['qualityType'] == 'tv':
translation['qualityType'] = 'tv'
elif translation['qualityType'] == 'bd':
translation['qualityType'] = 'bd'
elif translation['qualityType'] == 'dvd':
translation['qualityType'] = 'dvd'
else:
translation['qualityType'] = 'unknown'
if translation['episode']['episodeType'] == 'opening':
translation['episode']['episodeType'] = 'op'
elif translation['episode']['episodeType'] == 'ending':
translation['episode']['episodeType'] = 'ed'
elif translation['episode']['episodeType'] == 'preview':
translation['episode']['episodeType'] = 'pv'
else:
translation['episode']['episodeType'] = 'other'
return translation
@staticmethod
def _is_404(anime_info):
print('_is_404')
if anime_info.get('code', 200) == 404:
return True
return False
@staticmethod
def _is_anons(anime_info):
print('_is_anons')
if anime_info['anons']:
return True
return False
@staticmethod
def _is_aired_episode(translation, anime_info):
print('_is_aired_episode')
if anime_info['episodes_aired'] + 2 < int(translation['episode']['episodeInt']) \
and anime_info['episodes_aired'] != 0:
return False
return True
@staticmethod
def _check_duration(translation, anime_info):
print('_check_duration')
if translation['duration'] != '0' and translation['duration'] != 0:
duration_in_minutes = anime_info['duration'] * 60
if float(translation['duration']) < (duration_in_minutes - (duration_in_minutes / 3)):
return False
else:
return True
return False
def _is_allow_type(self, translation):
print('_is_allow_type')
if translation['episode']['episodeType'] not in self.ALLOW_TYPE:
return False
return True
def _is_allow_type_for_op_ed(self, translation):
print('_is_allow_type')
if translation['episode']['episodeType'] not in self.ALLOW_TYPE_OP_ED:
return False
return True
@staticmethod
def _is_active(translation):
print('_is_active')
if translation['isActive'] != 1:
return False
return True
@staticmethod
def _has_mal_id(translation):
print('_has_mal_id')
if translation['series']['myAnimeListId'] == 0:
return False
return True
@staticmethod
def _has_kind(translation):
print('_has_kind')
if translation['typeKind'] == '':
return False
return True
@staticmethod
def _check_kind(translation, anime_info):
print('_check_kind')
if anime_info['kind'] != translation['episode']['episodeType']:
return False
return True
@staticmethod
def _has_lang(translation):
print('_has_lang')
if translation['typeLang'] == '':
return False
return True
@staticmethod
def _check_number_of_episode(translation):
print('_check_number_of_episode')
if translation['episode']['episodeInt'] == '':
return False
elif translation['episode']['episodeInt'] == '0':
return False
elif float(translation['episode']['episodeInt']) / int(float(translation['episode']['episodeInt'])) != 1:
return False
return True
def post_op_ed_to_shikimori(self):
print('post_op_ed_to_shikimori')
link = 'http://shikimori.org/api/animes/{id}/videos'
c = self.conn.cursor()
c.execute("SELECT anime_id, kind, url, title FROM op_ed WHERE uploaded=0")
db = c.fetchall()
for x in db:
time.sleep(0.7)
req = link.format(id=x[0])
anime_video = {
"url": x[2],
"kind": x[1],
"name": x[3]
}
data = {'video': anime_video}
param = requests.post(req, json=data, headers=self.HEADERS)
print(param.json())
c.execute('UPDATE op_ed SET uploaded=? WHERE anime_id=?', (1, x[0]))
self.conn.commit()
def _post_video_to_shikimori(self, anime_id, author_name, episode, kind, language, source, url, quality_type):
print('_post_video_to_shikimori')
c = self.conn.cursor()
link = 'https://shikimori.org/api/animes/{id}/anime_videos'
req = link.format(id=anime_id)
anime_video = {'anime_id': int(anime_id),
"state": "uploaded",
'author_name': str(author_name),
'episode': int(episode),
'kind': str(kind),
'language': str(language),
'source': str(source),
'url': str(url),
'quality': str(quality_type)}
data = {'anime_video': anime_video}
r = requests.post(req, json=data, headers=self.HEADERS)
print(r.json())
if r.json().get('errors') == ['Url видео с такой ссылкой уже добавлено']:
c.execute('UPDATE recent_translations \
SET uploaded=? WHERE url=?', (1, url))
self.conn.commit()
if r.json().get('id'):
c.execute('UPDATE recent_translations \
SET uploaded=?, video_id=? WHERE url=?', (1, r.json()['id'], url))
self.conn.commit()
def prepare_video_for_shikimori(self):
print('prepare_video_for_shikimori')
c = self.conn.cursor()
db = []
c.execute("SELECT * FROM recent_translations \
WHERE (uploaded LIKE {upl} AND broken LIKE {broken})".format(upl=0, broken=0))
for x in c.fetchall():
db.append(x)
for x in db:
time.sleep(0.7)
self._post_video_to_shikimori(x[1], x[4], x[6], x[2], x[3], x[5], x[5], x[9])
def delete_video_from_shikimori(self):
print('delete_video_from_shikimori')
c = self.conn.cursor()
c.execute("SELECT anime_id, video_id FROM delete_broken WHERE deleted=0")
db = c.fetchall()
if len(db) > 0:
for x in db:
r = requests.delete('http://shikimori.org/api/animes/{anime_id}/anime_videos/{video_id}'
.format(anime_id=x[0], video_id=x[1]), headers=self.HEADERS)
print(r.text)
print(x)
c.execute('UPDATE delete_broken SET deleted = 1 WHERE video_id=?', (x[1],))
self.conn.commit()
time.sleep(0.7)
def run(self):
while True:
self.get_translations()
self.prepare_video_for_shikimori()
self.check_changes()
self.delete_video_from_shikimori()
self.post_op_ed_to_shikimori()
print('ok')
print(datetime.datetime.now())
print('_______________________')
if __name__ == '__main__':
bot = Bot()
bot.run()