async def favorite_callback(bot: Amime, callback: CallbackQuery): content_type = callback.matches[0]["type"] content_id = int(callback.matches[0]["id"]) message = callback.message user = callback.from_user lang = callback._lang favorite = await Favorites.get_or_none(user=user.id, item=content_id, type=content_type) if favorite is None: await Favorites.create(user=user.id, item=content_id, type=content_type) await callback.answer(lang.added_to_favorites_alert, show_alert=True) else: await favorite.delete() await callback.answer(lang.removed_from_favorites_alert, show_alert=True) keyboard = bki(message.reply_markup) for line, column in enumerate(keyboard): for index, button in enumerate(column): if button[1].startswith("favorite"): keyboard[line][index] = await get_favorite_button( lang, user, content_type, content_id) await callback.edit_message_reply_markup(ikb(keyboard))
async def watched_callback(bot: Amime, callback: CallbackQuery): episode_id = int(callback.matches[0]["id"]) message = callback.message user = callback.from_user lang = callback._lang watched = await Watched.get_or_none( user=user.id, episode=episode_id, ) if watched is None: await Watched.create(user=user.id, episode=episode_id) else: await watched.delete() keyboard = bki(message.reply_markup) for line, column in enumerate(keyboard): for index, button in enumerate(column): if button[1].startswith("watched"): keyboard[line][index] = await get_watched_button( lang, user, episode_id, ) await callback.edit_message_reply_markup(ikb(keyboard))
async def reply_text(self, text: str, reply_markup=None, *args, **kwargs): if reply_markup and self._client.session_name != "bot": if type(reply_markup) == types.InlineKeyboardMarkup: reply_markup = bki(reply_markup) message = await Message.create(text=text, keyboard=reply_markup) bot = self._client.assistant inline_results = await self._client.get_inline_bot_results( bot.me.username or bot.me.id, str(message.key)) result = inline_results.results[0] reply_to = None if kwargs.get("quote"): reply_to = self.message_id return await self._client.send_inline_bot_result( self.chat.id, inline_results.query_id, result.id, reply_to_message_id=reply_to, ) return await self.reply_text(text, reply_markup=reply_markup, *args, **kwargs)
async def notify_callback(bot: Amime, callback: CallbackQuery): content_type = callback.matches[0]["c_type"] content_id = int(callback.matches[0]["c_id"]) recipient_type = callback.matches[0]["r_type"] recipient_id = int(callback.matches[0]["r_id"]) message = callback.message chat = message.chat user = callback.from_user lang = callback._lang if recipient_type == "group": if not await filters.administrator(bot, callback): return elif content_type == "episodes": callback.continue_propagation() notify = await Notify.get_or_none( recipient=recipient_id, recipient_type=recipient_type, item=content_id, type=content_type, ) if notify is None: await Notify.create( recipient=recipient_id, recipient_type=recipient_type, item=content_id, type=content_type, ) await callback.answer(lang.notifications_linked_alert, show_alert=True) else: await notify.delete() await callback.answer(lang.notifications_turned_off_alert, show_alert=True) keyboard = bki(message.reply_markup) for line, column in enumerate(keyboard): for index, button in enumerate(column): if button[1].startswith("notify"): keyboard[line][index] = await get_notify_button( lang, user if recipient_type == "user" else chat, content_type, content_id, ) await callback.edit_message_reply_markup(ikb(keyboard))
def message_ikb(self): return bki(self.reply_markup)
async def anime_scan(bot: Amime, message: Message): reply = message.reply_to_message lang = message._lang if reply.from_user.id == bot.me.id: return if not reply.media: await message.reply_text(lang.media_not_found_text) return media = (reply.photo or reply.sticker or reply.animation or reply.document or reply.video) if isinstance(media, (Document, Video)): if bool(media.thumbs) and len(media.thumbs) > 0: media = media.thumbs[0] else: return sent = await message.reply_photo("https://i.imgur.com/m0N2pFc.jpg", caption=lang.scanning_media_text) path = await bot.download_media(media) async with httpx.AsyncClient(http2=True) as client: try: response = await client.post( "https://api.trace.moe/search?anilistInfo&cutBorders", files=dict(image=open(path, "rb")), timeout=20.0, ) except httpx.TimeoutException: await sent.edit_text(lang.timed_out_text) return if response.status_code == 200: pass elif response.status_code == 429: await sent.edit_text(lang.api_overuse_text) return else: await sent.edit_text(lang.api_down_text) return data = response.json() results = data["result"] if len(results) == 0: await sent.edit_text(lang.no_results_text) return result = results[0] video = result["video"] to_time = result["to"] episode = result["episode"] anilist_id = result["anilist"]["id"] file_name = result["filename"] from_time = result["from"] similarity = result["similarity"] title_native = result["anilist"]["title"]["native"] title_romaji = result["anilist"]["title"]["romaji"] text = f"<b>{title_romaji}</b>" if bool(title_native): text += f" (<code>{title_native}</code>)" text += "\n" text += f"\n<b>ID</b>: <code>{anilist_id}</code>" if episode is not None: text += f"\n<b>{lang.episode}</b>: <code>{episode}</code>" text += ( f"\n<b>{lang.similarity}</b>: <code>{round(similarity * 100, 2)}%</code>" ) sent = await sent.edit_media( InputMediaPhoto( f"https://img.anili.st/media/{anilist_id}", text, ), reply_markup=ikb([[ (lang.view_more_button, f"anime {anilist_id}"), ]]), ) if video is not None: try: sent_video = await sent.reply_video( video, caption= f"<code>{file_name}</code>\n\n<code>{pendulum.from_timestamp(from_time).to_time_string()}</code> - <code>{pendulum.from_timestamp(to_time).to_time_string()}</code>", ) keyboard = bki(sent.reply_markup) keyboard[0].append(("📹 Preview", sent_video.link, "url")) await sent.edit_reply_markup(reply_markup=ikb(keyboard), ) except BaseException: pass await client.aclose() os.remove(path)