async def save_db(pyro_client, chat_id, message_id, db_store): tmp_file_name = "pyrogram.db" with open(tmp_file_name, "w") as fp: json.dump(db_store, fp) thumb_image_path = "./pyrobot/helper_functions/Pyrogram.jpg" await pyro_client.edit_message_media( chat_id=chat_id, message_id=message_id, media=InputMediaDocument( media=tmp_file_name, thumb=thumb_image_path, caption="pyrogram.db // do not touch", parse_mode="html" ) ) os.remove(tmp_file_name)
async def catch_youtube_dldata(c, q): filename = None cb_data = q.data.strip() # Callback Data Check yturl = cb_data.split("||")[-1] format_id = cb_data.split("||")[-2] if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")): print("no data found") raise ContinuePropagation filext = "%(title)s.%(ext)s" userdir = os.path.join(os.getcwd(), "downloads", str(q.message.chat.id)) if not os.path.isdir(userdir): os.makedirs(userdir) await q.edit_message_reply_markup( InlineKeyboardMarkup([[InlineKeyboardButton("Downloading...", callback_data="down")]])) filepath = os.path.join(userdir, filext) # await q.edit_message_reply_markup([[InlineKeyboardButton("Processing..")]]) audio_command = [ "youtube-dl", "-c", "--prefer-ffmpeg", "--extract-audio", "--audio-format", "mp3", "--audio-quality", format_id, "-o", filepath, yturl, ] video_command = [ "youtube-dl", "-c", "--embed-subs", "-f", f"{format_id}+bestaudio", "-o", filepath, "--hls-prefer-ffmpeg", yturl] loop = asyncio.get_event_loop() med = None if cb_data.startswith("audio"): filename = await downloadaudiocli(audio_command) med = InputMediaAudio( media=filename, caption=os.path.basename(filename), title=os.path.basename(filename) ) if cb_data.startswith("video"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaVideo( media=filename, duration=dur, caption=os.path.basename(filename), supports_streaming=True ) if cb_data.startswith("docaudio"): filename = await downloadaudiocli(audio_command) med = InputMediaDocument( media=filename, caption=os.path.basename(filename), ) if cb_data.startswith("docvideo"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaDocument( media=filename, caption=os.path.basename(filename), ) if med: loop.create_task(send_file(c, q, med, filename)) else: print("med not found")
async def upload_single_file(message, local_file_name, caption_str, from_user, edit_media): sent_message = None start_time = time.time() # thumbnail_location = os.path.join(DOWNLOAD_LOCATION, "thumbnails", str(from_user) + ".jpg") LOGGER.info(thumbnail_location) # try: message_for_progress_display = message if not edit_media: message_for_progress_display = await message.reply_text( "starting upload of {}".format( os.path.basename(local_file_name))) if local_file_name.upper().endswith(("MKV", "MP4", "WEBM")): metadata = extractMetadata(createParser(local_file_name)) duration = 0 if metadata.has("duration"): duration = metadata.get('duration').seconds # width = 0 height = 0 thumb_image_path = None if os.path.exists(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) else: thumb_image_path = await take_screen_shot( local_file_name, os.path.dirname(os.path.abspath(local_file_name)), (duration / 2)) # get the correct width, height, and duration for videos greater than 10MB if os.path.exists(thumb_image_path): metadata = extractMetadata(createParser(thumb_image_path)) if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") # resize image # ref: https://t.me/PyrogramChat/44663 # https://stackoverflow.com/a/21669827/4723940 Image.open(thumb_image_path).convert("RGB").save( thumb_image_path) img = Image.open(thumb_image_path) # https://stackoverflow.com/a/37631799/4723940 img.resize((320, height)) img.save(thumb_image_path, "JPEG") # https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#create-thumbnails # thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # send video if edit_media and message.photo: sent_message = await message.edit_media( media=InputMediaVideo(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html", width=width, height=height, duration=duration, supports_streaming=True) # quote=True, ) else: sent_message = await message.reply_video( video=local_file_name, # quote=True, caption=caption_str, parse_mode="html", duration=duration, width=width, height=height, thumb=thumb, supports_streaming=True, disable_notification=True, reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("trying to upload", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) elif local_file_name.upper().endswith( ("MP3", "M4A", "M4B", "FLAC", "WAV")): metadata = extractMetadata(createParser(local_file_name)) duration = 0 title = "" artist = "" if metadata.has("duration"): duration = metadata.get('duration').seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") thumb_image_path = None if os.path.isfile(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # send audio if edit_media and message.photo: sent_message = await message.edit_media( media=InputMediaAudio(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html", duration=duration, performer=artist, title=title) # quote=True, ) else: sent_message = await message.reply_audio( audio=local_file_name, # quote=True, caption=caption_str, parse_mode="html", duration=duration, performer=artist, title=title, thumb=thumb, disable_notification=True, reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("trying to upload", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) else: thumb_image_path = None if os.path.isfile(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) # if a file, don't upload "thumb" # this "diff" is a major derp -_- πππ thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # # send document if edit_media and message.photo: sent_message = await message.edit_media( media=InputMediaDocument(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html") # quote=True, ) else: sent_message = await message.reply_document( document=local_file_name, # quote=True, thumb=thumb, caption=caption_str, parse_mode="html", disable_notification=True, reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("trying to upload", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) except Exception as e: await message_for_progress_display.edit_text("**FAILED**\n" + str(e)) else: if message.message_id != message_for_progress_display.message_id: await message_for_progress_display.delete() os.remove(local_file_name) return sent_message
async def catch_youtube_dldata(c, q): thumb_image_path = os.getcwd() + "/" + "thumbnails" + "/" + str( q.from_user.id) + ".jpg" yt_thumb_image_path = os.getcwd() + "/" + "YouTubeThumb" + "/" + str( q.from_user.id) + ".jpg" if os.path.exists(thumb_image_path): thumb_image = thumb_image_path else: thumb_image = yt_thumb_image_path file_name = str(Config.PRE_FILE_TXT) cb_data = q.data # Callback Data Check (for Youtube formats) if cb_data.startswith(("video", "audio", "docaudio", "docvideo")): yturl = cb_data.split("||")[-1] format_id = cb_data.split("||")[-2] if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")): print("no data found") raise ContinuePropagation new_filext = "%(title)s.%(ext)s" filext = file_name + new_filext saved_file_path = os.path.join(os.getcwd(), "downloads", str(q.message.chat.id)) if not os.path.isdir(saved_file_path): os.makedirs(saved_file_path) dl_folder = [f for f in os.listdir(saved_file_path)] for f in dl_folder: try: os.remove(os.path.join(saved_file_path, f)) except IndexError: pass await q.edit_message_text(text=Translation.DOWNLOAD_START) filepath = os.path.join(saved_file_path, filext) audio_command = [ "youtube-dl", "-c", "--prefer-ffmpeg", "--extract-audio", "--audio-format", "mp3", "--audio-quality", format_id, "-o", filepath, yturl, ] video_command = [ "youtube-dl", "-c", "--embed-subs", "-f", f"{format_id}+bestaudio", "-o", filepath, "--hls-prefer-ffmpeg", yturl ] loop = asyncio.get_event_loop() med = None if cb_data.startswith("audio"): filename = await downloadaudiocli(audio_command) med = InputMediaAudio(media=filename, caption=os.path.basename(filename), title=os.path.basename(filename), thumb=thumb_image) if cb_data.startswith("video"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaVideo(media=filename, duration=dur, caption=os.path.basename(filename), thumb=thumb_image, supports_streaming=True) if cb_data.startswith("docaudio"): filename = await downloadaudiocli(audio_command) med = InputMediaDocument(media=filename, caption=os.path.basename(filename), thumb=thumb_image) if cb_data.startswith("docvideo"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaDocument(media=filename, caption=os.path.basename(filename), thumb=thumb_image) if med: loop.create_task(send_file(c, q, med)) else: print("med not found") ######################################### CB Data query for Bot Settings ############################################### else: # Callback Data Check (for bot settings) if cb_data.startswith(("help", "view_thumb", "close", "start", "thumb_del_conf", "del_sure")): if cb_data.startswith("help"): await help_me(c, q) if cb_data.startswith("close"): await close_button(c, q) if cb_data.startswith("view_thumb"): await view_thumbnail(c, q) if cb_data.startswith("start"): await start_bot(c, q) if cb_data.startswith("thumb_del_conf"): await del_thumb_confirm(c, q) if cb_data.startswith("del_sure"): await delete_thumbnail(c, q)
async def catch_youtube_dldata(c, q): cb_data = q.data.strip() #print(q.message.chat.id) # Callback Data Check yturl = cb_data.split("||")[-1] format_id = cb_data.split("||")[-2] thumb_image_path = "/app/downloads" + \ "/" + str(q.message.chat.id) + ".jpg" print(thumb_image_path) if os.path.exists(thumb_image_path): width = 0 height = 0 metadata = extractMetadata(createParser(thumb_image_path)) #print(metadata) if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") img = Image.open(thumb_image_path) if cb_data.startswith(("audio", "docaudio", "docvideo")): img.resize((320, height)) else: img.resize((90, height)) img.save(thumb_image_path, "JPEG") # print(thumb_image_path) if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")): print("no data found") raise ContinuePropagation filext = "%(title)s.%(ext)s" userdir = os.path.join(os.getcwd(), "downloads", str(q.message.chat.id)) if not os.path.isdir(userdir): os.makedirs(userdir) await q.edit_message_reply_markup( InlineKeyboardMarkup( [[InlineKeyboardButton("Downloading...", callback_data="down")]])) filepath = os.path.join(userdir, filext) # await q.edit_message_reply_markup([[InlineKeyboardButton("Processing..")]]) audio_command = [ "youtube-dl", "-c", "--prefer-ffmpeg", "--extract-audio", "--audio-format", "mp3", "--audio-quality", format_id, "-o", filepath, yturl, ] video_command = [ "youtube-dl", "-c", "--embed-subs", "-f", f"{format_id}+bestaudio", "-o", filepath, "--hls-prefer-ffmpeg", yturl ] loop = asyncio.get_event_loop() med = None if cb_data.startswith("audio"): filename = await downloadaudiocli(audio_command) med = InputMediaAudio(media=filename, thumb=thumb_image_path, caption=os.path.basename(filename), title=os.path.basename(filename)) if cb_data.startswith("video"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaVideo(media=filename, duration=dur, width=width, height=height, thumb=thumb_image_path, caption=os.path.basename(filename), supports_streaming=True) if cb_data.startswith("docaudio"): filename = await downloadaudiocli(audio_command) med = InputMediaDocument( media=filename, thumb=thumb_image_path, caption=os.path.basename(filename), ) if cb_data.startswith("docvideo"): filename = await downloadvideocli(video_command) dur = round(duration(filename)) med = InputMediaDocument( media=filename, thumb=thumb_image_path, caption=os.path.basename(filename), ) if med: loop.create_task(send_file(c, q, med, filename)) else: print("med not found")
thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name)) ) # if a file, don't upload "thumb" # this "diff" is a major derp -_- πππ thumb = None if thumb_image_path is not None and os.path.isfile(thumb_image_path): thumb = thumb_image_path # # send document if edit_media and message.photo: sent_message = await message.edit_media( media=InputMediaDocument( media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html" ) # quote=True, ) else: sent_message = await message.reply_document( document=local_file_name, # quote=True, thumb=thumb, caption=caption_str, parse_mode="html", disable_notification=True, reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=(
async def upload_single_file(message, local_file_name, caption_str, from_user, edit_media): await asyncio.sleep(EDIT_SLEEP_TIME_OUT) sent_message = None start_time = time.time() # thumbnail_location = os.path.join(DOWNLOAD_LOCATION, "thumbnails", str(from_user) + ".jpg") LOGGER.info(thumbnail_location) dyna_user_config_upload_as_doc = False for key in iter(user_specific_config): if key == from_user: dyna_user_config_upload_as_doc = user_specific_config[ key].upload_as_doc LOGGER.info(f'Found dyanamic config for user {from_user}') # if UPLOAD_AS_DOC.upper() == 'TRUE' or dyna_user_config_upload_as_doc: thumb_image_path = None if thumbnail_location is not None and os.path.exists( thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) if thumb_image_path is not None and os.path.exists(thumb_image_path): Image.open(thumb_image_path).convert("RGB").save(thumb_image_path) img = Image.open(thumb_image_path) # https://stackoverflow.com/a/37631799/4723940 img.resize((32, 32)) img.save(thumb_image_path, "JPEG") thumb = None if thumb_image_path is not None and os.path.isfile(thumb_image_path): thumb = thumb_image_path message_for_progress_display = message if not edit_media: message_for_progress_display = await message.reply_text( "<b>Starting Upload π€</b>\n\n<b>File Name :</b> <code>{}</code>" .format(os.path.basename(local_file_name))) sent_message = await message.reply_document( document=local_file_name, # quote=True, thumb=thumb, caption=caption_str, parse_mode="html", disable_notification=True, # reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("<b>Trying To Upload....π€</b>", message_for_progress_display, start_time)) if message.message_id != message_for_progress_display.message_id: await message_for_progress_display.delete() os.remove(local_file_name) else: try: message_for_progress_display = message if not edit_media: message_for_progress_display = await message.reply_text( "<b>Starting Upload π€</b>\n\n<b>File Name :</b> <code>{}</code>" .format(os.path.basename(local_file_name))) if local_file_name.upper().endswith(("MKV", "MP4", "WEBM")): metadata = extractMetadata(createParser(local_file_name)) duration = 0 if metadata.has("duration"): duration = metadata.get('duration').seconds width = 0 height = 0 thumb_image_path = None if os.path.exists(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) else: LOGGER.info("Taking Screenshot") thumb_image_path = await take_screen_shot( local_file_name, os.path.dirname(os.path.abspath(local_file_name)), math.floor(duration / 2)) # get the correct width, height, and duration for videos greater than 10MB if thumb_image_path is not None and os.path.isfile( thumb_image_path): metadata = extractMetadata(createParser(thumb_image_path)) if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") # ref: https://t.me/PyrogramChat/44663 # https://stackoverflow.com/a/21669827/4723940 Image.open(thumb_image_path).convert("RGB").save( thumb_image_path) img = Image.open(thumb_image_path) # https://stackoverflow.com/a/37631799/4723940 img.resize((320, height)) img.save(thumb_image_path, "JPEG") # https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#create-thumbnails # thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # send video if edit_media and message.photo: await asyncio.sleep(EDIT_SLEEP_TIME_OUT) sent_message = await message.edit_media( media=InputMediaVideo(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html", width=width, height=height, duration=duration, supports_streaming=True) # quote=True, ) else: sent_message = await message.reply_video( video=local_file_name, # quote=True, caption=caption_str, parse_mode="html", duration=duration, width=width, height=height, thumb=thumb, supports_streaming=True, disable_notification=True, # reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("<b>Trying To Upload....π€</b>", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) elif local_file_name.upper().endswith( ("MP3", "M4A", "M4B", "FLAC", "WAV")): metadata = extractMetadata(createParser(local_file_name)) duration = 0 title = "" artist = "" if metadata.has("duration"): duration = metadata.get('duration').seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") thumb_image_path = None if os.path.isfile(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # send audio if edit_media and message.photo: await asyncio.sleep(EDIT_SLEEP_TIME_OUT) sent_message = await message.edit_media( media=InputMediaAudio(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html", duration=duration, performer=artist, title=title) # quote=True, ) else: sent_message = await message.reply_audio( audio=local_file_name, # quote=True, caption=caption_str, parse_mode="html", duration=duration, performer=artist, title=title, thumb=thumb, disable_notification=True, # reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("<b>Trying To Upload....π€</b>", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) else: thumb_image_path = None if os.path.isfile(thumbnail_location): thumb_image_path = await copy_file( thumbnail_location, os.path.dirname(os.path.abspath(local_file_name))) # if a file, don't upload "thumb" # this "diff" is a major derp -_- πππ thumb = None if thumb_image_path is not None and os.path.isfile( thumb_image_path): thumb = thumb_image_path # # send document if edit_media and message.photo: sent_message = await message.edit_media( media=InputMediaDocument(media=local_file_name, thumb=thumb, caption=caption_str, parse_mode="html") # quote=True, ) else: sent_message = await message.reply_document( document=local_file_name, # quote=True, thumb=thumb, caption=caption_str, parse_mode="html", disable_notification=True, # reply_to_message_id=message.reply_to_message.message_id, progress=progress_for_pyrogram, progress_args=("<b>Trying To Upload....π€</b>", message_for_progress_display, start_time)) if thumb is not None: os.remove(thumb) except Exception as e: await message_for_progress_display.edit_text("**FAILED**\n" + str(e)) LOGGER.exception(e) else: if message.message_id != message_for_progress_display.message_id: await message_for_progress_display.delete() os.remove(local_file_name) return sent_message