From 6e1731f1df856a624ede2a662f8a06f86ab589ed Mon Sep 17 00:00:00 2001 From: blank X Date: Tue, 18 May 2021 14:45:39 +0700 Subject: [PATCH] Update whatanime to the new api --- sukuinote/plugins/whatanime.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/sukuinote/plugins/whatanime.py b/sukuinote/plugins/whatanime.py index 6774a91..a80132b 100644 --- a/sukuinote/plugins/whatanime.py +++ b/sukuinote/plugins/whatanime.py @@ -33,29 +33,26 @@ async def whatanime(client, message): await proc.communicate() await reply.edit_text('Uploading...') 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) - if isinstance(json, str): - await reply.edit_text(json, parse_mode=None) + if json.get('error'): + await reply.edit_text(json['error'], parse_mode=None) else: try: - match = json['docs'][0] + match = json['result'][0] except IndexError: await reply.edit_text('No match') else: - nsfw = match['is_adult'] - title_native = match['title_native'] - title_english = match['title_english'] - title_romaji = match['title_romaji'] - synonyms = ', '.join(match['synonyms']) - filename = match['filename'] - tokenthumb = match['tokenthumb'] - anilist_id = match['anilist_id'] + nsfw = match['anilist']['isAdult'] + title_native = match['anilist']['title']['native'] + title_english = match['anilist']['title']['english'] + title_romaji = match['anilist']['title']['romaji'] + synonyms = ', '.join(match['anilist']['synonyms']) + anilist_id = match['anilist']['id'] episode = match['episode'] similarity = match['similarity'] 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') - at_time = match['at'] text = f'{title_romaji}' if title_english: text += f' ({title_english})' @@ -69,11 +66,10 @@ async def whatanime(client, message): if nsfw: text += 'Hentai/NSFW: Yes' 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: - async with session.get(url) as resp: + async with session.get(match['video']) as resp: while True: - chunk = await resp.content.read(10) + chunk = await resp.content.read(4096) if not chunk: break file.write(chunk)