From 110bfd4cdd7680bf54e508281876ecb1a6029b27 Mon Sep 17 00:00:00 2001 From: Hafitz Setya <71178188+breakdowns@users.noreply.github.com> Date: Mon, 15 Mar 2021 09:25:23 +0700 Subject: [PATCH] v4.1 (#26) - Added Custom Filename support --- README.md | 1 + .../download_utils/aria2_download.py | 6 ++-- .../download_utils/telegram_downloader.py | 10 ++++-- .../youtube_dl_download_helper.py | 16 ++++++--- .../mirror_utils/upload_utils/gdriveTools.py | 16 ++++----- bot/modules/mirror.py | 21 +++++++----- bot/modules/watch.py | 34 +++++++++++++------ 7 files changed, 67 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 5478f25..f1697d9 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Fork this repo, than upload credentials.json and token.pickle to your forks - Shell and Executor - Index Link support - Shortener support +- Custom Filename - Custom Buttons - Speedtest with picture results - Extract these filetypes and uploads to google drive diff --git a/bot/helper/mirror_utils/download_utils/aria2_download.py b/bot/helper/mirror_utils/download_utils/aria2_download.py index f1171fe..56f398f 100644 --- a/bot/helper/mirror_utils/download_utils/aria2_download.py +++ b/bot/helper/mirror_utils/download_utils/aria2_download.py @@ -83,11 +83,11 @@ class AriaDownloadHelper(DownloadHelper): on_download_complete=self.__onDownloadComplete) - def add_download(self, link: str, path,listener): + def add_download(self, link: str, path, listener, filename): if is_magnet(link): - download = aria2.add_magnet(link, {'dir': path}) + download = aria2.add_magnet(link, {'dir': path, 'out': filename}) else: - download = aria2.add_uris([link], {'dir': path}) + download = aria2.add_uris([link], {'dir': path, 'out': filename}) if download.error_message: # no need to proceed further at this point listener.onDownloadError(download.error_message) return diff --git a/bot/helper/mirror_utils/download_utils/telegram_downloader.py b/bot/helper/mirror_utils/download_utils/telegram_downloader.py index 259466b..709cf84 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -84,7 +84,7 @@ class TelegramDownloadHelper(DownloadHelper): if not self.__is_cancelled: self.__onDownloadError('Internal error occurred') - def add_download(self, message, path): + def add_download(self, message, path, filename): _message = self._bot.get_messages(message.chat.id, message.message_id) media = None media_array = [_message.document, _message.video, _message.audio] @@ -96,9 +96,13 @@ class TelegramDownloadHelper(DownloadHelper): with global_lock: # For avoiding locking the thread lock for long time unnecessarily download = media.file_id not in GLOBAL_GID - + if filename == "": + name = media.file_name + else: + name = filename + path = path + name if download: - self.__onDownloadStart(media.file_name, media.file_size, media.file_id) + self.__onDownloadStart(name, media.file_size, media.file_id) LOGGER.info(f'Downloading telegram file with id: {media.file_id}') threading.Thread(target=self.__download, args=(_message, path)).start() else: diff --git a/bot/helper/mirror_utils/download_utils/youtube_dl_download_helper.py b/bot/helper/mirror_utils/download_utils/youtube_dl_download_helper.py index cf517b9..e4f8748 100644 --- a/bot/helper/mirror_utils/download_utils/youtube_dl_download_helper.py +++ b/bot/helper/mirror_utils/download_utils/youtube_dl_download_helper.py @@ -19,7 +19,10 @@ class MyLogger: # Hack to fix changing changing extension match = re.search(r'.ffmpeg..Merging formats into..(.*?).$', msg) if match and not self.obj.is_playlist: - self.obj.name = match.group(1) + newname = match.group(1) + newname = newname.split("/") + newname = newname[-1] + self.obj.name = newname @staticmethod def warning(msg): @@ -98,14 +101,17 @@ class YoutubeDLHelper(DownloadHelper): def onDownloadError(self, error): self.__listener.onDownloadError(error) - def extractMetaData(self, link, qual): + def extractMetaData(self, link, qual, name): if 'hotstar' or 'sonyliv' in link: self.opts['geo_bypass_country'] = 'IN' with YoutubeDL(self.opts) as ydl: try: result = ydl.extract_info(link, download=False) - name = ydl.prepare_filename(result) + if name == "": + name = ydl.prepare_filename(result) + else: + name = name # noobway hack for changing extension after converting to mp3 if qual == "audio": name = name.replace(".mp4", ".mp3").replace(".webm", ".mp3") @@ -144,12 +150,12 @@ class YoutubeDLHelper(DownloadHelper): LOGGER.info("Download Cancelled by User!") self.onDownloadError("Download Cancelled by User!") - def add_download(self, link, path, qual): + def add_download(self, link, path, qual, name): pattern = '^.*(youtu\.be\/|youtube.com\/)(playlist?)' if re.match(pattern, link): self.opts['ignoreerrors'] = True self.__onDownloadStart() - self.extractMetaData(link, qual) + self.extractMetaData(link, qual, name) LOGGER.info(f"Downloading with YT-DL: {link}") self.__gid = f"{self.vid_id}{self.__listener.uid}" if qual == "audio": diff --git a/bot/helper/mirror_utils/upload_utils/gdriveTools.py b/bot/helper/mirror_utils/upload_utils/gdriveTools.py index bd43a32..aea293e 100644 --- a/bot/helper/mirror_utils/upload_utils/gdriveTools.py +++ b/bot/helper/mirror_utils/upload_utils/gdriveTools.py @@ -322,16 +322,16 @@ class GoogleDriveHelper: buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, durl)).text - buttons.buildbutton("Drive Link", surl) + buttons.buildbutton("⚡Drive Link⚡", surl) else: - buttons.buildbutton("Drive Link", durl) + buttons.buildbutton("⚡Drive Link⚡", durl) if INDEX_URL is not None: url = requests.utils.requote_uri(f'{INDEX_URL}/{meta.get("name")}/') if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, url)).text - buttons.buildbutton("Index Link", siurl) + buttons.buildbutton("🔥Index Link🔥", siurl) else: - buttons.buildbutton("Index Link", url) + buttons.buildbutton("🔥Index Link🔥", url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: @@ -345,9 +345,9 @@ class GoogleDriveHelper: buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, durl)).text - buttons.buildbutton("Drive Link ", surl) + buttons.buildbutton("⚡Drive Link⚡", surl) else: - buttons.buildbutton("Drive Link", durl) + buttons.buildbutton("⚡Drive Link⚡", durl) try: msg += f'\nSize: {get_readable_file_size(int(meta.get("size")))}' except TypeError: @@ -356,9 +356,9 @@ class GoogleDriveHelper: url = requests.utils.requote_uri(f'{INDEX_URL}/{file.get("name")}') if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, url)).text - buttons.buildbutton("Index Link", siurl) + buttons.buildbutton("🔥Index Link🔥", siurl) else: - buttons.buildbutton("Index Link", url) + buttons.buildbutton("🔥Index Link🔥", url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py index bbecd4f..5e530bf 100644 --- a/bot/modules/mirror.py +++ b/bot/modules/mirror.py @@ -143,9 +143,9 @@ class MirrorListener(listeners.MirrorListeners): buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, link)).text - buttons.buildbutton("Drive Link", surl) + buttons.buildbutton("⚡Drive Link⚡", surl) else: - buttons.buildbutton("Drive Link", link) + buttons.buildbutton("⚡Drive Link⚡", link) LOGGER.info(f'Done Uploading {download_dict[self.uid].name()}') if INDEX_URL is not None: share_url = requests.utils.requote_uri(f'{INDEX_URL}/{download_dict[self.uid].name()}') @@ -153,9 +153,9 @@ class MirrorListener(listeners.MirrorListeners): share_url += '/' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get('https://{}/api?api={}&url={}&format=text'.format(SHORTENER, SHORTENER_API, share_url)).text - buttons.buildbutton("Index Link", siurl) + buttons.buildbutton("🔥Index Link🔥", siurl) else: - buttons.buildbutton("Index Link", share_url) + buttons.buildbutton("🔥Index Link🔥", share_url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: @@ -197,10 +197,15 @@ class MirrorListener(listeners.MirrorListeners): def _mirror(bot, update, isTar=False, extract=False): message_args = update.message.text.split(' ') + name_args = update.message.text.split('|') try: link = message_args[1] except IndexError: link = '' + try: + name = name_args[1] + except IndexError: + name = '' LOGGER.info(link) link = link.strip() reply_to = update.message.reply_to_message @@ -213,12 +218,12 @@ def _mirror(bot, update, isTar=False, extract=False): file = i break - if len(link) == 0: + if not bot_utils.is_url(link) and not bot_utils.is_magnet(link) or len(link) == 0: if file is not None: if file.mime_type != "application/x-bittorrent": listener = MirrorListener(bot, update, isTar, tag, extract) tg_downloader = TelegramDownloadHelper(listener) - tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/') + tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/', name) sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) @@ -245,10 +250,10 @@ def _mirror(bot, update, isTar=False, extract=False): sendMessage("Mega links are blocked bcoz mega downloading is too much unstable and buggy. mega support will be added back after fix", bot, update) else: mega_dl = MegaDownloadHelper() - mega_dl.add_download(link, f'{DOWNLOAD_DIR}/{listener.uid}/', listener) + mega_dl.add_download(link, f'{DOWNLOAD_DIR}/{listener.uid}/', listener, name) sendStatusMessage(update, bot) else: - ariaDlManager.add_download(link, f'{DOWNLOAD_DIR}/{listener.uid}/', listener) + ariaDlManager.add_download(link, f'{DOWNLOAD_DIR}/{listener.uid}/', listener, name) sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) diff --git a/bot/modules/watch.py b/bot/modules/watch.py index a2ec2d7..93edcd4 100644 --- a/bot/modules/watch.py +++ b/bot/modules/watch.py @@ -10,20 +10,36 @@ from bot.helper.telegram_helper.filters import CustomFilters import threading -def _watch(bot: Bot, update: Update, args: list, isTar=False): +def _watch(bot: Bot, update, isTar=False): + mssg = update.message.text + message_args = mssg.split(' ') + name_args = mssg.split('|') try: - link = args[0] + link = message_args[1] except IndexError: - msg = f"/{BotCommands.WatchCommand} [yt_dl supported link] [quality] to mirror with youtube_dl.\n\n" - msg += "Example of quality :- audio, 144, 360, 720, 1080.\nNote :- Quality is optional" + msg = f"/{BotCommands.WatchCommand} [yt_dl supported link] [quality] |[CustomName] to mirror with youtube_dl.\n\n" + msg += "Note :- Quality and custom name are optional\n\nExample of quality :- audio, 144, 240, 360, 480, 720, 1080, 2160." + msg += "\n\nIf you want to use custom filename, plz enter it after |" + msg += f"\n\nExample :-\n/{BotCommands.WatchCommand} https://youtu.be/Pk_TthHfLeE 720 |Slam\n\n" + msg += "This file will be downloaded in 720p quality and it's name will be Slam" sendMessage(msg, bot, update) return try: - qual = args[1] + if "|" in mssg: + mssg = mssg.split("|") + qual = mssg[0].split(" ")[2] + if qual == "": + raise IndexError + else: + qual = message_args[2] if qual != "audio": qual = f'bestvideo[height<={qual}]+bestaudio/best[height<={qual}]' except IndexError: qual = "bestvideo+bestaudio/best" + try: + name = name_args[1] + except IndexError: + name = "" reply_to = update.message.reply_to_message if reply_to is not None: tag = reply_to.from_user.username @@ -32,7 +48,7 @@ def _watch(bot: Bot, update: Update, args: list, isTar=False): listener = MirrorListener(bot, update, isTar, tag) ydl = YoutubeDLHelper(listener) - threading.Thread(target=ydl.add_download,args=(link, f'{DOWNLOAD_DIR}{listener.uid}', qual)).start() + threading.Thread(target=ydl.add_download,args=(link, f'{DOWNLOAD_DIR}{listener.uid}', qual, name)).start() sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) @@ -40,18 +56,16 @@ def _watch(bot: Bot, update: Update, args: list, isTar=False): @run_async def watchTar(update, context): - _watch(context.bot, update, context.args, True) + _watch(context.bot, update, True) def watch(update, context): - _watch(context.bot, update, context.args) + _watch(context.bot, update) mirror_handler = CommandHandler(BotCommands.WatchCommand, watch, - pass_args=True, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) tar_mirror_handler = CommandHandler(BotCommands.TarWatchCommand, watchTar, - pass_args=True, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler)