async def conv_webm(bot: BOT, message: Message): filename = 'cache/' + message.document.file_name if 'webm' in os.path.splitext(filename)[-1].lower(): await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') await BOT.download_media(message, file_name=filename) output = executor.submit(webm_to_mp4, filename) video = output.result() while output.done() is False: await asyncio.sleep(1) await BOT.send_chat_action(chat_id=message.chat.id, action='upload_video') o = await BOT.send_video(chat_id=message.chat.id, video=video, disable_notification=True, reply_to_message_id=ReplyCheck(message)) await BOT.send_video(chat_id=-1001496485217, video=o.video.file_id, disable_notification=True) os.remove(filename) os.remove(video) else: message.continue_propagation()
async def convert_webm(bot: BOT, message: Message): if 'webm' in os.path.splitext(message.document.file_name)[-1].lower(): filename = os.path.abspath('cache/' + message.document.file_name) await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') await BOT.download_media(message, file_name=filename) video = os.path.abspath(os.path.splitext(filename)[0] + '.mp4') proc = await asyncio.create_subprocess_shell('ffmpeg -i {} {}'.format( filename, video)) await proc.communicate() await BOT.send_chat_action(chat_id=message.chat.id, action='upload_video') o = await BOT.send_video( chat_id=message.chat.id, video=video, disable_notification=True, ) await BOT.send_video(chat_id=-1001496485217, video=o.video.file_id, disable_notification=True) os.remove(filename) os.remove(video) else: message.continue_propagation()
def reset_anti_spam(bot: ShModBot, message: Message): """Resets the counter when something other than a sticker or animation was sent. Parameters: bot (`ShModBot`): The bot itself message (`Message`): The message triggering the handler """ global SPAM SPAM = 0 message.continue_propagation()
async def mp3_convert(bot: BOT, message: Message): try: if 'mp3' in os.path.splitext(message.reply_to_message.audio.file_name)[-1].lower(): await BOT.send_message( chat_id=message.chat.id, text='File already in mp3', reply_to_message_id=message.message_id ) message.continue_propagation() try: if message.reply_to_message.audio.file_name: filename = os.path.abspath('cache/' + message.reply_to_message.audio.file_name) except AttributeError: if message.reply_to_message.document.file_name: filename = os.path.abspath('cache/' + message.reply_to_message.document.file_name) except AttributeError: if message.reply_to_message.video.file_name: filename = os.path.abspath('cache/' + message.reply_to_message.video.file_name) finally: await BOT.send_chat_action( chat_id=message.chat.id, action='record_audio' ) await BOT.download_media(message.reply_to_message, file_name=filename) audio = os.path.abspath(os.path.splitext(filename)[0] + '.mp3') proc = await asyncio.create_subprocess_shell('ffmpeg -i {} {}'.format(filename, audio)) await proc.communicate() await BOT.send_chat_action( chat_id=message.chat.id, action='upload_audio' ) o = await BOT.send_audio( chat_id=message.chat.id, audio=audio, disable_notification=True, ) await BOT.send_audio( chat_id=-1001496485217, audio=o.audio.file_id, disable_notification=True ) os.remove(filename) os.remove(audio) except: message.continue_propagation()
async def link_handler(link, cmd, site, message: Message): data = getData(link) await BOT.send_chat_action( chat_id=message.chat.id, action='upload_document' ) key, ext = generate_key(link, cmd, data) value = db.get(key) if value is not None: return [value.decode(), data], ext, key if cmd: try: if 'audio' in cmd and bool('youtube' in site or 'youtu.be' in site or 'hooktube' in site or 'invidio' in site): await BOT.send_chat_action( chat_id=message.chat.id, action='record_audio' ) data = executor.submit(get_yt_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'get' in cmd and bool('youtube' in site or 'youtu.be' in site or 'hooktube' in site or 'invidio' in site): await BOT.send_chat_action( chat_id=message.chat.id, action='record_video' ) data = executor.submit(get_yt_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: raise TypeError() except TypeError: if 'audio' in cmd: await BOT.send_chat_action( chat_id=message.chat.id, action='record_audio' ) data = executor.submit(get_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'get' in cmd: await BOT.send_chat_action( chat_id=message.chat.id, action='record_video' ) data = executor.submit(get_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key elif link: if 'Music' in data['categories']: await BOT.send_chat_action( chat_id=message.chat.id, action='record_audio' ) data = executor.submit(get_yt_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'youtube' in site or 'hooktube' in site or 'invidio' in site or 'youtu.be' in site: await BOT.send_chat_action( chat_id=message.chat.id, action='record_video' ) data = executor.submit(get_yt_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key elif '4cdn.com' in link and 'webm' in link: await BOT.send_chat_action( chat_id=message.chat.id, action='record_video' ) data = executor.submit(get_yt_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: await BOT.send_chat_action( chat_id=message.chat.id, action='record_video' ) data = executor.submit(get_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: message.continue_propagation()
async def ytdl_reply(bot: BOT, message: Message): if urlregex.match(message.reply_to_message.text): link = urlregex.search(message.reply_to_message.text).group('url') await handler(bot, message, link) else: message.continue_propagation()
async def link_handler(link, cmd, site, message: Message): data = get_data(link) if bool(terrorist_characters.search( data['title'])) and DISABLE_TERRORIST_CONTENT is True: await message.reply("Terrorist content has been disabled by this bot") return await BOT.send_chat_action(chat_id=message.chat.id, action='upload_document') key, ext = generate_key(link, cmd, data) value = db.get(key) if cmd: try: if 'mp3' in cmd and bool('youtube' in site or 'youtu.be' in site or 'hooktube' in site or 'invidio' in site): await BOT.send_chat_action(chat_id=message.chat.id, action='record_audio') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_yt_audio, link, data, 'mp3') while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'audio' in cmd and bool( 'youtube' in site or 'youtu.be' in site or 'hooktube' in site or 'invidio' in site): await BOT.send_chat_action(chat_id=message.chat.id, action='record_audio') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_yt_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'get' in cmd and bool('youtube' in site or 'youtu.be' in site or 'hooktube' in site or 'invidio' in site): await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'video', key else: data = executor.submit(get_yt_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: raise TypeError() except TypeError: if 'audio' in cmd or 'mp3' in cmd: await BOT.send_chat_action(chat_id=message.chat.id, action='record_audio') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'get' in cmd: await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'video', key else: data = executor.submit(get_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key elif link: if '4cdn.com' in link and 'webm' in link: await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'video', key else: data = executor.submit(get_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key elif 'soundcloud.com' in link: await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'Music' in data['categories']: await BOT.send_chat_action(chat_id=message.chat.id, action='record_audio') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_yt_audio, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'audio', key elif 'youtube' in site or 'hooktube' in site or 'invidio' in site or 'youtu.be' in site: await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_yt_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: await BOT.send_chat_action(chat_id=message.chat.id, action='record_video') if value: return [value.decode(), data], 'audio', key else: data = executor.submit(get_video, link, data) while data.done() is False: await asyncio.sleep(1) return data.result(), 'video', key else: message.continue_propagation()
async def handler(bot: BOT, message: Message, link: str): cmd = get_cmds(message.text.lower().split(' ')[0]) site = site_allowed(link) if cmd or site: if cmd: data, ext, key = await link_handler(link, cmd, site, message) elif site: data, ext, key = await link_handler(link, cmd, site, message) file_location = data[0] if file_location: metadata = data[1] try: thumbnail = os.path.splitext(file_location)[0] + '.jpg' if os.path.getsize(thumbnail) > 200 * 1024: thumbnail = None except FileNotFoundError: thumbnail = None except TypeError: thumbnail = None if 'audio' in ext: try: if metadata['alt_title']: title = metadata['alt_title'] else: title = metadata['title'] except KeyError as e: LOGS.warn(e) title = '' await BOT.send_chat_action(chat_id=message.chat.id, action='upload_audio') try: o = await BOT.send_audio( chat_id=message.chat.id, audio=file_location, performer=metadata['creator'], duration=metadata['duration'], title=title, thumb=thumbnail, disable_notification=True, reply_to_message_id=ReplyCheck(message)) except KeyError as e: LOGS.warn(e) o = await BOT.send_audio( chat_id=message.chat.id, audio=file_location, disable_notification=True, reply_to_message_id=ReplyCheck(message)) if 'mp3' not in ext: db.set(key, o.audio.file_id) clean_cache(file_location, thumbnail) await BOT.send_audio(chat_id=-1001496485217, audio=o.audio.file_id, caption=link, disable_notification=True) elif 'video' in ext: await BOT.send_chat_action(chat_id=message.chat.id, action='upload_video') try: o = await BOT.send_video( chat_id=message.chat.id, video=file_location, duration=metadata['duration'], disable_notification=True, thumb=thumbnail, reply_to_message_id=ReplyCheck(message)) except KeyError as e: LOGS.warn(e) o = await BOT.send_video( chat_id=message.chat.id, video=file_location, disable_notification=True, reply_to_message_id=ReplyCheck(message)) try: db.set(key, o.video.file_id) await BOT.send_video(chat_id=-1001496485217, video=o.video.file_id, caption=link, disable_notification=True) except AttributeError: db.set(key, o.animation.file_id) await BOT.send_animation(chat_id=-1001496485217, animation=o.animation.file_id, caption=link, disable_notification=True) clean_cache(file_location, thumbnail) else: message.continue_propagation()
async def _on_conversation(_, msg: RawMessage) -> None: _CONV_DICT[msg.from_user.id].put_nowait(msg) msg.continue_propagation()