Spaces:
Paused
Paused
| # MIT License | |
| # Copyright (c) 2022 Hash Minner | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE | |
| import os | |
| import time | |
| import json | |
| import asyncio | |
| import logging | |
| from opencc import OpenCC | |
| from pyrogram.types import Thumbnail | |
| from pyrogram import Client, filters | |
| from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton | |
| if bool(os.environ.get("WEBHOOK")): | |
| from Uploader.config import Config | |
| else: | |
| from sample_config import Config | |
| from Uploader.script import Translation | |
| from Uploader.functions.ran_text import random_char | |
| from Uploader.functions.display_progress import humanbytes | |
| from Uploader.functions.display_progress import humanbytes | |
| logging.basicConfig(level=logging.DEBUG, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| logging.getLogger("pyrogram").setLevel(logging.WARNING) | |
| s2tw = OpenCC('s2tw.json').convert | |
| async def echo(bot, update): | |
| logger.info(update.from_user) | |
| url = update.text | |
| youtube_dl_username = None | |
| youtube_dl_password = None | |
| file_name = None | |
| if "youtu.be" in url: | |
| return await update.reply_text( | |
| "**Choose Download type**", | |
| reply_markup=InlineKeyboardMarkup( | |
| [ | |
| [ | |
| InlineKeyboardButton( | |
| "Audio π΅", callback_data="ytdl_audio"), | |
| InlineKeyboardButton( | |
| "Video π¬", callback_data="ytdl_video") | |
| ] | |
| ] | |
| ), | |
| quote=True | |
| ) | |
| if "|" in url: | |
| url_parts = url.split("|") | |
| if len(url_parts) == 2: | |
| url = url_parts[0] | |
| file_name = url_parts[1] | |
| elif len(url_parts) == 4: | |
| url = url_parts[0] | |
| file_name = url_parts[1] | |
| youtube_dl_username = url_parts[2] | |
| youtube_dl_password = url_parts[3] | |
| else: | |
| for entity in update.entities: | |
| if entity.type == "text_link": | |
| url = entity.url | |
| elif entity.type == "url": | |
| o = entity.offset | |
| l = entity.length | |
| url = url[o:o + l] | |
| if url is not None: | |
| url = url.strip() | |
| if file_name is not None: | |
| file_name = file_name.strip() | |
| # https://stackoverflow.com/a/761825/4723940 | |
| if youtube_dl_username is not None: | |
| youtube_dl_username = youtube_dl_username.strip() | |
| if youtube_dl_password is not None: | |
| youtube_dl_password = youtube_dl_password.strip() | |
| logger.info(url) | |
| logger.info(file_name) | |
| else: | |
| for entity in update.entities: | |
| if entity.type == "text_link": | |
| url = entity.url | |
| elif entity.type == "url": | |
| o = entity.offset | |
| l = entity.length | |
| url = url[o:o + l] | |
| if Config.HTTP_PROXY != "": | |
| command_to_exec = [ | |
| "yt-dlp", | |
| "--no-warnings", | |
| "--allow-dynamic-mpd", | |
| "-j", | |
| url, | |
| "--proxy", Config.HTTP_PROXY | |
| ] | |
| else: | |
| command_to_exec = [ | |
| "yt-dlp", | |
| "--no-warnings", | |
| "--allow-dynamic-mpd", | |
| "-j", | |
| url | |
| ] | |
| if youtube_dl_username is not None: | |
| command_to_exec.append("--username") | |
| command_to_exec.append(youtube_dl_username) | |
| if youtube_dl_password is not None: | |
| command_to_exec.append("--password") | |
| command_to_exec.append(youtube_dl_password) | |
| logger.info(command_to_exec) | |
| chk = await bot.send_message( | |
| chat_id=update.chat.id, | |
| text='Proccesing your β', | |
| disable_web_page_preview=True, | |
| reply_to_message_id=update.id | |
| ) | |
| if update.from_user.id not in Config.AUTH_USERS: | |
| if str(update.from_user.id) in Config.ADL_BOT_RQ: | |
| current_time = time.time() | |
| previous_time = Config.ADL_BOT_RQ[str(update.from_user.id)] | |
| process_max_timeout = round(Config.PROCESS_MAX_TIMEOUT/60) | |
| present_time = round(Config.PROCESS_MAX_TIMEOUT - | |
| (current_time - previous_time)) | |
| Config.ADL_BOT_RQ[str(update.from_user.id)] = time.time() | |
| if round(current_time - previous_time) < Config.PROCESS_MAX_TIMEOUT: | |
| await bot.edit_message_text(chat_id=update.chat.id, text=Translation.FREE_USER_LIMIT_Q_SZE.format(process_max_timeout, present_time), disable_web_page_preview=True, message_id=chk.id) | |
| return | |
| else: | |
| Config.ADL_BOT_RQ[str(update.from_user.id)] = time.time() | |
| process = await asyncio.create_subprocess_exec( | |
| *command_to_exec, | |
| # stdout must a pipe to be accessible as process.stdout | |
| stdout=asyncio.subprocess.PIPE, | |
| stderr=asyncio.subprocess.PIPE, | |
| ) | |
| # Wait for the subprocess to finish | |
| stdout, stderr = await process.communicate() | |
| e_response = stderr.decode().strip() | |
| logger.info(e_response) | |
| t_response = stdout.decode().strip() | |
| # logger.info(t_response) | |
| # https://github.com/rg3/youtube-dl/issues/2630#issuecomment-38635239 | |
| if e_response and "nonnumeric port" not in e_response: | |
| # logger.warn("Status : FAIL", exc.returncode, exc.output) | |
| error_message = e_response.replace( | |
| "please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.", "") | |
| if "This video is only available for registered users." in error_message: | |
| error_message += Translation.SET_CUSTOM_USERNAME_PASSWORD | |
| await chk.delete() | |
| time.sleep(40.5) | |
| await bot.send_message( | |
| chat_id=update.chat.id, | |
| text=Translation.NO_VOID_FORMAT_FOUND.format(str(error_message)), | |
| reply_to_message_id=update.id, | |
| disable_web_page_preview=True | |
| ) | |
| return False | |
| if t_response: | |
| # logger.info(t_response) | |
| x_reponse = t_response | |
| if "\n" in x_reponse: | |
| x_reponse, _ = x_reponse.split("\n") | |
| response_json = json.loads(x_reponse) | |
| randem = random_char(5) | |
| save_ytdl_json_path = Config.DOWNLOAD_LOCATION + \ | |
| "/" + str(update.from_user.id) + f'{randem}' + ".json" | |
| with open(save_ytdl_json_path, "w", encoding="utf8") as outfile: | |
| json.dump(response_json, outfile, ensure_ascii=False) | |
| # logger.info(response_json) | |
| inline_keyboard = [] | |
| duration = None | |
| if "duration" in response_json: | |
| duration = response_json["duration"] | |
| if "formats" in response_json: | |
| for formats in response_json["formats"]: | |
| format_id = formats.get("format_id") | |
| format_string = formats.get("format_note") | |
| if format_string is None: | |
| format_string = formats.get("format") | |
| if "DASH" in format_string.upper(): | |
| continue | |
| format_ext = formats.get("ext") | |
| if formats.get('filesize'): | |
| size = formats['filesize'] | |
| elif formats.get('filesize_approx'): | |
| size = formats['filesize_approx'] | |
| else: | |
| size = 0 | |
| cb_string_video = "{}|{}|{}|{}".format( | |
| "video", format_id, format_ext, randem) | |
| cb_string_file = "{}|{}|{}|{}".format( | |
| "file", format_id, format_ext, randem) | |
| if format_string is not None and not "audio only" in format_string: | |
| ikeyboard = [ | |
| InlineKeyboardButton( | |
| "π¬ " + format_string + " " + format_ext + | |
| " " + humanbytes(size) + " ", | |
| callback_data=(cb_string_video).encode("UTF-8") | |
| ) | |
| ] | |
| else: | |
| # special weird case :\ | |
| ikeyboard = [ | |
| InlineKeyboardButton( | |
| "π¬ [" + | |
| "] ( " + | |
| humanbytes(size) + " )", | |
| callback_data=(cb_string_video).encode("UTF-8") | |
| ) | |
| ] | |
| inline_keyboard.append(ikeyboard) | |
| if duration is not None: | |
| cb_string_64 = "{}|{}|{}|{}".format( | |
| "audio", "64k", "mp3", randem) | |
| cb_string_128 = "{}|{}|{}|{}".format( | |
| "audio", "128k", "mp3", randem) | |
| cb_string = "{}|{}|{}|{}".format( | |
| "audio", "320k", "mp3", randem) | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "πΌ α΄α΄πΉ " + "(" + "64 α΄Κα΄s" + ")", callback_data=cb_string_64.encode("UTF-8")), | |
| InlineKeyboardButton( | |
| "πΌ α΄α΄πΉ " + "(" + "128 α΄Κα΄s" + ")", callback_data=cb_string_128.encode("UTF-8")) | |
| ]) | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "πΌ α΄α΄πΉ " + "(" + "320 α΄Κα΄s" + ")", callback_data=cb_string.encode("UTF-8")) | |
| ]) | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "β α΄Κα΄sα΄", callback_data='close') | |
| ]) | |
| else: | |
| format_id = response_json["format_id"] | |
| format_ext = response_json["ext"] | |
| cb_string_file = "{}|{}|{}|{}".format( | |
| "file", format_id, format_ext, randem) | |
| cb_string_video = "{}|{}|{}|{}".format( | |
| "video", format_id, format_ext, randem) | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "π¬ Video", | |
| callback_data=(cb_string_video).encode("UTF-8") | |
| ) | |
| ]) | |
| cb_string_file = "{}={}={}".format( | |
| "file", format_id, format_ext) | |
| cb_string_video = "{}={}={}".format( | |
| "video", format_id, format_ext) | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "π Document", | |
| callback_data=(cb_string_file).encode("UTF-8") | |
| ) | |
| ]) | |
| reply_markup = InlineKeyboardMarkup(inline_keyboard) | |
| await chk.delete() | |
| await bot.send_message( | |
| chat_id=update.chat.id, | |
| text=Translation.FORMAT_SELECTION.format( | |
| Thumbnail) + "\n" + Translation.SET_CUSTOM_USERNAME_PASSWORD, | |
| reply_markup=reply_markup, | |
| reply_to_message_id=update.id | |
| ) | |
| else: | |
| # fallback for nonnumeric port a.k.a seedbox.io | |
| inline_keyboard = [] | |
| cb_string_file = "{}={}={}".format( | |
| "file", "LFO", "NONE") | |
| cb_string_video = "{}={}={}".format( | |
| "video", "OFL", "ENON") | |
| inline_keyboard.append([ | |
| InlineKeyboardButton( | |
| "π¬ α΄α΄α΄ Ιͺα΄", | |
| callback_data=(cb_string_video).encode("UTF-8") | |
| ) | |
| ]) | |
| reply_markup = InlineKeyboardMarkup(inline_keyboard) | |
| await chk.delete(True) | |
| await bot.send_message( | |
| chat_id=update.chat.id, | |
| text=Translation.FORMAT_SELECTION, | |
| reply_markup=reply_markup, | |
| reply_to_message_id=update.id | |
| ) | |