-
Notifications
You must be signed in to change notification settings - Fork 5
/
anilist.py
93 lines (88 loc) · 2.59 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
"""
Anilist Search Plugin for Userbot
Usage : .anilist animeName
By :- @Zero_cool7870
"""
import requests
import re
import json
import asyncio
from ULTRA import CMD_HELP
from ULTRA.utils import admin_cmd, edit_or_reply, sudo_cmd
async def callAPI(search_str):
query = '''
query ($id: Int,$search: String) {
Media (id: $id, type: ANIME,search: $search) {
id
title {
romaji
english
}
description (asHtml: false)
startDate{
year
}
episodes
chapters
volumes
season
type
format
status
duration
averageScore
genres
bannerImage
}
}
'''
variables = {
'search' : search_str
}
url = 'https://graphql.anilist.co'
response = requests.post(url, json={'query': query, 'variables': variables})
return response.text
async def formatJSON(outData):
msg = ""
jsonData = json.loads(outData)
res = list(jsonData.keys())
if "errors" in res:
msg += f"**Error** : `{jsonData['errors'][0]['message']}`"
return msg
else:
jsonData = jsonData['data']['Media']
if "bannerImage" in jsonData.keys():
msg += f"[〽️]({jsonData['bannerImage']})"
else :
msg += "〽️"
title = jsonData['title']['romaji']
link = f"https://anilist.co/anime/{jsonData['id']}"
msg += f"[{title}]({link})"
msg += f"\n\n**Type** : {jsonData['format']}"
msg += f"\n**Genres** : "
for g in jsonData['genres']:
msg += g+" "
msg += f"\n**Status** : {jsonData['status']}"
msg += f"\n**Episode** : {jsonData['episodes']}"
msg += f"\n**Year** : {jsonData['startDate']['year']}"
msg += f"\n**Score** : {jsonData['averageScore']}"
msg += f"\n**Duration** : {jsonData['duration']} min\n\n"
#https://t.me/catULTRA_support/19496
cat = f"{jsonData['description']}"
msg += " __" + re.sub("<br>", '\n', cat) +"__"
return msg
@bot.on(admin_cmd(pattern="anilist (.*)"))
@bot.on(sudo_cmd(pattern="anilist (.*)", allow_sudo=True))
async def anilist(event):
if event.fwd_from:
return
input_str = event.pattern_match.group(1)
event = await edit_or_reply(event, "Searching...")
result = await callAPI(input_str)
msg = await formatJSON(result)
await event.edit(msg, link_preview=True)
CMD_HELP.update({
"anilist":
".anilist <anime name >\
\nUSAGE: Shows you the details of the anime."
})