async def upload(event: NewMessage.Event) -> None: """ Upload media to Telegram. **{prefix}upload (path)** or **{prefix}ul (path)** """ match = event.matches[0].group(3) target_files = [] if not match: await event.answer("`Uploaded the void.`") return match = match.strip().replace("\\", "/") if match else "" fmatch = pathlib.Path(match) dmatch = pathlib.Path(downloads / match) if "*" not in match: if fmatch.exists(): target_files.append(fmatch) elif dmatch.exists(): target_files.append(dmatch) if not target_files: for f in downloads.glob("*.*"): if f.match(match) and f.is_file(): target_files.append(f) # Un-comment this for recursive file fetching from the bot's root dir """for f in pathlib.Path('.').glob('**/*.*'): if f in target_files: continue if not f.match('*/__pycache__/*') and f.match(match) and f.is_file(): target_files.append(f)""" if not target_files: await event.answer("`Couldn't find what you were looking for.`") return files = "\n".join([f"`{await _get_file_name(f)}`" for f in target_files]) if len(target_files) > 1: await event.answer(f"**Found multiple files for {match}:**\n{files}") return for f in target_files: f = f.absolute() prog = ProgressCallback(event, filen=await _get_file_name(f, False)) attributes, mime_type = get_attributes(str(f)) ul = io.open(f, "rb") uploaded = await client.fast_upload_file( file=ul, progress_callback=prog.up_progress) ul.close() media = types.InputMediaUploadedDocument(file=uploaded, mime_type=mime_type, attributes=attributes, thumb=None) await client.send_file(event.chat_id, file=media, force_document=True, reply_to=event) await event.answer(f"`Successfully uploaded {files}.`")
async def url_handler(event: Event): client = event.client link = sources.get(event.chat_id, None) if not link: await event.reply("You must add source first!") return real_path = os.path.join("buffer", f"folder_{secrets.token_urlsafe(16)}") os.mkdir(real_path) await download_by_url(link, real_path) try: # Finding all downloaded videos in the current directory for path in [f for f in os.listdir(real_path) if os.path.isfile(os.path.join(real_path, f))]: path = os.path.join(real_path, path) # Cutting Magic cutted_paths = cut_the_butt(path, event.text) try: for cutted_path in cutted_paths: # Uploading file to telegram servers with telethon file = await client.upload_file( cutted_path ) # Getting attributes of the uploaded file try: attributes = get_attributes(cutted_path) except Exception as e: attributes = None # Make sure deletion of your message is not raising try: await client.send_file( event.chat_id, file, attributes=attributes[0], reply_to=event ) except: await client.send_file( event.chat_id, file, attributes=attributes[0] ) finally: # Finally remove downloaded file os.remove(path) for _path in cutted_paths: os.remove(_path) finally: # Finally delete empty folder os.rmdir(real_path)
async def fix_attributes(path, info_dict: dict, supports_streaming: bool = False, round_message: bool = False) -> list: """Evite múltiples instancias de un atributo.""" new_attributes = [] video = False audio = False uploader = info_dict.get("uploader", "Unknown artist") duration = int(info_dict.get("duration", 0)) suffix = path.suffix[1:] if supports_streaming and suffix != "mp4": supports_streaming = True attributes, mime_type = get_attributes(path) if suffix == "mp3": title = str( info_dict.get("title", info_dict.get("id", "Unknown title"))) audio = types.DocumentAttributeAudio(duration=duration, voice=None, title=title, performer=uploader) elif suffix == "mp4": width = int(info_dict.get("width", 0)) height = int(info_dict.get("height", 0)) for attr in attributes: if isinstance(attr, types.DocumentAttributeVideo): duration = duration or attr.duration width = width or attr.w height = height or attr.h break video = types.DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=round_message, supports_streaming=supports_streaming, ) if audio and isinstance(audio, types.DocumentAttributeAudio): new_attributes.append(audio) if video and isinstance(video, types.DocumentAttributeVideo): new_attributes.append(video) for attr in attributes: if (isinstance(attr, types.DocumentAttributeAudio) and not audio or not isinstance(attr, types.DocumentAttributeAudio) and not video or not isinstance(attr, types.DocumentAttributeAudio) and not isinstance(attr, types.DocumentAttributeVideo)): new_attributes.append(attr) return new_attributes, mime_type
async def upload(event: NewMessage.Event) -> None: """Upload media to Telegram.""" match = event.matches[0].group(3) target_files = [] if not match: await event.answer("__Uploaded the void?__") return match = match.strip().replace('\\', '/') if match else '' fmatch = pathlib.Path(match) if '*' in match and '/' not in match: for f in downloads.glob('*.*'): if f.match(match): target_files.append(f) elif '*' not in match and '/' in match: if fmatch.exists(): target_files.append(fmatch) elif (downloads / match).exists(): target_files.append(downloads / match) for f in pathlib.Path('.').glob('**/*.*'): if f in target_files: continue if not f.match('*/__pycache__/*') and f.match(match): target_files.append(f) if not target_files: await event.answer("__Couldn't find what you were looking for.__") return files = ', '.join([f.stem for f in target_files]) for f in target_files: f = f.resolve() prog = ProgressCallback(event, filen=f.stem) attributes, mime_type = get_attributes(str(f)) ul = io.open(f, 'rb') uploaded = await client.fast_upload_file( file=ul, progress_callback=prog.up_progress) ul.close() media = types.InputMediaUploadedDocument(file=uploaded, mime_type=mime_type, attributes=attributes, thumb=None) await client.send_file(event.chat_id, file=media, caption=str(f.parent / f.name), force_document=True, reply_to=event) await event.answer(f"__Successfully uploaded {files}.__")
async def fix_attributes(path, info_dict: dict, round_message: bool = False, supports_streaming: bool = False) -> list: """Avoid multiple instances of an attribute.""" new_attributes = [] video = False audio = False title = str(info_dict.get('title', info_dict.get('id', 'Unknown title'))) uploader = info_dict.get('uploader', 'Unknown artist') duration = int(info_dict.get('duration', 0)) suffix = path.suffix[1:] if supports_streaming and suffix != 'mp4': supports_streaming = False attributes, mime_type = get_attributes(path) if suffix in audioFormats: audio = types.DocumentAttributeAudio(duration, None, title, uploader) elif suffix in videoFormats: width = int(info_dict.get('width', 0)) height = int(info_dict.get('height', 0)) for attr in attributes: if isinstance(attr, types.DocumentAttributeVideo): duration = duration or attr.duration width = width or attr.w height = height or attr.h break video = types.DocumentAttributeVideo(duration, width, height, round_message, supports_streaming) if audio and isinstance(audio, types.DocumentAttributeAudio): new_attributes.append(audio) if video and isinstance(video, types.DocumentAttributeVideo): new_attributes.append(video) for attr in attributes: if isinstance(attr, types.DocumentAttributeAudio): if not audio: new_attributes.append(attr) elif isinstance(attr, types.DocumentAttributeVideo): if not video: new_attributes.append(attr) else: new_attributes.append(attr) return new_attributes, mime_type
async def _(event): if event.fwd_from: return mone = await event.edit("Processing ...") input_str = event.pattern_match.group(1) thumb = thumb_image_path if os.path.exists(thumb_image_path) else None if os.path.exists(input_str): start = datetime.now() c_time = time.time() with open(input_str, "rb") as out: res = await upload_file( event.client, out, progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task(progress(d, t, mone, c_time, "uploading"))) attributes, mime_type = utils.get_attributes(input_str, ) media = types.InputMediaUploadedDocument( file=res, mime_type=mime_type, attributes=attributes, thumb=thumb, ) await event.reply(file=media) # await bot.send_file( # event.chat_id, # input_str, # force_document=True, # supports_streaming=False, # allow_cache=False, # reply_to=event.message.id, # thumb=thumb, # progress_callback=lambda d, t: asyncio.get_event_loop().create_task( # progress(d, t, mone, c_time, "trying to upload") # ) # ) end = datetime.now() os.remove(input_str) ms = (end - start).seconds j = await event.reply(f"Uploaded in {ms} seconds.") await asyncio.sleep(2) await j.delete() else: k = await event.reply("404: File Not Found") await asyncio.sleep(2) await k.delete()
async def upload(path, event, udir_event, catflag=None): # sourcery no-metrics catflag = catflag or False reply_to_id = await reply_id(event) if os.path.isdir(path): await event.client.send_message( event.chat_id, f"**Folder : **`{str(path)}`", ) Files = os.listdir(path) Files = sortthings(Files, path) for file in Files: catpath = os.path.join(path, file) await upload(Path(catpath), event, udir_event) elif os.path.isfile(path): fname = os.path.basename(path) c_time = time.time() thumb = None if os.path.exists(thumb_image_path): thumb = thumb_image_path f = path.absolute() attributes, mime_type = get_attributes(str(f)) ul = io.open(f, "rb") uploaded = await event.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress( d, t, event, c_time, "trying to upload", file_name=fname)), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type=mime_type, attributes=attributes, force_file=catflag, thumb=await event.client.upload_file(thumb) if thumb else None, ) await event.client.send_file( event.chat_id, file=media, caption=f"**File Name : **`{fname}`", reply_to=reply_to_id, ) UPLOAD_.uploaded += 1
async def _(event): if event.fwd_from: return input_str = event.pattern_match.group(1) mone = await event.edit("Processing ...") if os.path.exists(input_str): start = datetime.now() # await event.edit("Processing ...") lst_of_files = sorted(get_lst_of_files(input_str, [])) logger.info(lst_of_files) u = 0 await event.edit("Found {} files. ".format(len(lst_of_files)) + "Uploading will start soon. " + "Please wait!") thumb = thumb_image_path if os.path.exists(thumb_image_path) else None for single_file in lst_of_files: if os.path.exists(single_file): # https://stackoverflow.com/a/678242/4723940 caption_rts = os.path.basename(single_file) force_document = True supports_streaming = False document_attributes = [] width = 0 height = 0 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") if single_file.upper().endswith(Config.TL_VID_STREAM_TYPES): metadata = extractMetadata(createParser(single_file)) duration = 0 if metadata.has("duration"): duration = metadata.get('duration').seconds document_attributes = [ DocumentAttributeVideo(duration=duration, w=width, h=height, round_message=False, supports_streaming=True) ] supports_streaming = True force_document = False if single_file.upper().endswith(Config.TL_MUS_STREAM_TYPES): metadata = extractMetadata(createParser(single_file)) duration = metadata.get( 'duration').seconds if metadata.has("duration") else 0 title = metadata.get("title") if metadata.has( "title") else "" artist = metadata.get("artist") if metadata.has( "artist") else "" document_attributes = [ DocumentAttributeAudio(duration=duration, voice=False, title=title, performer=artist, waveform=None) ] supports_streaming = True force_document = False try: c_time = time.time() with open(single_file, "rb") as out: res = await upload_file( event.client, out, progress_callback=lambda d, t: asyncio. get_event_loop().create_task( progress(d, t, mone, c_time, "uploading"))) attributes, mime_type = utils.get_attributes( single_file, ) media = types.InputMediaUploadedDocument( file=res, mime_type=mime_type, attributes=document_attributes, thumb=thumb, force_file=force_document, supports_streaming=supports_streaming) await event.reply(file=media) # await bot.send_file( # event.chat_id, # single_file, # caption=caption_rts, # force_document=force_document, # supports_streaming=supports_streaming, # allow_cache=False, # reply_to=event.message.id, # thumb=thumb, # attributes=document_attributes, # # progress_callback=lambda d, t: asyncio.get_event_loop().create_task( # # progress(d, t, event, c_time, "trying to upload") # # ) # ) except Exception as e: await bot.send_message(event.chat_id, "{} caused `{}`".format( caption_rts, str(e)), reply_to=event.message.id) # some media were having some issues continue os.remove(single_file) u += 1 # await event.edit("Uploaded {} / {} files.".format(u, len(lst_of_files))) # @ControllerBot was having issues, # if both edited_updates and update events come simultaneously. await asyncio.sleep(5) end = datetime.now() ms = (end - start).seconds await event.edit("Uploaded {} files in {} seconds.".format(u, ms)) else: await event.edit("404: Directory Not Found")
async def dl_manager(event: Union[Message, events.NewMessage.Event]): client = event.client commands, query = await async_find_commands(event) # Extracting urls from text urls = extract_url(query) # Making sure any amount of links are being # found and downloaded from for url in urls: # Make sure deletion of your message is not raising try: await event.edit(f"Downloading file(s)..") except: pass # Creating new random folder for each link # ( Many videos in one link possible aka playlists ) real_path = os.path.join("buffer", f"folder_{secrets.token_urlsafe(16)}") # Creating new folder directory os.mkdir(real_path) # Boolean to decide is this video or audio is_audio = "-audio" in commands or "-mp3" in commands is_document = "-f" in commands or "-d" in commands # TODO: Implement callback in chat # Async wrapper for smooth download-converting change async def smooth_switch(d, event): if d['status'] == 'finished': await event.edit("Downloading..\n(Progress: `finished`)") if is_audio: await asyncio.sleep(0.7) await event.edit("Converting to audio..") # Async wrapper for download progress async def downloading(d, event): if d['status']: if time.time() % 2 == 0: await event.client.send_message( "me", (f"{d['status']}, type:{type(d['status'])}")) # Gathering async functions in synchronous context # ( On finish downloading, before converting; ) # ( During downloading, progress bar. ) def sync_progress(d): asyncio.run_coroutine_threadsafe(smooth_switch(d, event), loop=client.loop) #asyncio.run_coroutine_threadsafe(downloading(d, event), loop=client.loop) # Download video, passing callback function here await download_by_url(url, real_path, is_audio=is_audio, callback=[sync_progress]) try: # Finding all downloaded videos in the current directory for path in [ f for f in os.listdir(real_path) if os.path.isfile(os.path.join(real_path, f)) ]: path = os.path.join(real_path, path) try: last_dt = datetime.datetime.now() diff = datetime.timedelta(seconds=2) first_time_update = True # On-progress upload asynchronous callback func async def on_progress_upload(sent_bytes, total_bytes): nonlocal last_dt, first_time_update perc = round((sent_bytes / total_bytes) * 100) curr = datetime.datetime.now() if first_time_update: first_time_update = False # Make sure deletion of your message is not raising try: await event.edit( f"Uploading..\n(Progress: `{perc}%`)") except: pass elif curr - last_dt >= diff: last_dt = curr # Make sure deletion of your message is not raising try: await event.edit( f"Uploading..\n(Progress: `{perc}%`)") except: pass # Uploading file to telegram servers with telethon file = await client.upload_file( path, progress_callback=on_progress_upload) # Make sure deletion of your message is not raising try: await event.edit(f"Uploading..\n(Progress: `Done`)" ) await asyncio.sleep(0.5) await event.edit(f"`Task has been completed.`") await asyncio.sleep(0.5) await event.delete() except: pass # Getting attributes of the uploaded file try: attributes = get_attributes(path) except Exception as e: attributes = None # Make sure deletion of your message is not raising try: await client.send_file(event.chat_id, file, attributes=attributes[0], reply_to=event, force_document=is_document) except: await client.send_file(event.chat_id, file, attributes=attributes[0], force_document=is_document) finally: # Finally remove downloaded file os.remove(path) finally: # Finally delete empty folder os.rmdir(real_path)
async def ytdl_download_callback(c_q: CallbackQuery): # sourcery no-metrics yt_code = (str(c_q.pattern_match.group(1).decode("UTF-8")) if c_q.pattern_match.group(1) is not None else None) choice_id = (str(c_q.pattern_match.group(2).decode("UTF-8")) if c_q.pattern_match.group(2) is not None else None) downtype = (str(c_q.pattern_match.group(3).decode("UTF-8")) if c_q.pattern_match.group(3) is not None else None) if str(choice_id).isdigit(): choice_id = int(choice_id) if choice_id == 0: await c_q.answer("🔄 Processing...", alert=False) await c_q.edit(buttons=(await download_button(yt_code))) return startTime = time() choice_str, disp_str = get_choice_by_id(choice_id, downtype) media_type = "Video" if downtype == "v" else "Audio" callback_continue = f"Downloading {media_type} Please Wait..." callback_continue += f"\n\nFormat Code : {disp_str}" await c_q.answer(callback_continue, alert=True) upload_msg = await c_q.client.send_message(BOTLOG_CHATID, "Uploading...") yt_url = BASE_YT_URL + yt_code await c_q.edit( f"<b>⬇️ Downloading {media_type} ....</b>\n\n🔗 <a href={yt_url}> <b>Link</b></a>\n🆔 <b>Format Code</b> : {disp_str}", parse_mode="html", ) if downtype == "v": retcode = await _tubeDl(url=yt_url, starttime=startTime, uid=choice_str) else: retcode = await _mp3Dl(url=yt_url, starttime=startTime, uid=choice_str) if retcode != 0: return await upload_msg.edit(str(retcode)) _fpath = "" thumb_pic = None for _path in glob.glob(os.path.join(Config.TEMP_DIR, str(startTime), "*")): if _path.lower().endswith((".jpg", ".png", ".webp")): thumb_pic = _path else: _fpath = _path if not _fpath: await edit_delete(upload_msg, "nothing found !") return if not thumb_pic and downtype == "v": thumb_pic = str(await pool.run_in_thread(download) (await get_ytthumb(yt_code))) attributes, mime_type = get_attributes(str(_fpath)) ul = io.open(Path(_fpath), "rb") uploaded = await c_q.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, c_q, startTime, "trying to upload", file_name=os.path.basename(Path(_fpath)), )), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type=mime_type, attributes=attributes, force_file=False, thumb=await c_q.client.upload_file(thumb_pic) if thumb_pic else None, ) uploaded_media = await c_q.client.send_file( BOTLOG_CHATID, file=media, caption= f"<b>File Name : </b><code>{os.path.basename(Path(_fpath))}</code>", parse_mode="html", ) await upload_msg.delete() await c_q.edit( text=f"📹 <a href={yt_url}><b>{os.path.basename(Path(_fpath))}</b></a>", file=uploaded_media.media, parse_mode="html", )
async def download_audio(event): """Para descargar audio de YouTube y muchos otros sitios.""" url = event.pattern_match.group(1) rmsg = await event.get_reply_message() if not url and rmsg: myString = rmsg.text url = re.search("(?P<url>https?://[^\s]+)", myString).group("url") if not url: return await edit_or_reply( event, "`¿Qué se supone que debo hacer? Coloca un enlace`") catevent = await edit_or_reply(event, "`Preparando la descarga...`") reply_to_id = await reply_id(event) try: vid_data = YoutubeDL({ "no-playlist": True }).extract_info(url, download=False) except ExtractorError: vid_data = {"Título": url, "Subido por": "Skueletor", "Formatos": []} startTime = time() retcode = await _mp3Dl(url=url, starttime=startTime, uid="320") if retcode != 0: return await event.edit(str(retcode)) _fpath = "" thumb_pic = None for _path in glob.glob(os.path.join(Config.TEMP_DIR, str(startTime), "*")): if _path.lower().endswith((".jpg", ".png", ".webp")): thumb_pic = _path else: _fpath = _path if not _fpath: return await edit_delete(catevent, "__Incapaz de subir el archivo__") await catevent.edit(f"`Preparando todo para subir video:`\ \n**{vid_data['Título']}**\ \nSubido por *{vid_data['uploader']}*") attributes, mime_type = get_attributes(str(_fpath)) ul = io.open(pathlib.Path(_fpath), "rb") if thumb_pic is None: thumb_pic = str(await pool.run_in_thread(download) (await get_ytthumb(get_yt_video_id(url)))) uploaded = await event.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, catevent, startTime, "Intentando subirlo", file_name=os.path.basename(pathlib.Path(_fpath)), )), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type=mime_type, attributes=attributes, force_file=False, thumb=await event.client.upload_file(thumb_pic) if thumb_pic else None, ) await event.client.send_file( event.chat_id, file=media, caption= f"<b>Nombre del archivo : </b><code>{vid_data.get('title', os.path.basename(pathlib.Path(_fpath)))}</code>", reply_to=reply_to_id, parse_mode="html", ) for _path in [_fpath, thumb_pic]: os.remove(_path) await catevent.delete()
def upload(self, file, file_name=None): entity = types.InputPeerSelf() # begin _file_to_media media = None file_handle = None # begin _upload_file file_id = helpers.generate_random_long() if not file_name and getattr(file, 'name', None): file_name = file.name if file_name is None: file_name = str(file_id) file = file.read() file_size = len(file) part_size = int(utils.get_appropriated_part_size(file_size) * 1024) is_large = file_size > 10 * 1024 * 1024 hash_md5 = hashlib.md5() if not is_large: hash_md5.update(file) part_count = (file_size + part_size - 1) // part_size with BytesIO(file) as stream: for part_index in range(part_count): part = stream.read(part_size) if is_large: request = functions.upload.SaveBigFilePartRequest( file_id, part_index, part_count, part) else: request = functions.upload.SaveFilePartRequest( file_id, part_index, part) result = self.client(request) yield float(round(100.0 * (part_index / part_count), 2)) if not result: raise RuntimeError( 'Failed to upload file part {}.'.format(part_index)) if is_large: file_handle = types.InputFileBig(file_id, part_count, file_name) else: file_handle = custom.InputSizedFile(file_id, part_count, file_name, md5=hash_md5, size=file_size) # end _upload_file attributes, mime_type = utils.get_attributes(file, force_document=True, voice_note=False, video_note=False) attributes[0].file_name = file_name media = types.InputMediaUploadedDocument(file=file_handle, mime_type=mime_type, attributes=attributes) # end _file_to_media request = functions.messages.SendMediaRequest(entity, media, reply_to_msg_id=None, message='', entities=[], reply_markup=None, silent=None) t = self.client(request) if type(t) != types.Updates: t = self.client.loop.run_until_complete(t) msg = self.client._get_response_message(request, t, entity) yield msg
async def _(event): if event.fwd_from: return input_str = event.pattern_match.group(1) reply_message = await event.get_reply_message() if reply_message is None: await event.edit("reply to a media to use the `nfc` operation.\nInspired by @FileConverterBot") return await event.edit("trying to download media file, to my local") try: start = datetime.now() c_time = time.time() downloaded_file_name = await borg.download_media( reply_message, Config.TMP_DOWNLOAD_DIRECTORY, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( slitu.progress(d, t, event, c_time, "trying to download") ) ) except Exception as e: # pylint:disable=C0103,W0703 await event.edit(str(e)) else: end = datetime.now() ms = (end - start).seconds await event.edit("Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms)) new_required_file_name = "" new_required_file_caption = "" command_to_run = [] voice_note = False supports_streaming = False if input_str == "voice": new_required_file_caption = "NLFC_" + str(round(time.time())) + ".opus" new_required_file_name = Config.TMP_DOWNLOAD_DIRECTORY + "/" + new_required_file_caption command_to_run = [ "ffmpeg", "-i", downloaded_file_name, "-map", "0:a", "-codec:a", "libopus", "-b:a", "100k", "-vbr", "on", new_required_file_name ] voice_note = True supports_streaming = True elif input_str == "mp3": new_required_file_caption = "NLFC_" + str(round(time.time())) + ".mp3" new_required_file_name = Config.TMP_DOWNLOAD_DIRECTORY + "/" + new_required_file_caption command_to_run = [ "ffmpeg", "-i", downloaded_file_name, "-vn", new_required_file_name ] voice_note = False supports_streaming = True else: await event.edit("not supported") os.remove(downloaded_file_name) return logger.info(command_to_run) t_response, e_response = await slitu.run_command(command_to_run) os.remove(downloaded_file_name) if os.path.exists(new_required_file_name): end_two = datetime.now() force_document = False file_handle = await event.client.upload_file( new_required_file_name, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( slitu.progress(d, t, event, c_time, "trying to upload") ) ) attributes, mime_type = get_attributes( new_required_file_name, mime_type=None, attributes=[], force_document=force_document, voice_note=voice_note, video_note=False, supports_streaming=supports_streaming, thumb=None ) os.remove(new_required_file_name) attributes = [DocumentAttributeAudio( duration=attributes[-1].duration, voice=voice_note, title=Config.NFC_TITLE, performer=Config.NFC_PERFORMER, waveform=attributes[-1].waveform )] media = InputMediaUploadedDocument( file=file_handle, mime_type=mime_type, attributes=attributes, thumb=None, force_file=force_document ) await event.reply( file=media ) ms_two = (end_two - end).seconds await event.edit(f"converted in {ms_two} seconds")
async def download_audio(event): """To download audio from YouTube and many other sites.""" url = event.pattern_match.group(1) rmsg = await event.get_reply_message() if not url and rmsg: myString = rmsg.text url = re.search("(?P<url>https?://[^\s]+)", myString).group("url") if not url: return await edit_or_reply(event, "`What I am Supposed to do? Give link`") catevent = await edit_or_reply(event, "`Preparing to download...`") reply_to_id = await reply_id(event) try: vid_data = YoutubeDL({ "no-playlist": True }).extract_info(url, download=False) except ExtractorError: vid_data = {"title": url, "uploader": "Catuserbot", "formats": []} startTime = time() retcode = await _mp3Dl(url=url, starttime=startTime, uid="320") if retcode != 0: return await event.edit(str(retcode)) _fpath = "" thumb_pic = None for _path in glob.glob(os.path.join(Config.TEMP_DIR, str(startTime), "*")): if _path.lower().endswith((".jpg", ".png", ".webp")): thumb_pic = _path else: _fpath = _path if not _fpath: return await edit_delete(catevent, "__Unable to upload file__") await catevent.edit(f"`Preparing to upload video:`\ \n**{vid_data['title']}**\ \nby *{vid_data['uploader']}*") attributes, mime_type = get_attributes(str(_fpath)) ul = io.open(pathlib.Path(_fpath), "rb") if thumb_pic is None: thumb_pic = str(await pool.run_in_thread(download) (await get_ytthumb(get_yt_video_id(url)))) uploaded = await event.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, catevent, startTime, "trying to upload", file_name=os.path.basename(pathlib.Path(_fpath)), )), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type=mime_type, attributes=attributes, force_file=False, thumb=await event.client.upload_file(thumb_pic) if thumb_pic else None, ) await event.client.send_file( event.chat_id, file=media, caption= f"<b>File Name : </b><code>{vid_data.get('title', os.path.basename(pathlib.Path(_fpath)))}</code>", reply_to=reply_to_id, parse_mode="html", ) for _path in [_fpath, thumb_pic]: os.remove(_path) await catevent.delete()
async def yt_dl(event): """Download videos from YouTube with their url in multiple formats.""" url = event.matches[0].group(1) fmt = event.matches[0].group(2) if not url: await event.answer("`.ytdl <url>` or `.ytdl <url> <format>`") return ffmpeg = await is_ffmpeg_there() params = copy.deepcopy(ydl_opts) if fmt: fmt = fmt.strip() if fmt == 'listformats': info = await extract_info( client.loop, concurrent.futures.ThreadPoolExecutor(), params, url ) if isinstance(info, dict): fmts = await list_formats(info) await event.answer(fmts) else: await event.answer(info) return elif fmt in audioFormats and ffmpeg: params.update(format='bestaudio') params['postprocessors'].append( { 'key': 'FFmpegExtractAudio', 'preferredcodec': fmt, 'preferredquality': '320', } ) elif fmt in videoFormats and ffmpeg: params.update(format='bestvideo') params['postprocessors'].append( { 'key': 'FFmpegVideoConvertor', 'preferedformat': fmt } ) else: params.update(format=fmt) if ffmpeg: params.update(key='FFmpegMetadata') if fmt in ['mp3', 'mp4', 'm4a']: params.update(writethumbnail=True) params['postprocessors'].append({'key': 'EmbedThumbnail'}) progress = ProgressHook(event) await event.answer("`Processing...`") params['progress_hooks'].append(progress.hook) output = await extract_info( loop=client.loop, executor=concurrent.futures.ThreadPoolExecutor(), ydl_opts=params, url=url, download=True ) warning = ( f"`WARNING: FFMPEG is not installed!` [FFMPEG install guide]({ffurl})" " `If you requested multiple formats, they won't be merged.`\n\n" ) if isinstance(output, str): result = warning + output if not ffmpeg else output await event.answer(result, link_preview=False) else: path, thumb, info = output title = info.get('title', info.get('id', 'Unknown title')) uploader = info.get('uploader', None) duration = int(info.get('duration', 0)) width = info.get('width', None) height = info.get('height', None) url = info.get('webpage_url', None) href = f"[{title}]({url})" text = success.format(href) result = warning + text if not ffmpeg else text progress_cb = ProgressCallback(event, filen=title) dl = io.open(path, 'rb') uploaded = await client.fast_upload_file(dl, progress_cb.up_progress) dl.close() attributes, mime_type = get_attributes(path) if path.suffix[1:] in audioFormats: attributes.append( types.DocumentAttributeAudio(duration, None, title, uploader) ) elif path.suffix[1:] in videoFormats: attributes.append( types.DocumentAttributeVideo(duration, width, height) ) media = types.InputMediaUploadedDocument( file=uploaded, mime_type=mime_type, attributes=attributes, thumb=await client.upload_file(thumb) if thumb else None ) await client.send_file( event.chat_id, media, caption=href, force_document=True ) if thumb: os.remove(thumb) await event.delete()
async def video_catfile(event): # sourcery no-metrics "To make circular video note." reply = await event.get_reply_message() args = event.pattern_match.group(1) catid = await reply_id(event) if not reply or not reply.media: return await edit_delete(event, "`Reply to supported media`") mediatype = media_type(reply) if mediatype == "Round Video": return await edit_delete( event, "__Do you think I am a dumb person😏? The replied media is already in round format,recheck._", ) if mediatype not in ["Photo", "Audio", "Voice", "Gif", "Sticker", "Video"]: return await edit_delete(event, "```Supported Media not found...```") flag = True catevent = await edit_or_reply(event, "`Converting to round format..........`") catfile = await reply.download_media(file="./temp/") if mediatype in ["Gif", "Video", "Sticker"]: if not catfile.endswith((".webp")): if catfile.endswith((".tgs")): hmm = await make_gif(catevent, catfile) os.rename(hmm, "./temp/circle.mp4") catfile = "./temp/circle.mp4" media_info = MediaInfo.parse(catfile) aspect_ratio = 1 for track in media_info.tracks: if track.track_type == "Video": aspect_ratio = track.display_aspect_ratio height = track.height width = track.width if aspect_ratio != 1: crop_by = width if (height > width) else height await _catutils.runcmd( f'ffmpeg -i {catfile} -vf "crop={crop_by}:{crop_by}" {PATH}' ) else: copyfile(catfile, PATH) if str(catfile) != str(PATH): os.remove(catfile) try: catthumb = await reply.download_media(thumb=-1) except Exception as e: LOGS.error(f"circle - {str(e)}") elif mediatype in ["Voice", "Audio"]: catthumb = None try: catthumb = await reply.download_media(thumb=-1) except Exception: catthumb = os.path.join("./temp", "thumb.jpg") await thumb_from_audio(catfile, catthumb) if catthumb is not None and not os.path.exists(catthumb): catthumb = os.path.join("./temp", "thumb.jpg") copyfile(thumb_loc, catthumb) if ( catthumb is not None and not os.path.exists(catthumb) and os.path.exists(thumb_loc) ): flag = False catthumb = os.path.join("./temp", "thumb.jpg") copyfile(thumb_loc, catthumb) if catthumb is not None and os.path.exists(catthumb): await _catutils.runcmd( f"""ffmpeg -loop 1 -i {catthumb} -i {catfile} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -vf \"scale=\'iw-mod (iw,2)\':\'ih-mod(ih,2)\',format=yuv420p\" -shortest -movflags +faststart {PATH}""" ) os.remove(catfile) else: os.remove(catfile) return await edit_delete( catevent, "`No thumb found to make it video note`", 5 ) if ( mediatype in [ "Voice", "Audio", "Gif", "Video", "Sticker", ] and not catfile.endswith((".webp")) ): if os.path.exists(PATH): c_time = time.time() attributes, mime_type = get_attributes(PATH) ul = io.open(PATH, "rb") uploaded = await event.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, catevent, c_time, "Uploading....") ), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type="video/mp4", attributes=[ types.DocumentAttributeVideo( duration=0, w=1, h=1, round_message=True, supports_streaming=True, ) ], force_file=False, thumb=await event.client.upload_file(catthumb) if catthumb else None, ) sandy = await event.client.send_file( event.chat_id, media, reply_to=catid, video_note=True, supports_streaming=True, ) if not args: await _catutils.unsavegif(event, sandy) os.remove(PATH) if flag: os.remove(catthumb) await catevent.delete() return data = reply.photo or reply.media.document img = io.BytesIO() await event.client.download_file(data, img) im = Image.open(img) w, h = im.size img = Image.new("RGBA", (w, h), (0, 0, 0, 0)) img.paste(im, (0, 0)) m = min(w, h) img = img.crop(((w - m) // 2, (h - m) // 2, (w + m) // 2, (h + m) // 2)) w, h = img.size mask = Image.new("L", (w, h), 0) draw = ImageDraw.Draw(mask) draw.ellipse((10, 10, w - 10, h - 10), fill=255) mask = mask.filter(ImageFilter.GaussianBlur(2)) img = ImageOps.fit(img, (w, h)) img.putalpha(mask) im = io.BytesIO() im.name = "cat.webp" img.save(im) im.seek(0) await event.client.send_file(event.chat_id, im, reply_to=catid) await catevent.delete()
async def _(event): if event.fwd_from: return mone = await event.edit("Processing ...") input_str = event.pattern_match.group(1) thumb = None file_name = input_str if os.path.exists(file_name): start = datetime.now() metadata = extractMetadata(createParser(file_name)) duration = 0 width = 0 height = 0 if metadata: if metadata.has("duration"): duration = metadata.get('duration').seconds 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") if os.path.exists(thumb_image_path): thumb = thumb_image_path else: thumb = await take_screen_shot(file_name, Config.TMP_DOWNLOAD_DIRECTORY, duration // 2) # Telegram only works with MP4 files # this is good, since with MKV files sent as streamable Telegram responds, # Bad Request: VIDEO_CONTENT_TYPE_INVALID c_time = time.time() try: start = datetime.now() with open(file_name, "rb") as out: res = await upload_file( event.client, out, progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, mone, c_time, "uploading as a stream"))) attributes, mime_type = utils.get_attributes(input_str, ) media = types.InputMediaUploadedDocument( file=res, mime_type=mime_type, attributes=[ DocumentAttributeVideo(duration=duration, w=width, h=height, round_message=False, supports_streaming=True) ], thumb=thumb, ) await event.reply(file=media) # await bot.send_file( # event.chat_id, # file_name, # thumb=thumb, # caption=input_str, # force_document=False, # allow_cache=False, # reply_to=event.message.id, # attributes=[ # DocumentAttributeVideo( # duration=duration, # w=width, # h=height, # round_message=False, # supports_streaming=True # ) # ], # progress_callback=lambda d, t: asyncio.get_event_loop().create_task( # progress(d, t, mone, c_time, "trying to upload") # ) # ) except Exception as e: await mone.edit(str(e)) else: end = datetime.now() os.remove(input_str) ms = (end - start).seconds await mone.edit(f"Uploaded in {ms} seconds.") else: await mone.edit("404: File Not Found")
async def upload_a_file(path,message,force_edit,database=None,thumb_path=None,user_msg=None): if get_val("EXPRESS_UPLOAD"): return await upload_single_file(path, message, force_edit, database, thumb_path, user_msg) queue = message.client.queue if database is not None: if database.get_cancel_status(message.chat_id,message.id): # add os remove here return None if not os.path.exists(path): return None if user_msg is None: user_msg = await message.get_reply_message() #todo improve this uploading ✔️ file_name = os.path.basename(path) caption_str = "" caption_str += "<code>" caption_str += file_name caption_str += "</code>" metadata = extractMetadata(createParser(path)) ometa = metadata if metadata is not None: # handle none for unknown metadata = metadata.exportDictionary() try: mime = metadata.get("Common").get("MIME type") except: mime = metadata.get("Metadata").get("MIME type") ftype = mime.split("/")[0] ftype = ftype.lower().strip() else: ftype = "unknown" #print(metadata) if not force_edit: data = "upcancel {} {} {}".format(message.chat_id,message.id,user_msg.sender_id) buts = [KeyboardButtonCallback("Cancel upload.",data.encode("UTF-8"))] msg = await message.reply("📤 **Uploading:** `{}`".format(file_name),buttons=buts) else: msg = message uploader_id = None if queue is not None: torlog.info(f"Waiting for the worker here for {file_name}") msg = await msg.edit(f"{msg.text}\n⌛ Waiting for a uploaders to get free... ") uploader_id = await queue.get() torlog.info(f"Waiting over for the worker here for {file_name} aquired worker {uploader_id}") out_msg = None start_time = time.time() tout = get_val("EDIT_SLEEP_SECS") opath = path if user_msg is not None: dis_thumb = user_db.get_var("DISABLE_THUMBNAIL", user_msg.sender_id) if dis_thumb is False or dis_thumb is None: thumb_path = user_db.get_thumbnail(user_msg.sender_id) if not thumb_path: thumb_path = None try: if get_val("FAST_UPLOAD"): torlog.info("Fast upload is enabled") with open(path,"rb") as filee: path = await upload_file(message.client,filee,file_name, lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database) ) if user_msg is not None: force_docs = user_db.get_var("FORCE_DOCUMENTS",user_msg.sender_id) else: force_docs = None if force_docs is None: force_docs = get_val("FORCE_DOCUMENTS") if message.media and force_edit: out_msg = await msg.edit( file=path, text=caption_str ) else: if ftype == "video" and not force_docs: try: if thumb_path is not None: thumb = thumb_path else: thumb = await thumb_manage.get_thumbnail(opath) except: thumb = None torlog.exception("Error in thumb") try: attrs, _ = get_attributes(opath,supports_streaming=True) out_msg = await msg.client.send_file( msg.to_id, file=path, parse_mode="html", thumb=thumb, caption=caption_str, reply_to=message.id, supports_streaming=True, progress_callback=lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database), attributes=attrs ) except VideoContentTypeInvalidError: attrs, _ = get_attributes(opath,force_document=True) torlog.warning("Streamable file send failed fallback to document.") out_msg = await msg.client.send_file( msg.to_id, file=path, parse_mode="html", caption=caption_str, thumb=thumb, reply_to=message.id, force_document=True, progress_callback=lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database), attributes=attrs ) except Exception: torlog.error("Error:- {}".format(traceback.format_exc())) elif ftype == "audio" and not force_docs: # not sure about this if attrs, _ = get_attributes(opath) out_msg = await msg.client.send_file( msg.to_id, file=path, parse_mode="html", caption=caption_str, reply_to=message.id, progress_callback=lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database), attributes=attrs ) else: if force_docs: attrs, _ = get_attributes(opath,force_document=True) out_msg = await msg.client.send_file( msg.to_id, file=path, parse_mode="html", caption=caption_str, reply_to=message.id, force_document=True, progress_callback=lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database), attributes=attrs, thumb=thumb_path ) else: attrs, _ = get_attributes(opath) out_msg = await msg.client.send_file( msg.to_id, file=path, parse_mode="html", caption=caption_str, reply_to=message.id, progress_callback=lambda c,t: progress(c,t,msg,file_name,start_time,tout,message,database), attributes=attrs, thumb=thumb_path ) except Exception as e: if str(e).find("cancel") != -1: torlog.info("Canceled an upload lol") await msg.edit(f"Failed to upload {e}") else: torlog.exception("In Tele Upload") await msg.edit(f"Failed to upload {e}") finally: if queue is not None: await queue.put(uploader_id) torlog.info(f"Freed uploader with id {uploader_id}") if out_msg is None: return None if out_msg.id != msg.id: await msg.delete() return out_msg
async def yt_dl(event): """Download videos from YouTube with their url in multiple formats.""" url = event.matches[0].group(1) fmt = event.matches[0].group(2) if not url: await event.answer("`.ytdl <url>` or `.ytdl <url> <format>`") return ffmpeg = await is_ffmpeg_there() params = ydl_opts.copy() if fmt: fmt = fmt.strip() if fmt == 'listformats': info = await extract_info( client.loop, concurrent.futures.ThreadPoolExecutor(), params, url ) if isinstance(info, dict): fmts = await list_formats(info) await event.answer(fmts) else: await event.answer(info) return elif fmt in audioFormats and ffmpeg: params.update(format='bestaudio') params['postprocessors'].append( { 'key': 'FFmpegExtractAudio', 'preferredcodec': fmt, 'preferredquality': '320', } ) elif fmt in videoFormats and ffmpeg: params.update(format='bestvideo') params['postprocessors'].append( { 'key': 'FFmpegVideoConvertor', 'preferedformat': fmt } ) else: params.update(format=fmt) if ffmpeg: params.update(key='FFmpegMetadata') if fmt in ['mp3', 'mp4', 'm4a']: params.update(writethumbnail=True) params['postprocessors'].append({'key': 'EmbedThumbnail'}) await event.answer("`Processing...`") output = await extract_info( client.loop, concurrent.futures.ThreadPoolExecutor(), params, url, download=True ) warning = ( f"`WARNING: FFMPEG is not installed!` [FFMPEG install guide]({ffurl})" " `If you requested multiple formats, they won't be merged.`\n\n" ) if isinstance(output, str): result = warning + output if not ffmpeg else output await event.answer(result, link_preview=False) else: title, link, path = output huh = f"[{title}]({link})" text = success.format(huh) result = warning + text if not ffmpeg else text await event.answer(f"`Uploading` {huh}`...`", link_preview=False) dl = io.open(path, 'rb') uploaded = await client.fast_upload_file(dl, upload_progress) dl.close() attributes, _ = get_attributes(path) await client.send_file( event.chat_id, uploaded, attributes=attributes, force_document=True, reply_to=event ) await event.answer( result, link_preview=False, log=("YTDL", f"Successfully downloaded {huh}!") )