Update whatanime to the new api

This commit is contained in:
blank X 2021-05-18 14:45:39 +07:00
parent 54b9f96be3
commit 6e1731f1df
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 12 additions and 16 deletions

View File

@ -33,29 +33,26 @@ async def whatanime(client, message):
await proc.communicate() await proc.communicate()
await reply.edit_text('Uploading...') await reply.edit_text('Uploading...')
with open(new_path, 'rb') as file: with open(new_path, 'rb') as file:
async with session.post('https://trace.moe/api/search', data={'image': file}) as resp: async with session.post('https://api.trace.moe/search?cutBorders&anilistInfo', data={'image': file}) as resp:
json = await resp.json(content_type=None) json = await resp.json(content_type=None)
if isinstance(json, str): if json.get('error'):
await reply.edit_text(json, parse_mode=None) await reply.edit_text(json['error'], parse_mode=None)
else: else:
try: try:
match = json['docs'][0] match = json['result'][0]
except IndexError: except IndexError:
await reply.edit_text('No match') await reply.edit_text('No match')
else: else:
nsfw = match['is_adult'] nsfw = match['anilist']['isAdult']
title_native = match['title_native'] title_native = match['anilist']['title']['native']
title_english = match['title_english'] title_english = match['anilist']['title']['english']
title_romaji = match['title_romaji'] title_romaji = match['anilist']['title']['romaji']
synonyms = ', '.join(match['synonyms']) synonyms = ', '.join(match['anilist']['synonyms'])
filename = match['filename'] anilist_id = match['anilist']['id']
tokenthumb = match['tokenthumb']
anilist_id = match['anilist_id']
episode = match['episode'] episode = match['episode']
similarity = match['similarity'] similarity = match['similarity']
from_time = str(datetime.timedelta(seconds=match['from'])).split('.', 1)[0].rjust(8, '0') from_time = str(datetime.timedelta(seconds=match['from'])).split('.', 1)[0].rjust(8, '0')
to_time = str(datetime.timedelta(seconds=match['to'])).split('.', 1)[0].rjust(8, '0') to_time = str(datetime.timedelta(seconds=match['to'])).split('.', 1)[0].rjust(8, '0')
at_time = match['at']
text = f'<a href="https://anilist.co/anime/{anilist_id}">{title_romaji}</a>' text = f'<a href="https://anilist.co/anime/{anilist_id}">{title_romaji}</a>'
if title_english: if title_english:
text += f' ({title_english})' text += f' ({title_english})'
@ -69,11 +66,10 @@ async def whatanime(client, message):
if nsfw: if nsfw:
text += '<b>Hentai/NSFW:</b> Yes' text += '<b>Hentai/NSFW:</b> Yes'
async def _send_preview(): async def _send_preview():
url = f'https://media.trace.moe/video/{anilist_id}/{urlencode(filename)}?t={at_time}&token={tokenthumb}'
with tempfile.NamedTemporaryFile() as file: with tempfile.NamedTemporaryFile() as file:
async with session.get(url) as resp: async with session.get(match['video']) as resp:
while True: while True:
chunk = await resp.content.read(10) chunk = await resp.content.read(4096)
if not chunk: if not chunk:
break break
file.write(chunk) file.write(chunk)