Don't throw an exception when it cannot connect to nekos.life

This commit is contained in:
blank X 2020-11-24 15:17:50 +07:00
parent 81b3d012b8
commit 7daa29a2e2
2 changed files with 74 additions and 65 deletions

View File

@ -1,4 +1,5 @@
import os import os
import logging
import requests import requests
from pyrogram import Client, filters from pyrogram import Client, filters
from pyrogram.types.messages_and_media import Photo, Animation 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 = '' help_text = ''
resp = requests.get('https://nekos.life/api/v2/endpoints') try:
json = resp.json() resp = requests.get('https://nekos.life/api/v2/endpoints')
for i in json: json = resp.json()
_, i = i.split(' ', 1) except Exception:
i = i.strip() logging.exception('Cannot connect to nekos.life')
if i.startswith('/api/v2/img/<\''): else:
for i in os.path.basename(i)[1:-1].split(', '): for i in json:
i = i[1:-1] _, i = i.split(' ', 1)
if 'v3' in i: i = i.strip()
continue if i.startswith('/api/v2/img/<\''):
def _generate(i): for i in os.path.basename(i)[1:-1].split(', '):
@Client.on_message(~filters.sticker & ~filters.via_bot & ~filters.edited & filters.me & filters.command(i, prefixes=config['config']['prefixes'])) i = i[1:-1]
@log_errors if 'v3' in i:
@public_log_errors continue
async def func(client, message): def _generate(i):
bot = await slave.get_me() @Client.on_message(~filters.sticker & ~filters.via_bot & ~filters.edited & filters.me & filters.command(i, prefixes=config['config']['prefixes']))
results = await client.get_inline_bot_results(bot.username or bot.id, i) @log_errors
result = results.results[0] @public_log_errors
to_reply = message async def func(client, message):
if not getattr(message.reply_to_message, 'empty', True): bot = await slave.get_me()
to_reply = message.reply_to_message results = await client.get_inline_bot_results(bot.username or bot.id, i)
if result.type == 'photo': result = results.results[0]
file = Photo._parse(client, result.photo) to_reply = message
else: if not getattr(message.reply_to_message, 'empty', True):
file = Animation._parse(client, result.document, result.document.attributes, 'hello.mp4') to_reply = message.reply_to_message
try: if result.type == 'photo':
await to_reply.reply_cached_media(file.file_id, file.file_ref, caption=result.send_message.message, parse_mode=None) file = Photo._parse(client, result.photo)
except Forbidden: else:
await to_reply.reply_text(result.send_message.message, parse_mode=None) file = Animation._parse(client, result.document, result.document.attributes, 'hello.mp4')
return func try:
func = _generate(i) await to_reply.reply_cached_media(file.file_id, file.file_ref, caption=result.send_message.message, parse_mode=None)
globals()[i] = func except Forbidden:
locals()[i] = func await to_reply.reply_text(result.send_message.message, parse_mode=None)
func = None return func
help_text += '{prefix}' + i.lower() + f' - Gets a {"gif" if "gif" in i else "picture"} of {i.lower()}\n' func = _generate(i)
break globals()[i] = func
locals()[i] = func
help_dict['nekos'] = ('Nekos.life', help_text + '\nCan also be activated inline with: @{bot} <i>&lt;command without dot&gt;</i>') 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} <i>&lt;command without dot&gt;</i>')

View File

@ -1,33 +1,38 @@
import os import os
import logging
import requests import requests
from pyrogram import Client, filters from pyrogram import Client, filters
from pyrogram.types import InputTextMessageContent, InlineQueryResultArticle, InlineQueryResultPhoto, InlineQueryResultAnimation from pyrogram.types import InputTextMessageContent, InlineQueryResultArticle, InlineQueryResultPhoto, InlineQueryResultAnimation
from .. import log_errors, session, app_user_ids from .. import log_errors, session, app_user_ids
resp = requests.get('https://nekos.life/api/v2/endpoints') try:
json = resp.json() resp = requests.get('https://nekos.life/api/v2/endpoints')
for i in json: json = resp.json()
_, i = i.split(' ', 1) except Exception:
i = i.strip() logging.exception('Cannot connect to nekos.life')
if i.startswith('/api/v2/img/<\''): else:
for i in os.path.basename(i)[1:-1].split(', '): for i in json:
i = i[1:-1] _, i = i.split(' ', 1)
if 'v3' in i: i = i.strip()
continue if i.startswith('/api/v2/img/<\''):
def _generate(i): for i in os.path.basename(i)[1:-1].split(', '):
@Client.on_inline_query(filters.regex(f'^{i}$')) i = i[1:-1]
@log_errors if 'v3' in i:
async def func(client, inline_query): continue
if inline_query.from_user.id not in app_user_ids: def _generate(i):
await inline_query.answer([InlineQueryResultArticle('...no', InputTextMessageContent('...no'))], cache_time=3600, is_personal=True) @Client.on_inline_query(filters.regex(f'^{i}$'))
return @log_errors
async with session.get(f'https://nekos.life/api/v2/img/{i}') as resp: async def func(client, inline_query):
url = (await resp.json())['url'] if inline_query.from_user.id not in app_user_ids:
call = InlineQueryResultAnimation if '.gif' == os.path.splitext(url)[1] else InlineQueryResultPhoto await inline_query.answer([InlineQueryResultArticle('...no', InputTextMessageContent('...no'))], cache_time=3600, is_personal=True)
await inline_query.answer([call(url, caption=url, parse_mode=None)], cache_time=0) return
return func async with session.get(f'https://nekos.life/api/v2/img/{i}') as resp:
func = _generate(i) url = (await resp.json())['url']
globals()[i] = func call = InlineQueryResultAnimation if '.gif' == os.path.splitext(url)[1] else InlineQueryResultPhoto
locals()[i] = func await inline_query.answer([call(url, caption=url, parse_mode=None)], cache_time=0)
func = None return func
break func = _generate(i)
globals()[i] = func
locals()[i] = func
func = None
break