forked from naoTimesdev/naoTimes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanilist.py
619 lines (577 loc) · 26.4 KB
/
anilist.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import discord
import discord.ext.commands as commands
import asyncio
import time
import aiohttp
from datetime import datetime, timedelta
def setup(bot):
bot.add_cog(Anilist(bot))
def monthintext(number):
idn = ["Januari", "Februari", "Maret", "April",
"Mei", "Juni", "Juli", "Agustus",
"September", "Oktober", "November", "Desember"]
if number is None:
return "Unknown"
x = number - 1
if x < 0:
return "Unknown"
return idn[number - 1]
anilist_query = '''
query ($page: Int, $perPage: Int, $search: String) {
Page (page: $page, perPage: $perPage) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media(search: $search, type: %s) {
id
title {
romaji
english
}
coverImage {
large
}
averageScore
volumes
episodes
status
genres
description
startDate {
year
month
day
}
endDate {
year
month
day
}
nextAiringEpisode {
airingAt
timeUntilAiring
episode
}
}
}
}
'''
def create_time_format(secs):
months = int(secs // 2592000) # 30 days format
secs -= months * 2592000
days = int(secs // 86400)
secs -= days * 86400
hours = int(secs // 3600)
secs -= hours * 3600
minutes = int(secs // 60)
secs -= minutes * 60
return_text = ''
if months != 0:
return_text += '{} bulan '.format(months)
return return_text + '{} hari {} jam {} menit {} detik lagi'.format(days, hours, minutes, secs)
async def alistAnimu(title, num=1):
num = num - 1
variables = {
'search': title,
'page': 1,
'perPage': 50
}
api_link = 'https://graphql.anilist.co'
async with aiohttp.ClientSession() as session:
try:
async with session.post(api_link, json={'query': anilist_query % ('ANIME'), 'variables': variables}) as r:
try:
data = await r.json()
except IndexError:
return "ERROR: Terjadi kesalahan internal"
if r.status != 200:
if r.status == 404:
return "ERROR: Tidak dapat menemukan anime tersebut"
elif r.status == 500:
return "ERROR: Internal Error :/"
try:
entry = data['data']['Page']['media'][num]
except IndexError:
return "ERROR: Tidak ada hasil."
except session.ClientError:
return "ERROR: Koneksi terputus"
dataLen = data['data']['Page']['pageInfo']['total']
sY = entry['startDate']['year']
if sY is None:
start = 'Belum Rilis'
else:
start = '{}'.format(str(sY))
sM = entry['startDate']['month']
if sM is None:
start = start
else:
start = '{}/{}'.format(start,str(sM))
sD = entry['startDate']['day']
if sD is None:
start = start
else:
start = '{}/{}'.format(start,str(sD))
eY = entry['endDate']['year']
if eY is None:
end = 'Belum Selesai'
else:
end = '{}'.format(str(eY))
eM = entry['endDate']['month']
if eM is None:
end = end
else:
end = '{}/{}'.format(end,str(eM))
eD = entry['endDate']['day']
if eD is None:
end = end
else:
end = '{}/{}'.format(end,str(eD))
title = entry['title']['romaji']
ani_id = str(entry['id'])
epTotal = str(entry["episodes"])
rate = entry['averageScore']
if rate is None:
rate = 'None'
else:
rate = int(rate)/10
status = str(entry["status"]).lower().capitalize()
synop = str(entry['description'])
synop = synop.replace("<br>", ' ')
genre = entry['genres']
genres = ', '.join(genre).lower()
img = str(entry['coverImage']['large'])
aniID = str(entry['id'])
aniLink = 'https://anilist.co/anime/{}'.format(aniID)
print('GOT EVERYSHIT UWU')
if status == 'Releasing' or status == "Not_yet_released":
nextEp = entry['nextAiringEpisode']
if not nextEp:
return (title, epTotal, status.replace('_', ' '), rate, start, end, img, synop, "ID: {} | {}".format(ani_id, genres), aniLink, dataLen)
airingDate = nextEp['airingAt']
deltaAirDate = timedelta(seconds=int(airingDate))
timeTuple = datetime(1,1,1) + deltaAirDate
timeremain = nextEp['timeUntilAiring']
nextUp = nextEp['episode']
consYear = time.strftime('%Y')
airingDate = f'{timeTuple.day} {monthintext(timeTuple.month)} {consYear}'
remainTime = create_time_format(timeremain)
return (title, epTotal, status.replace('_', ' '), rate, start, end, img, synop, "ID: {} | {}".format(ani_id, genres), aniLink, dataLen, nextUp, airingDate, remainTime)
else:
return (title, epTotal, status.replace('_', ' '), rate, start, end, img, synop, "ID: {} | {}".format(ani_id, genres), aniLink, dataLen)
async def alistMango(title, num=1):
num = num - 1
variables = {
'search': title,
'page': 1,
'perPage': 50
}
api_link = 'https://graphql.anilist.co'
async with aiohttp.ClientSession() as session:
try:
async with session.post(api_link, json={'query': anilist_query % ('MANGA'), 'variables': variables}) as r:
try:
data = await r.json()
except IndexError:
return "ERROR: Terjadi kesalahan internal"
if r.status != 200:
if r.status == 404:
return "ERROR: Tidak dapat menemukan anime tersebut"
elif r.status == 500:
return "ERROR: Internal Error :/"
try:
entry = data['data']['Page']['media'][num]
except IndexError:
return "ERROR: Tidak ada hasil."
except session.ClientError:
return "ERROR: Koneksi terputus"
dataLen = len(data['data']['Page']['media'])
sY = entry['startDate']['year']
if sY is None:
start = 'Belum Rilis'
else:
start = '{}'.format(str(sY))
sM = entry['startDate']['month']
if sM is None:
start = start
else:
start = '{}/{}'.format(start,str(sM))
sD = entry['startDate']['day']
if sD is None:
start = start
else:
start = '{}/{}'.format(start,str(sD))
eY = entry['endDate']['year']
if eY is None:
end = 'Belum Selesai'
else:
end = '{}'.format(str(eY))
eM = entry['endDate']['month']
if eM is None:
end = end
else:
end = '{}/{}'.format(end,str(eM))
eD = entry['endDate']['day']
if eD is None:
end = end
else:
end = '{}/{}'.format(end,str(eD))
title = entry['title']['romaji']
volumes = entry['volumes']
rate = entry['averageScore']
if rate is None:
rate = 'None'
else:
rate = int(rate)/10
status = str(entry["status"]).lower().capitalize()
synop = str(entry['description'])
genre = entry['genres']
genres = ', '.join(genre).lower()
img = str(entry['coverImage']['large'])
ani_id = str(entry['id'])
aniLink = 'https://anilist.co/manga/{}'.format(ani_id)
finalResult = {"title": title, 'status': status, 'volume': volumes,'score': rate, 'startDate': start, 'endDate': end, 'posterImg': img, 'synopsis': synop, 'genre': "ID: {} | {}".format(ani_id, genres), 'link': aniLink, 'dataLength': dataLen}
return finalResult
class Anilist:
def __init__(self, bot):
self.bot = bot
async def __error(self, ctx, error):
if not isinstance(error, commands.UserInputError):
raise error
try:
await ctx.send(error)
except discord.Forbidden:
pass
@commands.command(pass_context=True, aliases=['animu', 'kartun', 'ani'])
async def anime(self, ctx, *, judulAnime):
"""Search anime info via anilist.co."""
init = await alistAnimu(judulAnime, 1)
if type(init) is str:
await self.bot.say(init)
return 'No result'
else:
pass
maxPage = init[10]
firstRun = True
time_table = False
num = 1
while True:
#(title, epTotal, status, rate, start, end, img, synop, genres, aniLink, nextUp, airingDate, remainTime, dataLen)
if firstRun:
firstRun = False
num = 1
find = await alistAnimu(judulAnime, num)
title = '[{}]({})'.format(find[0], find[9])
embed=discord.Embed(title="Anime Info", color=0x81e28d)
embed.set_image(url=find[6])
embed.add_field(name='Judul', value=title, inline=False)
embed.add_field(name='Episode', value=find[1], inline=True)
embed.add_field(name='Status', value=find[2], inline=True)
embed.add_field(name='Skor', value=find[3], inline=True)
embed.add_field(name='Tanggal Mulai', value=find[4], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find[5], inline=True)
embed.set_footer(text=find[8])
try:
embed.add_field(name='Deskripsi', value=find[7], inline=False)
two_part = False
msg = await self.bot.say(embed=embed)
except discord.errors.HTTPException:
fmtSyn = '```{}```'.format(str(find[7]))
embed.remove_field(-1)
two_part = True
msg = await self.bot.say(embed=embed)
msg2 = await self.bot.say(fmtSyn)
if str(find[2]) == 'Releasing' and time_table == True and len(find) != 11 or str(find[2]) == 'Not yet released' and time_table == True and len(find) != 11:
toReact = ['👍', '✅']
elif maxPage == 1 and num == 1:
if str(find[2]) == 'Releasing' and time_table == False and len(find) != 11 or str(find[2]) == 'Not yet released' and time_table == False and len(find) != 11:
toReact = ['⏳', '✅']
else:
toReact = ['✅']
elif num == 1:
if str(find[2]) == 'Releasing' and time_table == False and len(find) != 11 or str(find[2]) == 'Not yet released' and time_table == False and len(find) != 11:
toReact = ['⏩', '⏳', '✅']
else:
toReact = ['⏩', '✅']
elif num == maxPage:
if str(find[2]) == 'Releasing' and time_table == False and len(find) != 11 and time_table == False and len(find) != 11 or str(find[2]) == 'Not yet released' and time_table == False and len(find) != 11:
toReact = ['⏪', '⏳', '✅']
else:
toReact = ['⏪', '✅']
elif num > 1 and num < maxPage:
if str(find[2]) == 'Releasing' and time_table == False and len(find) != 11 or str(find[2]) == 'Not yet released' and time_table == False and len(find) != 11:
toReact = ['⏪', '⏩', '⏳', '✅']
else:
toReact = ['⏪', '⏩', '✅']
for reaction in toReact:
if two_part:
await self.bot.add_reaction(msg2, reaction)
else:
await self.bot.add_reaction(msg, reaction)
#feel free to change ✅ to 🆗 or the opposite
def checkReaction(reaction, user):
e = str(reaction.emoji)
return e.startswith(('⏪', '⏩', '✅', '⏳', '👍'))
if two_part:
res = await self.bot.wait_for_reaction(message=msg2, user=ctx.message.author, timeout=30, check=checkReaction)
else:
res = await self.bot.wait_for_reaction(message=msg, user=ctx.message.author, timeout=30, check=checkReaction)
if res is None:
if two_part:
await self.bot.clear_reactions(msg2)
else:
await self.bot.clear_reactions(msg)
return
elif '⏪' in str(res.reaction.emoji):
num = num - 1
time_table = False
find = await alistAnimu(judulAnime, num)
title = '[{}]({})'.format(find[0], find[9])
embed=discord.Embed(title="Anime Info", color=0x81e28d)
embed.set_image(url=find[6])
embed.add_field(name='Judul', value=title, inline=False)
embed.add_field(name='Episode', value=find[1], inline=True)
embed.add_field(name='Status', value=find[2], inline=True)
embed.add_field(name='Skor', value=find[3], inline=True)
embed.add_field(name='Tanggal Mulai', value=find[4], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find[5], inline=True)
embed.set_footer(text=find[8])
fmtSyn = '```{}```'.format(str(find[7]))
try:
embed.add_field(name='Deskripsi', value=find[7], inline=False)
if two_part:
await self.bot.delete_message(msg2)
else:
await self.bot.clear_reactions(msg)
two_part = False
msg = await self.bot.edit_message(msg, embed=embed)
except discord.errors.HTTPException:
fmtSyn = '```{}```'.format(str(find[7]))
embed.remove_field(-1)
if two_part:
await self.bot.clear_reactions(msg2)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.edit_message(msg2, fmtSyn)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.say(fmtSyn)
two_part = True
elif '⏩' in str(res.reaction.emoji):
num = num + 1
time_table = False
find = await alistAnimu(judulAnime, num)
title = '[{}]({})'.format(find[0], find[9])
embed=discord.Embed(title="Anime Info", color=0x81e28d)
embed.set_image(url=find[6])
embed.add_field(name='Judul', value=title, inline=False)
embed.add_field(name='Episode', value=find[1], inline=True)
embed.add_field(name='Status', value=find[2], inline=True)
embed.add_field(name='Skor', value=find[3], inline=True)
embed.add_field(name='Tanggal Mulai', value=find[4], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find[5], inline=True)
embed.set_footer(text=find[8])
try:
embed.add_field(name='Deskripsi', value=find[7], inline=False)
if two_part:
await self.bot.delete_message(msg2)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
two_part = False
except discord.errors.HTTPException:
fmtSyn = '```{}```'.format(str(find[7]))
embed.remove_field(-1)
if two_part:
await self.bot.clear_reactions(msg2)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.edit_message(msg2, fmtSyn)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.say(fmtSyn)
two_part = True
elif '👍' in str(res.reaction.emoji):
time_table = False
title = '[{}]({})'.format(find[0], find[9])
embed=discord.Embed(title="Anime Info", color=0x81e28d)
embed.set_image(url=find[6])
embed.add_field(name='Judul', value=title, inline=False)
embed.add_field(name='Episode', value=find[1], inline=True)
embed.add_field(name='Status', value=find[2], inline=True)
embed.add_field(name='Skor', value=find[3], inline=True)
embed.add_field(name='Tanggal Mulai', value=find[4], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find[5], inline=True)
embed.set_footer(text=find[8])
try:
embed.add_field(name='Deskripsi', value=find[7], inline=False)
if two_part:
await self.bot.delete_message(msg2)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
two_part = False
except discord.errors.HTTPException:
fmtSyn = '```{}```'.format(str(find[7]))
embed.remove_field(-1)
if two_part:
await self.bot.clear_reactions(msg2)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.edit_message(msg2, fmtSyn)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.say(fmtSyn)
two_part = True
elif '⏳' in str(res.reaction.emoji):
time_table = True
find = await alistAnimu(judulAnime, num)
epiTxt = 'Episode ' + str(find[11])
fmtFooter = f'Akan tayang pada {find[12]}'
embed=discord.Embed(title=find[0], color=0x81e28d)
embed.add_field(name=epiTxt, value=str(find[13]), inline=False)
embed.set_footer(text=fmtFooter)
if two_part:
await self.bot.delete_message(msg2)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
two_part = False
elif '✅' in str(res.reaction.emoji):
await self.bot.delete_message(ctx.message)
if two_part:
await self.bot.delete_message(msg2)
await self.bot.delete_message(msg)
break
@commands.command(pass_context=True, aliases=['komik', 'mango'])
async def manga(self, ctx, *, title):
"""Search manga info via anilist.co."""
init = await alistMango(title, 1)
if type(init) is str:
await self.bot.say(init)
return 'No result'
else:
pass
maxPage = int(init['dataLength'])
firstRun = True
while True:
if firstRun:
firstRun = False
num = 1
find = await alistMango(title, num)
embed=discord.Embed(title="Anime Info", url=find['link'], color=0x81e28d)
embed.set_image(url=find['posterImg'])
embed.add_field(name='Judul', value=find['title'], inline=False)
embed.add_field(name='Volume', value=find['volume'], inline=True)
embed.add_field(name='Status', value=find['status'], inline=True)
embed.add_field(name='Skor', value=find['score'], inline=True)
embed.add_field(name='Tanggal Mulai', value=find['startDate'], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find['endDate'], inline=True)
embed.set_footer(text=find['genre'])
try:
embed.add_field(name='Deskripsi', value=find['synopsis'], inline=False)
two_part = False
msg = await self.bot.say(embed=embed)
except:
fmtSyn = '```{}```'.format(find['synopsis'])
two_part = True
msg = await self.bot.say(embed=embed)
msg2 = await self.bot.say(fmtSyn)
if maxPage == 1 and num == 1:
toReact = ['✅']
elif num == 1:
toReact = ['⏩', '✅']
elif num == maxPage:
toReact = ['⏪', '✅']
elif num > 1 and num < maxPage:
toReact = ['⏪', '⏩', '✅']
for reaction in toReact:
if two_part:
await self.bot.add_reaction(msg2, reaction)
else:
await self.bot.add_reaction(msg, reaction)
#feel free to change ✅ to 🆗 or the opposite
def checkReaction(reaction, user):
e = str(reaction.emoji)
return e.startswith(('⏪', '⏩', '✅'))
if two_part:
res = await self.bot.wait_for_reaction(message=msg2, user=ctx.message.author, timeout=30, check=checkReaction)
else:
res = await self.bot.wait_for_reaction(message=msg, user=ctx.message.author, timeout=30, check=checkReaction)
if res is None:
break
elif '⏪' in str(res.reaction.emoji):
num = num - 1
find = await alistMango(title, num)
embed=discord.Embed(title="Anime Info", url=find['link'], color=0x81e28d)
embed.set_image(url=find['posterImg'])
embed.add_field(name='Judul', value=find['title'], inline=False)
embed.add_field(name='Volume', value=find['volume'], inline=True)
embed.add_field(name='Status', value=find['status'], inline=True)
embed.add_field(name='Skor', value=find['score'], inline=True)
embed.add_field(name='Tanggal Mulai', value=find['startDate'], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find['endDate'], inline=True)
embed.set_footer(text=find['genre'])
try:
embed.add_field(name='Deskripsi', value=find['synopsis'], inline=False)
if two_part:
await self.bot.delete_message(msg2)
await self.bot.clear_reactions(msg2)
else:
await self.bot.clear_reactions(msg)
two_part = False
msg = await self.bot.edit_message(msg, embed=embed)
except:
fmtSyn = '```{}```'.format(find['synopsis'])
if two_part:
await self.bot.clear_reactions(msg2)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.edit_message(msg2, fmtSyn)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.say(fmtSyn)
two_part = True
elif '⏩' in str(res.reaction.emoji):
num = num + 1
find = await alistMango(title, num)
embed=discord.Embed(title="Anime Info", url=find['link'], color=0x81e28d)
embed.set_image(url=find['posterImg'])
embed.add_field(name='Judul', value=find['title'], inline=False)
embed.add_field(name='Volume', value=find['volume'], inline=True)
embed.add_field(name='Status', value=find['status'], inline=True)
embed.add_field(name='Skor', value=find['score'], inline=True)
embed.add_field(name='Tanggal Mulai', value=find['startDate'], inline=True)
embed.add_field(name='Tanggal Berakhir', value=find['endDate'], inline=True)
embed.set_footer(text=find['genre'])
try:
embed.add_field(name='Deskripsi', value=find['synopsis'], inline=False)
if two_part:
await self.bot.delete_message(msg2)
await self.bot.clear_reactions(msg2)
else:
await self.bot.clear_reactions(msg)
two_part = False
msg = await self.bot.edit_message(msg, embed=embed)
except:
fmtSyn = '```{}```'.format(find['synopsis'])
if two_part:
await self.bot.clear_reactions(msg2)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.edit_message(msg2, fmtSyn)
else:
await self.bot.clear_reactions(msg)
msg = await self.bot.edit_message(msg, embed=embed)
msg2 = await self.bot.say(fmtSyn)
two_part = True
elif '✅' in str(res.reaction.emoji):
await self.bot.delete_message(ctx.message)
if two_part:
await self.bot.delete_message(msg2)
await self.bot.delete_message(msg)
break