|
| 1 | +import aiohttp |
| 2 | +import requests |
| 3 | + |
| 4 | +from .anilist import t |
| 5 | +from Anime_Inline import ANIME |
| 6 | +from pyrogram import __version__ |
| 7 | +from Anime_Inline.Cutiepii_Robot.anilist import url, anime_query, manga_query, airing_query, character_query |
| 8 | +from pyrogram.types import ( |
| 9 | + InlineKeyboardButton, |
| 10 | + InlineKeyboardMarkup, |
| 11 | + InputTextMessageContent, |
| 12 | + InlineQueryResultArticle, |
| 13 | + InlineQueryResultPhoto |
| 14 | +) |
| 15 | + |
| 16 | +from Anime_Inline.helper.errors import capture_err |
| 17 | +from Anime_Inline import OWNER_USERNAME, BOT_USERNAME, BOT_NAME, INLINE_PIC, INLINE_PIC_1 |
| 18 | + |
| 19 | +NEXT_OFFSET = 25 |
| 20 | + |
| 21 | +@ANIME.on_inline_query() |
| 22 | +@capture_err |
| 23 | +async def inline_query_handler(client, query): |
| 24 | + string = query.query.lower() |
| 25 | + if string == "": |
| 26 | + await client.answer_inline_query(query.id, |
| 27 | + results=[ |
| 28 | + InlineQueryResultPhoto( |
| 29 | + caption=f"Hello I'm {BOT_NAME} , I can help you to find everything about anime. You can use command or inline to using me π", |
| 30 | + photo_url=f"{INLINE_PIC}", |
| 31 | + parse_mode="markdown", |
| 32 | + title=f"Help?", |
| 33 | + description=f"Click Here..", |
| 34 | + reply_markup=InlineKeyboardMarkup( |
| 35 | + [[ |
| 36 | + InlineKeyboardButton("Anime", switch_inline_query_current_chat="anime "), |
| 37 | + InlineKeyboardButton("Manga", switch_inline_query_current_chat="manga ") |
| 38 | + ], |
| 39 | + [ |
| 40 | + InlineKeyboardButton("Airing", switch_inline_query_current_chat="airing "), |
| 41 | + InlineKeyboardButton("Character", switch_inline_query_current_chat="character ") |
| 42 | + ], |
| 43 | + [ |
| 44 | + InlineKeyboardButton(text="Help", url=f"https://t.me/{BOT_USERNAME}?start=help") |
| 45 | + ]] |
| 46 | + ) |
| 47 | + ), |
| 48 | + InlineQueryResultPhoto( |
| 49 | + caption=f"{BOT_USERNAME} Maintained by {OWNER_USERNAME}", |
| 50 | + photo_url=f"{INLINE_PIC_1}", |
| 51 | + parse_mode="markdown", |
| 52 | + title=f"About?", |
| 53 | + description=f"Click Here..", |
| 54 | + reply_markup=InlineKeyboardMarkup( |
| 55 | + [[ |
| 56 | + InlineKeyboardButton(text="Support", url="support group telegram url"), |
| 57 | + InlineKeyboardButton(text="Channel", url="bot channel url telegram") |
| 58 | + ], |
| 59 | + [ |
| 60 | + InlineKeyboardButton(text="Anime_Inline", url="https://github.com/Awesome-RJ/Anime-Inline"), |
| 61 | + InlineKeyboardButton(text="Owner", url=f"https://t.me/{OWNER_USERNAME}") |
| 62 | + ], |
| 63 | + [ |
| 64 | + InlineKeyboardButton(text="Help", url=f"https://t.me/{BOT_USERNAME}?start=help") |
| 65 | + ]] |
| 66 | + ) |
| 67 | + ), |
| 68 | + ], |
| 69 | + switch_pm_text="bot name Inline Module", |
| 70 | + switch_pm_parameter="start", |
| 71 | + cache_time=300 |
| 72 | + ) |
| 73 | + |
| 74 | + answers = [] |
| 75 | + if string.split()[0] == "anime": |
| 76 | + if len(string.split()) == 1: |
| 77 | + await client.answer_inline_query(query.id, |
| 78 | + results=answers, |
| 79 | + switch_pm_text="Search an Anime", |
| 80 | + switch_pm_parameter="start" |
| 81 | + ) |
| 82 | + return |
| 83 | + search = string.split(None, 1)[1] |
| 84 | + variables = {'search' : search} |
| 85 | + async with aiohttp.ClientSession() as session: |
| 86 | + async with session.post(url, json={'query': anime_query, 'variables': variables}) as resp: |
| 87 | + r = await resp.json() |
| 88 | + json = r['data'].get('Media', None) |
| 89 | + if json: |
| 90 | + mal_id = int(json.get('idMal')) |
| 91 | + msg = f"**{json['title']['romaji']}** (`{json['title']['native']}`)\n**Type**: {json['format']}\n**Status**: {json['status']}\n**Episodes**: {json.get('episodes', 'N/A')}\n**Duration**: {json.get('duration', 'N/A')} Per Ep.\n**Score**: {json['averageScore']}\n**Genres**: `" |
| 92 | + for x in json['genres']: |
| 93 | + msg += f"{x}, " |
| 94 | + msg = msg[:-2] + '`\n' |
| 95 | + msg += "**Studios**: `" |
| 96 | + for x in json['studios']['nodes']: |
| 97 | + msg += f"{x['name']}, " |
| 98 | + msg = msg[:-2] + '`\n' |
| 99 | + info = json.get('siteUrl') |
| 100 | + mal_link = f"https://myanimelist.net/anime/{mal_id}" |
| 101 | + trailer = json.get('trailer', None) |
| 102 | + if trailer: |
| 103 | + trailer_id = trailer.get('id', None) |
| 104 | + site = trailer.get('site', None) |
| 105 | + if site == "youtube": trailer = 'https://youtu.be/' + trailer_id |
| 106 | + description = json.get('description', 'N/A').replace('<i>', '').replace('</i>', '').replace('<br>', '') |
| 107 | + msg += shorten(description, info) |
| 108 | + image = info.replace('anilist.co/anime/', 'img.anili.st/media/') |
| 109 | + if trailer: |
| 110 | + buttons = [[InlineKeyboardButton("Anilist", url=info), InlineKeyboardButton("MAL", url=mal_link)], [InlineKeyboardButton("Trailer π¬", url=trailer)]] |
| 111 | + else: |
| 112 | + buttons = [[InlineKeyboardButton("Anilist", url=info), InlineKeyboardButton("MAL", url=mal_link) |
| 113 | + ]] |
| 114 | + if image: |
| 115 | + answers.append(InlineQueryResultPhoto( |
| 116 | + caption=msg, |
| 117 | + photo_url=image, |
| 118 | + parse_mode="markdown", |
| 119 | + title=f"{json['title']['romaji']}", |
| 120 | + description=f"{json['format']} | {json.get('episodes', 'N/A')} Episode{'s' if len(str(json.get('episodes'))) > 1 else ''}", |
| 121 | + reply_markup=InlineKeyboardMarkup(buttons))) |
| 122 | + else: |
| 123 | + answers.append(InlineQueryResultArticle( |
| 124 | + title=f"{json['title']['romaji']}", |
| 125 | + description=f"{json['format']} | {json.get('episodes', 'N/A')} Episode{'s' if len(str(json.get('episodes'))) > 1 else ''}", |
| 126 | + input_message_content=InputTextMessageContent(msg, parse_mode="md", disable_web_page_preview=True), |
| 127 | + reply_markup=InlineKeyboardMarkup(buttons))) |
| 128 | + await client.answer_inline_query(query.id, |
| 129 | + results=answers, |
| 130 | + cache_time=0, |
| 131 | + is_gallery=False |
| 132 | + ) |
| 133 | + |
| 134 | + elif string.split()[0] == "manga": |
| 135 | + if len(string.split()) == 1: |
| 136 | + await client.answer_inline_query(query.id, |
| 137 | + results=answers, |
| 138 | + switch_pm_text="Search Manga", |
| 139 | + switch_pm_parameter="start" |
| 140 | + ) |
| 141 | + return |
| 142 | + search = string.split(None, 1)[1] |
| 143 | + variables = {'search' : search} |
| 144 | + async with aiohttp.ClientSession() as session: |
| 145 | + async with session.post(url, json={'query': manga_query, 'variables': variables}) as resp: |
| 146 | + r = await resp.json() |
| 147 | + json = r['data'].get('Media', None) |
| 148 | + if json: |
| 149 | + msg = f"**{json['title']['romaji']}** (`{json['title']['native']}`)\n**Status**: {json['status']}\n**Year**: {json['startDate']['year']}\n**Score**: {json['averageScore']}\n**Genres**: `" |
| 150 | + for x in json['genres']: |
| 151 | + msg += f"{x}, " |
| 152 | + msg = msg[:-2] + '`\n' |
| 153 | + description = json.get('description', 'N/A').replace('<i>', '').replace('</i>', '').replace('<br>', '') |
| 154 | + info = json.get('siteUrl') |
| 155 | + if info: |
| 156 | + buttons = InlineKeyboardMarkup([[InlineKeyboardButton("More Info", url=info)]]) |
| 157 | + else: |
| 158 | + buttons = None |
| 159 | + msg += shorten(description, info) |
| 160 | + banner_url = json.get('bannerImage') |
| 161 | + if banner_url: |
| 162 | + answers.append(InlineQueryResultPhoto( |
| 163 | + caption=msg, |
| 164 | + photo_url=banner_url, |
| 165 | + parse_mode="markdown", |
| 166 | + title=f"{json['title']['romaji']}", |
| 167 | + description=f"{json['startDate']['year']}", |
| 168 | + reply_markup=buttons))8 |
| 169 | + else: |
| 170 | + answers.append(InlineQueryResultArticle( |
| 171 | + title=f"{json['title']['romaji']}", |
| 172 | + description=f"{json['averageScore']}", |
| 173 | + input_message_content=InputTextMessageContent(msg, parse_mode="markdown", disable_web_page_preview=True), |
| 174 | + reply_markup=buttons)) |
| 175 | + await client.answer_inline_query(query.id, |
| 176 | + results=answers, |
| 177 | + cache_time=0, |
| 178 | + is_gallery=False |
| 179 | + ) |
| 180 | + elif string.split()[0] == "airing": |
| 181 | + if len(string.split()) == 1: |
| 182 | + await client.answer_inline_query(query.id, |
| 183 | + results=answers, |
| 184 | + switch_pm_text="Get the Airing Status", |
| 185 | + switch_pm_parameter="start" |
| 186 | + ) |
| 187 | + return |
| 188 | + search = string.split(None, 1)[1] |
| 189 | + variables = {'search': search} |
| 190 | + response = requests.post( |
| 191 | + url, json={'query': airing_query, 'variables': variables}).json()['data']['Media'] |
| 192 | + info = response['siteUrl'] |
| 193 | + if info: |
| 194 | + buttons = InlineKeyboardMarkup([[InlineKeyboardButton("More Info", url=info)]]) |
| 195 | + else: |
| 196 | + buttons = None |
| 197 | + image = info.replace('anilist.co/anime/', 'img.anili.st/media/') |
| 198 | + thumb = image or None |
| 199 | + ms_g = f"**Name**: **{response['title']['romaji']}**(`{response['title']['native']}`)\n**ID**: `{response['id']}`" |
| 200 | + if response['nextAiringEpisode']: |
| 201 | + airing_time = response['nextAiringEpisode']['timeUntilAiring'] * 1000 |
| 202 | + airing_time_final = t(airing_time) |
| 203 | + in_des = f"Episode {response['nextAiringEpisode']['episode']} Airing in {airing_time_final}" |
| 204 | + ms_g += f"\n**Episode**: `{response['nextAiringEpisode']['episode']}`\n**Airing In**: `{airing_time_final}`" |
| 205 | + else: |
| 206 | + in_des = "N/A" |
| 207 | + ms_g += f"\n**Episode**:{response['episodes']}\n**Status**: `N/A`" |
| 208 | + answers.append(InlineQueryResultArticle( |
| 209 | + title=f"{response['title']['romaji']}", |
| 210 | + description=f"{in_des}", |
| 211 | + input_message_content=InputTextMessageContent(f"{ms_g}[β β ]({image})", parse_mode="markdown", disable_web_page_preview=False), |
| 212 | + reply_markup=buttons, |
| 213 | + thumb_url=thumb)) |
| 214 | + await client.answer_inline_query(query.id, |
| 215 | + results=answers, |
| 216 | + cache_time=0, |
| 217 | + is_gallery=False |
| 218 | + ) |
| 219 | + elif string.split()[0] == "character": |
| 220 | + if len(string.split()) == 1: |
| 221 | + await client.answer_inline_query(query.id, |
| 222 | + results=answers, |
| 223 | + switch_pm_text="Get Character Info", |
| 224 | + switch_pm_parameter="start" |
| 225 | + ) |
| 226 | + return |
| 227 | + search = string.split(None, 1)[1] |
| 228 | + variables = {'query': search} |
| 229 | + json = requests.post(url, json={'query': character_query, 'variables': variables}).json()['data']['Character'] |
| 230 | + if json: |
| 231 | + ms_g = f"**{json.get('name').get('full')}**(`{json.get('name').get('native')}`)\nβ€οΈ Favourites : {json['favourites']}\n" |
| 232 | + description = f"{json['description']}" |
| 233 | + site_url = json.get('siteUrl') |
| 234 | + ms_g += shorten(description, site_url) |
| 235 | + image = json.get('image', None) |
| 236 | + if image: |
| 237 | + image = image.get('large') |
| 238 | + answers.append(InlineQueryResultPhoto( |
| 239 | + caption=ms_g, |
| 240 | + photo_url=image, |
| 241 | + parse_mode="markdown", |
| 242 | + title=f"{json.get('name').get('full')}", |
| 243 | + description=f"β€οΈ{json['favourites']}", |
| 244 | + reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("More Info", url=site_url)]]))) |
| 245 | + else: |
| 246 | + answers.append(InlineQueryResultArticle( |
| 247 | + title=f"{json.get('name').get('full')}", |
| 248 | + description=f"{json['favourites']}", |
| 249 | + input_message_content=InputTextMessageContent(ms_g, parse_mode="markdown", disable_web_page_preview=True), |
| 250 | + reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("More Info", url=site_url)]]))) |
| 251 | + await client.answer_inline_query(query.id, |
| 252 | + results=answers, |
| 253 | + cache_time=0, |
| 254 | + is_gallery=False |
| 255 | + ) |
0 commit comments