|
| 1 | +from aiogram import Router |
| 2 | +from aiogram.types import ( |
| 3 | + InlineQuery, |
| 4 | + ChosenInlineResult, |
| 5 | + InlineQueryResultArticle, |
| 6 | + InputTextMessageContent, |
| 7 | + InlineKeyboardMarkup, |
| 8 | + InlineKeyboardButton, |
| 9 | + InlineQueryResultsButton |
| 10 | +) |
| 11 | + |
| 12 | +import logging |
| 13 | + |
| 14 | +from data.config import locale, api_alt_mode |
| 15 | +from data.loader import bot |
| 16 | +from misc.utils import lang_func |
| 17 | +from data.db_service import add_video, get_user, get_user_settings, increment_ad_msgs |
| 18 | +from misc.tiktok_api import ttapi |
| 19 | +from misc.video_types import send_video_result |
| 20 | + |
| 21 | +inline_router = Router(name=__name__) |
| 22 | + |
| 23 | +def please_wait_button(lang): |
| 24 | + return InlineKeyboardMarkup( |
| 25 | + inline_keyboard=[ |
| 26 | + [ |
| 27 | + InlineKeyboardButton(text=locale[lang]['inline_download_video_wait'], callback_data=f"wait") |
| 28 | + ] |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@inline_router.inline_query() |
| 34 | +async def handle_inline_query(inline_query: InlineQuery): |
| 35 | + """Handle inline queries and return example results""" |
| 36 | + api = ttapi() |
| 37 | + query_text = inline_query.query.strip() |
| 38 | + user_id = inline_query.from_user.id |
| 39 | + lang = await lang_func(user_id, inline_query.from_user.language_code) |
| 40 | + user_info = await get_user(user_id) |
| 41 | + results = [] |
| 42 | + |
| 43 | + if user_info is None: |
| 44 | + start_bot_button = InlineQueryResultsButton( |
| 45 | + text=locale[lang]['inline_start_bot'], |
| 46 | + start_parameter="inline") |
| 47 | + return await inline_query.answer(results, cache_time=0, button=start_bot_button, is_personal=True) |
| 48 | + |
| 49 | + if len(query_text) < 12: |
| 50 | + return await inline_query.answer(results, cache_time=0) |
| 51 | + |
| 52 | + video_link, is_mobile = await api.regex_check(query_text) |
| 53 | + if video_link is None: |
| 54 | + results.append( |
| 55 | + InlineQueryResultArticle( |
| 56 | + id="wrong_link", |
| 57 | + title=locale[lang]['inline_wrong_link_title'], |
| 58 | + description=locale[lang]['inline_wrong_link_description'], |
| 59 | + input_message_content=InputTextMessageContent( |
| 60 | + message_text=locale[lang]['inline_wrong_link'], |
| 61 | + parse_mode="HTML" |
| 62 | + ), |
| 63 | + thumbnail_url="https://em-content.zobj.net/source/apple/419/cross-mark_274c.png" |
| 64 | + ) |
| 65 | + ) |
| 66 | + return await inline_query.answer(results, cache_time=0) |
| 67 | + else: |
| 68 | + results.append( |
| 69 | + InlineQueryResultArticle( |
| 70 | + id=f"download/{query_text}", |
| 71 | + title=locale[lang]['inline_download_video'], |
| 72 | + description=locale[lang]['inline_download_video_description'], |
| 73 | + input_message_content=InputTextMessageContent( |
| 74 | + message_text=locale[lang]['inline_download_video_text'] |
| 75 | + ), |
| 76 | + thumbnail_url="https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/png/tiktok-light.png", |
| 77 | + reply_markup=please_wait_button(lang) |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | + await inline_query.answer(results, cache_time=0) |
| 83 | + |
| 84 | + |
| 85 | +@inline_router.chosen_inline_result() |
| 86 | +async def handle_chosen_inline_result(chosen_result: ChosenInlineResult): |
| 87 | + """Handle when user selects an inline result""" |
| 88 | + api = ttapi() |
| 89 | + user_id = chosen_result.from_user.id |
| 90 | + message_id = chosen_result.inline_message_id |
| 91 | + video_link = chosen_result.query |
| 92 | + settings = await get_user_settings(user_id) |
| 93 | + was_processed = False |
| 94 | + if not settings: |
| 95 | + return |
| 96 | + lang, file_mode = settings |
| 97 | + |
| 98 | + try: |
| 99 | + if api_alt_mode: |
| 100 | + video_info = await api.rapid_video(video_link) |
| 101 | + else: |
| 102 | + video_info = await api.video(video_link) |
| 103 | + |
| 104 | + if video_info is False: |
| 105 | + return await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['bugged_error']) |
| 106 | + elif video_info is None: |
| 107 | + return await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['error']) |
| 108 | + |
| 109 | + if video_info['type'] == 'images': # Process image |
| 110 | + return await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['only_video_supported']) |
| 111 | + # await bot.edit_message_text(inline_message_id=message_id, text="⬆️ <code>Sending image...</code>\n\n") |
| 112 | + # try: |
| 113 | + # was_processed = await send_image_result(message_id, video_info, lang, file_mode, 1) |
| 114 | + # except: |
| 115 | + # await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['error']) |
| 116 | + else: # Process video |
| 117 | + await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['sending_inline_video']) |
| 118 | + # try: |
| 119 | + await send_video_result(message_id, video_info, lang, file_mode, api_alt_mode, True) |
| 120 | + # except: |
| 121 | + # return await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['error']) |
| 122 | + |
| 123 | + await increment_ad_msgs(user_id) |
| 124 | + |
| 125 | + try: # Try to write log into database |
| 126 | + # Write log into database |
| 127 | + await add_video(user_id, video_link, video_info['type'] == 'images', was_processed, True) |
| 128 | + # Log into console |
| 129 | + logging.info(f'Video Download: INLINE {user_id} - VIDEO {video_link}') |
| 130 | + # If cant write log into database or log into console |
| 131 | + except Exception as e: |
| 132 | + logging.error('Cant write into database') |
| 133 | + logging.error(e) |
| 134 | + except Exception as e: # If something went wrong |
| 135 | + logging.error(e) |
| 136 | + try: |
| 137 | + await bot.edit_message_text(inline_message_id=message_id, text=locale[lang]['error']) |
| 138 | + except: |
| 139 | + pass |
0 commit comments