From 7daa29a2e2fee6c8e16638205ad2948fcef1a1cb Mon Sep 17 00:00:00 2001 From: blank X Date: Tue, 24 Nov 2020 15:17:50 +0700 Subject: [PATCH] Don't throw an exception when it cannot connect to nekos.life --- sukuinote/plugins/nekos.py | 80 +++++++++++++++++--------------- sukuinote/slave-plugins/nekos.py | 59 ++++++++++++----------- 2 files changed, 74 insertions(+), 65 deletions(-) diff --git a/sukuinote/plugins/nekos.py b/sukuinote/plugins/nekos.py index 758d6b8..053e260 100644 --- a/sukuinote/plugins/nekos.py +++ b/sukuinote/plugins/nekos.py @@ -1,4 +1,5 @@ import os +import logging import requests from pyrogram import Client, filters from pyrogram.types.messages_and_media import Photo, Animation @@ -7,41 +8,44 @@ from .. import config, help_dict, log_errors, session, slave, public_log_errors help_text = '' -resp = requests.get('https://nekos.life/api/v2/endpoints') -json = resp.json() -for i in json: - _, i = i.split(' ', 1) - i = i.strip() - if i.startswith('/api/v2/img/<\''): - for i in os.path.basename(i)[1:-1].split(', '): - i = i[1:-1] - if 'v3' in i: - continue - def _generate(i): - @Client.on_message(~filters.sticker & ~filters.via_bot & ~filters.edited & filters.me & filters.command(i, prefixes=config['config']['prefixes'])) - @log_errors - @public_log_errors - async def func(client, message): - bot = await slave.get_me() - results = await client.get_inline_bot_results(bot.username or bot.id, i) - result = results.results[0] - to_reply = message - if not getattr(message.reply_to_message, 'empty', True): - to_reply = message.reply_to_message - if result.type == 'photo': - file = Photo._parse(client, result.photo) - else: - file = Animation._parse(client, result.document, result.document.attributes, 'hello.mp4') - try: - await to_reply.reply_cached_media(file.file_id, file.file_ref, caption=result.send_message.message, parse_mode=None) - except Forbidden: - await to_reply.reply_text(result.send_message.message, parse_mode=None) - return func - func = _generate(i) - globals()[i] = func - locals()[i] = func - func = None - help_text += '{prefix}' + i.lower() + f' - Gets a {"gif" if "gif" in i else "picture"} of {i.lower()}\n' - break - -help_dict['nekos'] = ('Nekos.life', help_text + '\nCan also be activated inline with: @{bot} <command without dot>') +try: + resp = requests.get('https://nekos.life/api/v2/endpoints') + json = resp.json() +except Exception: + logging.exception('Cannot connect to nekos.life') +else: + for i in json: + _, i = i.split(' ', 1) + i = i.strip() + if i.startswith('/api/v2/img/<\''): + for i in os.path.basename(i)[1:-1].split(', '): + i = i[1:-1] + if 'v3' in i: + continue + def _generate(i): + @Client.on_message(~filters.sticker & ~filters.via_bot & ~filters.edited & filters.me & filters.command(i, prefixes=config['config']['prefixes'])) + @log_errors + @public_log_errors + async def func(client, message): + bot = await slave.get_me() + results = await client.get_inline_bot_results(bot.username or bot.id, i) + result = results.results[0] + to_reply = message + if not getattr(message.reply_to_message, 'empty', True): + to_reply = message.reply_to_message + if result.type == 'photo': + file = Photo._parse(client, result.photo) + else: + file = Animation._parse(client, result.document, result.document.attributes, 'hello.mp4') + try: + await to_reply.reply_cached_media(file.file_id, file.file_ref, caption=result.send_message.message, parse_mode=None) + except Forbidden: + await to_reply.reply_text(result.send_message.message, parse_mode=None) + return func + func = _generate(i) + globals()[i] = func + locals()[i] = func + func = None + help_text += '{prefix}' + i.lower() + f' - Gets a {"gif" if "gif" in i else "picture"} of {i.lower()}\n' + break + help_dict['nekos'] = ('Nekos.life', help_text + '\nCan also be activated inline with: @{bot} <command without dot>') diff --git a/sukuinote/slave-plugins/nekos.py b/sukuinote/slave-plugins/nekos.py index c2ce723..cb6a424 100644 --- a/sukuinote/slave-plugins/nekos.py +++ b/sukuinote/slave-plugins/nekos.py @@ -1,33 +1,38 @@ import os +import logging import requests from pyrogram import Client, filters from pyrogram.types import InputTextMessageContent, InlineQueryResultArticle, InlineQueryResultPhoto, InlineQueryResultAnimation from .. import log_errors, session, app_user_ids -resp = requests.get('https://nekos.life/api/v2/endpoints') -json = resp.json() -for i in json: - _, i = i.split(' ', 1) - i = i.strip() - if i.startswith('/api/v2/img/<\''): - for i in os.path.basename(i)[1:-1].split(', '): - i = i[1:-1] - if 'v3' in i: - continue - def _generate(i): - @Client.on_inline_query(filters.regex(f'^{i}$')) - @log_errors - async def func(client, inline_query): - if inline_query.from_user.id not in app_user_ids: - await inline_query.answer([InlineQueryResultArticle('...no', InputTextMessageContent('...no'))], cache_time=3600, is_personal=True) - return - async with session.get(f'https://nekos.life/api/v2/img/{i}') as resp: - url = (await resp.json())['url'] - call = InlineQueryResultAnimation if '.gif' == os.path.splitext(url)[1] else InlineQueryResultPhoto - await inline_query.answer([call(url, caption=url, parse_mode=None)], cache_time=0) - return func - func = _generate(i) - globals()[i] = func - locals()[i] = func - func = None - break +try: + resp = requests.get('https://nekos.life/api/v2/endpoints') + json = resp.json() +except Exception: + logging.exception('Cannot connect to nekos.life') +else: + for i in json: + _, i = i.split(' ', 1) + i = i.strip() + if i.startswith('/api/v2/img/<\''): + for i in os.path.basename(i)[1:-1].split(', '): + i = i[1:-1] + if 'v3' in i: + continue + def _generate(i): + @Client.on_inline_query(filters.regex(f'^{i}$')) + @log_errors + async def func(client, inline_query): + if inline_query.from_user.id not in app_user_ids: + await inline_query.answer([InlineQueryResultArticle('...no', InputTextMessageContent('...no'))], cache_time=3600, is_personal=True) + return + async with session.get(f'https://nekos.life/api/v2/img/{i}') as resp: + url = (await resp.json())['url'] + call = InlineQueryResultAnimation if '.gif' == os.path.splitext(url)[1] else InlineQueryResultPhoto + await inline_query.answer([call(url, caption=url, parse_mode=None)], cache_time=0) + return func + func = _generate(i) + globals()[i] = func + locals()[i] = func + func = None + break