Example #1
0
async def download_coroutine(bot, session, url, file_name, chat_id, message_id, start):
    display_message = ""
    async with session.get(url, timeout=PROCESS_MAX_TIMEOUT) as response:
        total_length = int(response.headers["Content-Length"])
        content_type = response.headers["Content-Type"]
        if "text" in content_type and total_length < 500:
            return await response.release()
        await bot.edit_message_text(
            chat_id,
            message_id,
            text="""Initiating Download
URL: {}
File Size: {}""".format(url, humanbytes(total_length))
        )
        with open(file_name, "wb") as f_handle:
            downloaded = 0
            while True:
                chunk = await response.content.read(CHUNK_SIZE)
                if not chunk:
                    break
                f_handle.write(chunk)
                downloaded += CHUNK_SIZE
                now = time.time()
                diff = now - start
                if round(diff % 5.00) == 0 or downloaded == total_length:
                    percentage = downloaded * 100 / total_length
                    speed = downloaded / diff
                    elapsed_time = round(diff) * 1000
                    time_to_completion = round(
                        (total_length - downloaded) / speed) * 1000
                    estimated_total_time = elapsed_time + time_to_completion
                    try:
                        current_message = """**Download Status**
URL: {}
File Size: {}
Downloaded: {}
ETA: {}

©️ @AnyDLBot""".format(
                            url,
                            humanbytes(total_length),
                            humanbytes(downloaded),
                            TimeFormatter(estimated_total_time)
                        )
                        if current_message != display_message:
                            await bot.edit_message_text(
                                chat_id,
                                message_id,
                                text=current_message
                            )
                            display_message = current_message
                    except Exception as e:
                        LOGGER.info(str(e))
        return await response.release()
Example #2
0
def DownLoadFile(
    url,
    file_name,
    chunk_size,
    client,
    ud_type,
    message_id,
    chat_id
):
    if os.path.exists(file_name):
        os.remove(file_name)
    if not url:
        return file_name
    r = requests.get(url, allow_redirects=True, stream=True)
    # https://stackoverflow.com/a/47342052/4723940
    total_size = int(r.headers.get("content-length", 0))
    downloaded_size = 0
    with open(file_name, 'wb') as fd:
        for chunk in r.iter_content(chunk_size=chunk_size):
            if chunk:
                fd.write(chunk)
                downloaded_size += chunk_size
            if client is not None:
                if ((total_size // downloaded_size) % 5) == 0:
                    time.sleep(0.3)
                    try:
                        client.edit_message_text(
                            chat_id,
                            message_id,
                            text="{}: {} of {}".format(
                                ud_type,
                                humanbytes(downloaded_size),
                                humanbytes(total_size)
                            )
                        )
                    except:
                        pass
    return file_name
Example #3
0
async def echo(bot, update):
    if update.from_user.id not in AUTH_USERS:
        await update.delete()
        return
    # LOGGER.info(update)
    # await bot.send_chat_action(
    #     chat_id=update.chat.id,
    #     action="typing"
    # )
    LOGGER.info(update.from_user)
    url = update.text
    youtube_dl_username = None
    youtube_dl_password = None
    file_name = None
    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 HTTP_PROXY is not None:
        command_to_exec = [
            "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
            "-j", url, "--proxy", HTTP_PROXY
        ]
    else:
        command_to_exec = [
            "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
            "-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)
    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 update.reply_text(text=Translation.NO_VOID_FORMAT_FOUND.format(
            str(error_message)),
                                quote=True,
                                parse_mode="html",
                                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)
        save_ytdl_json_path = DOWNLOAD_LOCATION + \
            "/" + str(update.from_user.id) + ".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")
                format_ext = formats.get("ext")
                approx_file_size = ""
                if "filesize" in formats:
                    approx_file_size = humanbytes(formats["filesize"])
                cb_string_video = "{}|{}|{}".format("video", format_id,
                                                    format_ext)
                cb_string_file = "{}|{}|{}".format("file", format_id,
                                                   format_ext)
                if format_string is not None and not "audio only" in format_string:
                    ikeyboard = [
                        InlineKeyboardButton(
                            "S " + format_string + " video " +
                            approx_file_size + " ",
                            callback_data=(cb_string_video).encode("UTF-8")),
                        InlineKeyboardButton(
                            "D " + format_ext + " " + approx_file_size + " ",
                            callback_data=(cb_string_file).encode("UTF-8"))
                    ]
                    """if duration is not None:
                        cb_string_video_message = "{}|{}|{}".format(
                            "vm", format_id, format_ext)
                        ikeyboard.append(
                            pyrogram.InlineKeyboardButton(
                                "VM",
                                callback_data=(
                                    cb_string_video_message).encode("UTF-8")
                            )
                        )"""
                else:
                    # special weird case :\
                    ikeyboard = [
                        InlineKeyboardButton(
                            "SVideo [" + "] ( " + approx_file_size + " )",
                            callback_data=(cb_string_video).encode("UTF-8")),
                        InlineKeyboardButton(
                            "DFile [" + "] ( " + approx_file_size + " )",
                            callback_data=(cb_string_file).encode("UTF-8"))
                    ]
                inline_keyboard.append(ikeyboard)
            if duration is not None:
                cb_string_64 = "{}|{}|{}".format("audio", "64k", "mp3")
                cb_string_128 = "{}|{}|{}".format("audio", "128k", "mp3")
                cb_string = "{}|{}|{}".format("audio", "320k", "mp3")
                inline_keyboard.append([
                    InlineKeyboardButton(
                        "MP3 " + "(" + "64 kbps" + ")",
                        callback_data=cb_string_64.encode("UTF-8")),
                    InlineKeyboardButton(
                        "MP3 " + "(" + "128 kbps" + ")",
                        callback_data=cb_string_128.encode("UTF-8"))
                ])
                inline_keyboard.append([
                    InlineKeyboardButton(
                        "MP3 " + "(" + "320 kbps" + ")",
                        callback_data=cb_string.encode("UTF-8"))
                ])
        else:
            format_id = response_json["format_id"]
            format_ext = response_json["ext"]
            cb_string_file = "{}|{}|{}".format("file", format_id, format_ext)
            cb_string_video = "{}|{}|{}".format("video", format_id, format_ext)
            inline_keyboard.append([
                InlineKeyboardButton(
                    "SVideo", callback_data=(cb_string_video).encode("UTF-8")),
                InlineKeyboardButton(
                    "DFile", callback_data=(cb_string_file).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(
                    "video", callback_data=(cb_string_video).encode("UTF-8")),
                InlineKeyboardButton(
                    "file", callback_data=(cb_string_file).encode("UTF-8"))
            ])
        reply_markup = InlineKeyboardMarkup(inline_keyboard)
        # logger.info(reply_markup)
        thumbnail = DEF_THUMB_NAIL_VID_S
        thumbnail_image = DEF_THUMB_NAIL_VID_S
        if "thumbnail" in response_json:
            if response_json["thumbnail"] is not None:
                thumbnail = response_json["thumbnail"]
                thumbnail_image = response_json["thumbnail"]
        thumb_image_path = DownLoadFile(
            thumbnail_image,
            DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg",
            128,
            None,  # bot,
            Translation.DOWNLOAD_START,
            update.message_id,
            update.chat.id)
        await update.reply_photo(
            photo=thumb_image_path,
            quote=True,
            caption=Translation.FORMAT_SELECTION.format(thumbnail) + "\n" +
            Translation.SET_CUSTOM_USERNAME_PASSWORD,
            reply_markup=reply_markup,
            parse_mode="html")
    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(
                "SVideo", callback_data=(cb_string_video).encode("UTF-8")),
            InlineKeyboardButton(
                "DFile", callback_data=(cb_string_file).encode("UTF-8"))
        ])
        reply_markup = InlineKeyboardMarkup(inline_keyboard)
        await update.reply_photo(
            photo=DEF_THUMB_NAIL_VID_S,
            quote=True,
            caption=Translation.FORMAT_SELECTION.format(""),
            reply_markup=reply_markup,
            parse_mode="html",
            reply_to_message_id=update.message_id)
async def youtube_dl_call_back(bot, update):
    cb_data = update.data
    # youtube_dl extractors
    tg_send_type, youtube_dl_format, youtube_dl_ext = cb_data.split("|")
    thumb_image_path = DOWNLOAD_LOCATION + \
        "/" + str(update.from_user.id) + ".jpg"
    save_ytdl_json_path = DOWNLOAD_LOCATION + \
        "/" + str(update.from_user.id) + ".json"
    try:
        with open(save_ytdl_json_path, "r", encoding="utf8") as f:
            response_json = json.load(f)
    except FileNotFoundError:
        await update.message.delete()
        return False

    youtube_dl_url, \
        custom_file_name, \
        youtube_dl_username, \
        youtube_dl_password = get_link(
            update.message.reply_to_message
        )
    if not custom_file_name:
        custom_file_name = str(response_json.get("title")) + \
            "_" + youtube_dl_format + "." + youtube_dl_ext
    await update.message.edit_caption(caption=Translation.DOWNLOAD_START)
    description = Translation.CUSTOM_CAPTION_UL_FILE
    if "fulltitle" in response_json:
        description = response_json["fulltitle"][0:1021]
        # escape Markdown and special characters
    tmp_directory_for_each_user = os.path.join(DOWNLOAD_LOCATION,
                                               str(update.from_user.id))
    if not os.path.isdir(tmp_directory_for_each_user):
        os.makedirs(tmp_directory_for_each_user)
    download_directory = os.path.join(tmp_directory_for_each_user,
                                      custom_file_name)
    command_to_exec = []
    if tg_send_type == "audio":
        command_to_exec = [
            "youtube-dl", "-c", "--max-filesize",
            str(TG_MAX_FILE_SIZE), "--prefer-ffmpeg", "--extract-audio",
            "--audio-format", youtube_dl_ext, "--audio-quality",
            youtube_dl_format, youtube_dl_url, "-o", download_directory
        ]
    else:
        minus_f_format = youtube_dl_format
        if "youtu" in youtube_dl_url:
            minus_f_format = youtube_dl_format + "+bestaudio"
        command_to_exec = [
            "youtube-dl", "-c", "--max-filesize",
            str(TG_MAX_FILE_SIZE), "--embed-subs", "-f", minus_f_format,
            "--hls-prefer-ffmpeg", youtube_dl_url, "-o", download_directory
        ]
    if HTTP_PROXY is not None:
        command_to_exec.append("--proxy")
        command_to_exec.append(HTTP_PROXY)
    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)
    command_to_exec.append("--no-warnings")
    # command_to_exec.append("--quiet")
    command_to_exec.append("--restrict-filenames")
    LOGGER.info(command_to_exec)
    start = datetime.now()
    t_response, e_response = await run_shell_command(command_to_exec)
    LOGGER.info(e_response)
    LOGGER.info(t_response)
    if e_response and Translation.YTDL_ERROR_MESSAGE in e_response:
        error_message = e_response.replace(Translation.YTDL_ERROR_MESSAGE, "")
        await update.message.edit_caption(caption=error_message)
        return False
    if t_response:
        # LOGGER.info(t_response)
        os.remove(save_ytdl_json_path)
        end_one = datetime.now()
        time_taken_for_download = (end_one - start).seconds
        file_size = TG_MAX_FILE_SIZE + 1
        download_directory_dirname = os.path.dirname(download_directory)
        download_directory_contents = os.listdir(download_directory_dirname)
        for download_directory_c in download_directory_contents:
            current_file_name = os.path.join(download_directory_dirname,
                                             download_directory_c)
            file_size = os.stat(current_file_name).st_size

            if file_size > TG_MAX_FILE_SIZE:
                await update.message.edit_caption(
                    caption=Translation.RCHD_TG_API_LIMIT.format(
                        time_taken_for_download, humanbytes(file_size)))

            else:
                is_w_f = False
                images = await generate_screen_shots(
                    current_file_name, tmp_directory_for_each_user, is_w_f, "",
                    300, 9)
                LOGGER.info(images)
                await update.message.edit_caption(
                    caption=Translation.UPLOAD_START)
                # get the correct width, height, and duration
                # for videos greater than 10MB
                # ref: message from @BotSupport
                width = 0
                height = 0
                duration = 0
                if tg_send_type != "file":
                    metadata = extractMetadata(createParser(current_file_name))
                    if metadata is not None:
                        if metadata.has("duration"):
                            duration = metadata.get('duration').seconds
                # get the correct width, height, and duration
                # for videos greater than 10MB
                if os.path.exists(thumb_image_path):
                    # https://stackoverflow.com/a/21669827/4723940
                    Image.open(thumb_image_path).convert("RGB").save(
                        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 tg_send_type == "vm":
                        height = width
                else:
                    thumb_image_path = None
                start_time = time.time()
                # try to upload file
                if tg_send_type == "audio":
                    await update.message.reply_audio(
                        audio=current_file_name,
                        caption=description,
                        parse_mode="HTML",
                        duration=duration,
                        # performer=response_json["uploader"],
                        # title=response_json["title"],
                        # reply_markup=reply_markup,
                        thumb=thumb_image_path,
                        progress=progress_for_pyrogram,
                        progress_args=(Translation.UPLOAD_START,
                                       update.message, start_time))
                elif tg_send_type == "file":
                    await update.message.reply_document(
                        document=current_file_name,
                        thumb=thumb_image_path,
                        caption=description,
                        parse_mode="HTML",
                        # reply_markup=reply_markup,
                        progress=progress_for_pyrogram,
                        progress_args=(Translation.UPLOAD_START,
                                       update.message, start_time))
                elif tg_send_type == "vm":
                    await update.message.reply_video_note(
                        video_note=current_file_name,
                        duration=duration,
                        length=width,
                        thumb=thumb_image_path,
                        progress=progress_for_pyrogram,
                        progress_args=(Translation.UPLOAD_START,
                                       update.message, start_time))
                elif tg_send_type == "video":
                    await update.message.reply_video(
                        video=current_file_name,
                        caption=description,
                        parse_mode="HTML",
                        duration=duration,
                        width=width,
                        height=height,
                        supports_streaming=True,
                        # reply_markup=reply_markup,
                        thumb=thumb_image_path,
                        progress=progress_for_pyrogram,
                        progress_args=(Translation.UPLOAD_START,
                                       update.message, start_time))
                else:
                    LOGGER.info("Did this happen? :\\")
                end_two = datetime.now()
                time_taken_for_upload = (end_two - end_one).seconds
                #
                media_album_p = []
                if images is not None:
                    i = 0
                    caption = "© @KS_AnyDLBot"
                    for image in images:
                        if os.path.exists(image):
                            if i == 0:
                                media_album_p.append(
                                    InputMediaPhoto(media=image,
                                                    caption=caption,
                                                    parse_mode="html"))
                            else:
                                media_album_p.append(
                                    InputMediaPhoto(media=image))
                            i = i + 1
                await update.message.reply_media_group(
                    media=media_album_p, disable_notification=True)
            #
            shutil.rmtree(tmp_directory_for_each_user, ignore_errors=True)
            os.remove(thumb_image_path)

            await update.message.delete()
async def youtube_dl_call_back(bot, update):
    cb_data = update.data
    # youtube_dl extractors
    tg_send_type, youtube_dl_format, youtube_dl_ext = cb_data.split("|")
    thumb_image_path = DOWNLOAD_LOCATION + \
        "/" + str(update.from_user.id) + ".jpg"
    save_ytdl_json_path = DOWNLOAD_LOCATION + \
        "/" + str(update.from_user.id) + ".json"
    try:
        with open(save_ytdl_json_path, "r", encoding="utf8") as f:
            response_json = json.load(f)
    except (FileNotFoundError) as e:
        await update.message.delete()
        return False
    youtube_dl_url = update.message.reply_to_message.text
    custom_file_name = str(response_json.get("title")) + \
        "_" + youtube_dl_format + "." + youtube_dl_ext
    youtube_dl_username = None
    youtube_dl_password = None
    if "|" in youtube_dl_url:
        url_parts = youtube_dl_url.split("|")
        if len(url_parts) == 2:
            youtube_dl_url = url_parts[0]
            custom_file_name = url_parts[1]
            if len(custom_file_name) > 64:
                await update.message.reply_text(
                    Translation.IFLONG_FILE_NAME.format(
                        alimit="64",
                        num=len(custom_file_name)
                    )
                )
                return
        elif len(url_parts) == 4:
            youtube_dl_url = url_parts[0]
            custom_file_name = url_parts[1]
            youtube_dl_username = url_parts[2]
            youtube_dl_password = url_parts[3]
        else:
            for entity in update.message.reply_to_message.entities:
                if entity.type == "text_link":
                    youtube_dl_url = entity.url
                elif entity.type == "url":
                    o = entity.offset
                    l = entity.length
                    youtube_dl_url = youtube_dl_url[o:o + l]
        if youtube_dl_url is not None:
            youtube_dl_url = youtube_dl_url.strip()
        if custom_file_name is not None:
            custom_file_name = custom_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(youtube_dl_url)
        LOGGER.info(custom_file_name)
    else:
        for entity in update.message.reply_to_message.entities:
            if entity.type == "text_link":
                youtube_dl_url = entity.url
            elif entity.type == "url":
                o = entity.offset
                l = entity.length
                youtube_dl_url = youtube_dl_url[o:o + l]
    await update.message.edit_caption(
        caption=Translation.DOWNLOAD_START
    )
    description = Translation.CUSTOM_CAPTION_UL_FILE
    if "fulltitle" in response_json:
        description = response_json["fulltitle"][0:1021]
        # escape Markdown and special characters
    tmp_directory_for_each_user = DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
    if not os.path.isdir(tmp_directory_for_each_user):
        os.makedirs(tmp_directory_for_each_user)
    download_directory = tmp_directory_for_each_user + "/" + custom_file_name
    command_to_exec = []
    if tg_send_type == "audio":
        command_to_exec = [
            "youtube-dl",
            "-c",
            "--max-filesize", str(TG_MAX_FILE_SIZE),
            "--prefer-ffmpeg",
            "--extract-audio",
            "--audio-format", youtube_dl_ext,
            "--audio-quality", youtube_dl_format,
            youtube_dl_url,
            "-o", download_directory
        ]
    else:
        # command_to_exec = ["youtube-dl", "-f", youtube_dl_format, "--hls-prefer-ffmpeg", "--recode-video", "mp4", "-k", youtube_dl_url, "-o", download_directory]
        minus_f_format = youtube_dl_format
        if "youtu" in youtube_dl_url:
            minus_f_format = youtube_dl_format + "+bestaudio"
        command_to_exec = [
            "youtube-dl",
            "-c",
            "--max-filesize", str(TG_MAX_FILE_SIZE),
            "--embed-subs",
            "-f", minus_f_format,
            "--hls-prefer-ffmpeg", youtube_dl_url,
            "-o", download_directory
        ]
    if HTTP_PROXY is not None:
        command_to_exec.append("--proxy")
        command_to_exec.append(HTTP_PROXY)
    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)
    command_to_exec.append("--no-warnings")
    # command_to_exec.append("--quiet")
    command_to_exec.append("--restrict-filenames")
    LOGGER.info(command_to_exec)
    start = datetime.now()
    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()
    t_response = stdout.decode().strip()
    LOGGER.info(e_response)
    LOGGER.info(t_response)
    ad_string_to_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 e_response and ad_string_to_replace in e_response:
        error_message = e_response.replace(ad_string_to_replace, "")
        await update.message.edit_caption(
            caption=error_message
        )
        return False
    if t_response:
        # LOGGER.info(t_response)
        os.remove(save_ytdl_json_path)
        end_one = datetime.now()
        time_taken_for_download = (end_one -start).seconds
        file_size = TG_MAX_FILE_SIZE + 1
        download_directory_dirname = os.path.dirname(download_directory)
        download_directory_contents = os.listdir(download_directory_dirname)
        for download_directory_c in download_directory_contents:
            current_file_name = os.path.join(
                download_directory_dirname,
                download_directory_c
            )
            file_size = os.stat(current_file_name).st_size
        
            if file_size > TG_MAX_FILE_SIZE:
                await update.message.edit_caption(
                    caption=Translation.RCHD_TG_API_LIMIT.format(
                        time_taken_for_download,
                        humanbytes(file_size)
                    )
                )
            else:
                is_w_f = False
                images = await generate_screen_shots(
                    current_file_name,
                    tmp_directory_for_each_user,
                    is_w_f,
                    "",
                    300,
                    9
                )
                LOGGER.info(images)
                await update.message.edit_caption(
                    caption=Translation.UPLOAD_START
                )
                # get the correct width, height, and duration for videos greater than 10MB
                # ref: message from @BotSupport
                width = 0
                height = 0
                duration = 0
                if tg_send_type != "file":
                    metadata = extractMetadata(createParser(current_file_name))
                    if metadata is not None:
                        if metadata.has("duration"):
                            duration = metadata.get('duration').seconds
                # get the correct width, height, and duration for videos greater than 10MB
                if os.path.exists(thumb_image_path):
                    width = 0
                    height = 90
                    
                    # 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.thumbnail((90, 90))
                    if tg_send_type == "file":
                        img.resize((320, height))
                    else:
                        img.resize((90, height))
                    img.save(thumb_image_path, "JPEG")
                    # https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#create-thumbnails
                    
                    metadata = extractMetadata(createParser(thumb_image_path))
                    if metadata.has("width"):
                        width = metadata.get("width")
                    if metadata.has("height"):
                        height = metadata.get("height")
                    if tg_send_type == "vm":
                        height = width
                else:
                    thumb_image_path = None
                start_time = time.time()
                # try to upload file
                if tg_send_type == "audio":
                    await bot.send_audio(
                        chat_id=update.message.chat.id,
                        audio=download_directory,
                        caption=description,
                        parse_mode="HTML",
                        duration=duration,
                        # performer=response_json["uploader"],
                        # title=response_json["title"],
                        # reply_markup=reply_markup,
                        thumb=thumb_image_path,
                        reply_to_message_id=update.message.reply_to_message.message_id,
                        progress=progress_for_pyrogram,
                        progress_args=(
                            Translation.UPLOAD_START,
                            update.message,
                            start_time
                        )
                    )
                elif tg_send_type == "file":
                    await bot.send_document(
                        chat_id=update.message.chat.id,
                        document=download_directory,
                        thumb=thumb_image_path,
                        caption=description,
                        parse_mode="HTML",
                        # reply_markup=reply_markup,
                        reply_to_message_id=update.message.reply_to_message.message_id,
                        progress=progress_for_pyrogram,
                        progress_args=(
                            Translation.UPLOAD_START,
                            update.message,
                            start_time
                        )
                    )
                elif tg_send_type == "vm":
                    await bot.send_video_note(
                        chat_id=update.message.chat.id,
                        video_note=download_directory,
                        duration=duration,
                        length=width,
                        thumb=thumb_image_path,
                        reply_to_message_id=update.message.reply_to_message.message_id,
                        progress=progress_for_pyrogram,
                        progress_args=(
                            Translation.UPLOAD_START,
                            update.message,
                            start_time
                        )
                    )
                elif tg_send_type == "video":
                    await bot.send_video(
                        chat_id=update.message.chat.id,
                        video=download_directory,
                        caption=description,
                        parse_mode="HTML",
                        duration=duration,
                        width=width,
                        height=height,
                        supports_streaming=True,
                        # reply_markup=reply_markup,
                        thumb=thumb_image_path,
                        reply_to_message_id=update.message.reply_to_message.message_id,
                        progress=progress_for_pyrogram,
                        progress_args=(
                            Translation.UPLOAD_START,
                            update.message,
                            start_time
                        )
                    )
                else:
                    LOGGER.info("Did this happen? :\\")
                end_two = datetime.now()
                time_taken_for_upload = (end_two - end_one).seconds
                #
                media_album_p = []
                if images is not None:
                    i = 0
                    caption = "© @AnyDLBot"
                    if is_w_f:
                        caption = "/upgrade to Plan D to remove the watermark\n© @AnyDLBot"
                    for image in images:
                        if os.path.exists(image):
                            if i == 0:
                                media_album_p.append(
                                    InputMediaPhoto(
                                        media=image,
                                        caption=caption,
                                        parse_mode="html"
                                    )
                                )
                            else:
                                media_album_p.append(
                                    InputMediaPhoto(
                                        media=image
                                    )
                                )
                            i = i + 1
                await bot.send_media_group(
                    chat_id=update.message.chat.id,
                    disable_notification=True,
                    reply_to_message_id=update.message.message_id,
                    media=media_album_p
                )
            #
            try:
                shutil.rmtree(tmp_directory_for_each_user)
                os.remove(thumb_image_path)
            except:
                pass
            await bot.edit_message_text(
                text=Translation.AFTER_SUCCESSFUL_UPLOAD_MSG_WITH_TS.format(time_taken_for_download, time_taken_for_upload),
                chat_id=update.message.chat.id,
                message_id=update.message.message_id,
                disable_web_page_preview=True
            )
Example #6
0
async def echo(_, message):
    LOGGER.info(message.from_user)
    url, _, youtube_dl_username, youtube_dl_password = get_link(message)

    info_dict = {}
    if youtube_dl_username and youtube_dl_password:
        info_dict.update(
            {
                "username": youtube_dl_username,
                "password": youtube_dl_password,
            }
        )
    if "hotstar" in url:
        info_dict.update(
            {
                "geo_bypass_country": "IN",
            }
        )
    try:
        info = await yt_extract_info(
            video_url=url,
            download=False,
            ytdl_opts=info_dict,
            ie_key=None,
        )
    except youtube_dl.utils.DownloadError as ytdl_error:
        await message.reply_text(text=str(ytdl_error), quote=True)
        return False

    if info:
        ikeyboard = InlineKeyboard()

        thumb_image = info.get("thumbnail", None)
        thumbnail = thumb_image or Config.DEFAULT_THUMBNAIL

        extractor_key = info.get("extractor_key", "Generic")
        duration = info.get("duration", None)
        if "formats" in info:
            for formats in info.get("formats"):
                format_id = formats.get("format_id")
                format_string = formats.get("format_note", None)
                if format_string is None:
                    format_string = formats.get("format")
                # @SpEcHiDe/PublicLeech//helper_funcs/youtube_dl_extractor.py#L100
                if "DASH" in format_string.upper():
                    continue
                format_ext = formats.get("ext")
                av_codec = "empty"
                if formats.get("acodec") == "none" or formats.get("vcodec") == "none":
                    av_codec = "none"
                approx_file_size = (
                    humanbytes(formats.get("filesize"))
                    if formats.get("filesize")
                    else ""
                )
                display_str = (
                    f"{format_string} [{format_ext.upper()}] {approx_file_size}"
                )
                cb_string_video = f"video|{extractor_key}|{format_id}|{av_codec}"
                # GDrive gets special pass, acodec is not listed here, ie acodec=None
                if (
                    extractor_key == "GoogleDrive"
                    and format_id == "source"
                    or extractor_key != "GoogleDrive"
                    and format_string
                    and "audio only" not in format_string
                ):
                    ikeyboard.row(
                        InlineKeyboardButton(display_str, callback_data=cb_string_video)
                    )
                elif extractor_key != "GoogleDrive":
                    # special weird case :\
                    ikeyboard.row(
                        InlineKeyboardButton(
                            f"Video {approx_file_size}", cb_string_video
                        ),
                    )
            if duration:
                ikeyboard.row(
                    InlineKeyboardButton(
                        "MP3 (64 kbps)", callback_data=f"audio|{extractor_key}|64|mp3"
                    ),
                    InlineKeyboardButton(
                        "MP3 (128 kbps)", callback_data=f"audio|{extractor_key}|128|mp3"
                    ),
                )
                ikeyboard.row(
                    InlineKeyboardButton(
                        "MP3 (320 kbps)", callback_data=f"audio|{extractor_key}|320|mp3"
                    )
                )
        elif "entries" in info:
            for entries in info.get("entries"):
                for formats in entries.get("formats"):
                    format_id = formats.get("format_id")
                    format_ext = formats.get("ext")
                    cb_string_file = f"file|{extractor_key}|{format_id}|{format_ext}"
                    ikeyboard.row(
                        InlineKeyboardButton(
                            f"YTDL Generic File [{format_ext.upper()}]", callback_data=cb_string_file
                        ),
                    )
        else:
            format_id = info.get("format_id")
            format_ext = info.get("ext")
            cb_string_file = f"file={extractor_key}={format_id}={format_ext}"
            ikeyboard.row(
                InlineKeyboardButton(
                    f"File [{format_ext.upper()}]", callback_data=cb_string_file
                ),
            )

        save_thumbnail = os.path.join(
            Config.WORK_DIR, str(message.from_user.id) + ".jpg"
        )
        if not os.path.isdir(Config.WORK_DIR):
            os.makedirs(Config.WORK_DIR)

        if os.path.exists(save_thumbnail):
            thumb_image_path = save_thumbnail
        else:
            thumb_image_path = await get_thumbnail(thumbnail, save_thumbnail)
        await message.reply_photo(
            photo=thumb_image_path,
            quote=True,
            caption=String.FORMAT_SELECTION.format(thumbnail)
            + "\n"
            + String.SET_CUSTOM_USERNAME_PASSWORD,
            reply_markup=ikeyboard,
            parse_mode="html",
        )
    else:
        # fallback for nonnumeric port a.k.a seedbox.io
        ikeyboard = InlineKeyboard()
        cb_string_file = "file=LFO=NONE=NONE"
        ikeyboard.row(
            InlineKeyboardButton("File", callback_data=cb_string_file),
        )
        await message.reply_photo(
            photo=Config.DEFAULT_THUMBNAIL,
            quote=True,
            caption=String.FORMAT_SELECTION.format(""),
            reply_markup=ikeyboard,
            parse_mode="html",
            reply_to_message_id=message.message_id,
        )
Example #7
0
async def echo(bot, update: Message):
    if update.from_user.id in AUTH_USERS:
        await update.delete()
        return
    # LOGGER.info(update)
    # await bot.send_chat_action(
    #     chat_id=update.chat.id,
    #     action="typing"
    # )
    LOGGER.info(update.from_user)
    url, _, youtube_dl_username, youtube_dl_password = get_link(update)
    if HTTP_PROXY is not None:
        command_to_exec = [
            "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
            "-j", url, "--proxy", HTTP_PROXY
        ]
    else:
        command_to_exec = [
            "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
            "-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)
    t_response, e_response = await run_shell_command(command_to_exec)
    # 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(Translation.YTDL_ERROR_MESSAGE, "")
        if Translation.ISOAYD_PREMIUM_VIDEOS in error_message:
            error_message += Translation.SET_CUSTOM_USERNAME_PASSWORD
        await update.reply_text(text=Translation.NO_VOID_FORMAT_FOUND.format(
            str(error_message)),
                                quote=True,
                                parse_mode="html",
                                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)
        save_ytdl_json_path = DOWNLOAD_LOCATION + \
            "/" + str(update.from_user.id) + ".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")
                format_ext = formats.get("ext")
                approx_file_size = ""
                if "filesize" in formats:
                    approx_file_size = humanbytes(formats["filesize"])
                cb_string_video = "{}|{}|{}".format("video", format_id,
                                                    format_ext)
                cb_string_file = "{}|{}|{}".format("file", format_id,
                                                   format_ext)
                if format_string and "audio only" not in format_string:
                    ikeyboard = [
                        InlineKeyboardButton(
                            f"S {format_string} video  {format_ext} ",
                            callback_data=(cb_string_video).encode("UTF-8")),
                        InlineKeyboardButton(
                            f"D {format_string} video  {approx_file_size} ",
                            callback_data=(cb_string_file).encode("UTF-8"))
                    ]
                else:
                    # special weird case :\
                    ikeyboard = [
                        InlineKeyboardButton(
                            "SVideo [" + "] ( " + approx_file_size + " )",
                            callback_data=(cb_string_video).encode("UTF-8")),
                        InlineKeyboardButton(
                            "DFile [" + "] ( " + approx_file_size + " )",
                            callback_data=(cb_string_file).encode("UTF-8"))
                    ]
                inline_keyboard.append(ikeyboard)
            if duration is not None:
                cb_string_64 = "{}|{}|{}".format("audio", "64k", "mp3")
                cb_string_128 = "{}|{}|{}".format("audio", "128k", "mp3")
                cb_string = "{}|{}|{}".format("audio", "320k", "mp3")
                inline_keyboard.append([
                    InlineKeyboardButton(
                        "MP3 " + "(" + "64 kbps" + ")",
                        callback_data=cb_string_64.encode("UTF-8")),
                    InlineKeyboardButton(
                        "MP3 " + "(" + "128 kbps" + ")",
                        callback_data=cb_string_128.encode("UTF-8"))
                ])
                inline_keyboard.append([
                    InlineKeyboardButton(
                        "MP3 " + "(" + "320 kbps" + ")",
                        callback_data=cb_string.encode("UTF-8"))
                ])
        else:
            format_id = response_json["format_id"]
            format_ext = response_json["ext"]
            cb_string_file = "{}|{}|{}".format("file", format_id, format_ext)
            cb_string_video = "{}|{}|{}".format("video", format_id, format_ext)
            inline_keyboard.append([
                InlineKeyboardButton(
                    "SVideo", callback_data=(cb_string_video).encode("UTF-8")),
                InlineKeyboardButton(
                    "DFile", callback_data=(cb_string_file).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(
                    "video", callback_data=(cb_string_video).encode("UTF-8")),
                InlineKeyboardButton(
                    "file", callback_data=(cb_string_file).encode("UTF-8"))
            ])
        reply_markup = InlineKeyboardMarkup(inline_keyboard)
        # logger.info(reply_markup)
        thumbnail = DEF_THUMB_NAIL_VID_S
        thumbnail_image = DEF_THUMB_NAIL_VID_S
        if "thumbnail" in response_json:
            if response_json["thumbnail"] is not None:
                thumbnail = response_json["thumbnail"]
                thumbnail_image = response_json["thumbnail"]
        thumb_image_path = DownLoadFile(
            thumbnail_image,
            DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg",
            128,
            None,  # bot,
            Translation.DOWNLOAD_START,
            update.message_id,
            update.chat.id)
        await update.reply_photo(
            photo=thumb_image_path,
            quote=True,
            caption=Translation.FORMAT_SELECTION.format(thumbnail) + "\n" +
            Translation.SET_CUSTOM_USERNAME_PASSWORD,
            reply_markup=reply_markup,
            parse_mode="html")
    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(
                "SVideo", callback_data=(cb_string_video).encode("UTF-8")),
            InlineKeyboardButton(
                "DFile", callback_data=(cb_string_file).encode("UTF-8"))
        ])
        reply_markup = InlineKeyboardMarkup(inline_keyboard)
        await update.reply_photo(
            photo=DEF_THUMB_NAIL_VID_S,
            quote=True,
            caption=Translation.FORMAT_SELECTION.format(""),
            reply_markup=reply_markup,
            parse_mode="html",
            reply_to_message_id=update.message_id)
Example #8
0
async def upload_worker(update, filename, send_as, generatess, download_directory):
    tmp_directory_for_each_user = os.path.join(
        Config.WORK_DIR, str(update.from_user.id)
    )
    thumb_image_path = os.path.join(Config.WORK_DIR, str(update.from_user.id) + ".jpg")
    download_directory_dirname = os.path.dirname(download_directory)
    download_directory_contents = os.listdir(download_directory_dirname)
    for download_directory_c in download_directory_contents:
        current_file_name = os.path.join(
            download_directory_dirname, download_directory_c
        )
        file_size = os.stat(current_file_name).st_size

        if file_size > Config.TG_MAX_FILE_SIZE:
            await update.message.edit_caption(
                caption=String.RCHD_TG_API_LIMIT.format(humanbytes(file_size))
            )
            return

        # get the correct width, height, and duration
        # for videos greater than 10MB
        # ref: message from @BotSupport
        width = 0
        height = 0
        duration = 0
        if send_as != "file":
            metadata = extractMetadata(createParser(current_file_name))
            if metadata is not None and metadata.has("duration"):
                duration = metadata.get("duration").seconds
        # get the correct width, height, and duration
        # for videos greater than 10MB
        if os.path.exists(thumb_image_path):
            # https://stackoverflow.com/a/21669827/4723940
            Image.open(thumb_image_path).convert("RGB").save(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")
        else:
            thumb_image_path = None
        start_upload = datetime.now()
        c_time = time.time()
        if send_as == "audio":
            await update.message.reply_audio(
                audio=current_file_name,
                caption=filename,
                parse_mode="HTML",
                duration=duration,
                # performer=response_json["uploader"],
                # title=response_json["title"],
                # reply_markup=reply_markup,
                thumb=thumb_image_path,
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        elif send_as == "file":
            await update.message.reply_document(
                document=current_file_name,
                thumb=thumb_image_path,
                caption=filename,
                parse_mode="HTML",
                # reply_markup=reply_markup,
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        elif send_as == "video":
            await update.message.reply_video(
                video=current_file_name,
                caption=filename,
                parse_mode="HTML",
                duration=duration,
                width=width,
                height=height,
                supports_streaming=True,
                # reply_markup=reply_markup,
                thumb=thumb_image_path,
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        else:
            LOGGER.info("Did this happen? :\\")
        end_upload = datetime.now()
        time_taken_for_upload = (end_upload - start_upload).seconds
        min_duration = 300
        media_album_p = []
        if generatess and duration > min_duration:
            images = generate_screenshots(
                current_file_name, tmp_directory_for_each_user, duration, 5
            )
            LOGGER.info(images)
            i = 0
            caption = f"© @AnyDLBot - Uploaded in {time_taken_for_upload} seconds"
            for image in images:
                if os.path.exists(image):
                    if i == 0:
                        media_album_p.append(
                            InputMediaPhoto(
                                media=image, caption=caption, parse_mode="html"
                            )
                        )
                    else:
                        media_album_p.append(InputMediaPhoto(media=image))
                    i += 1
        await update.message.reply_media_group(
            media=media_album_p, disable_notification=True
        )
        #
        return True
Example #9
0
async def echo(bot, update):
    if update.from_user.id not in AUTH_USERS:
        await update.delete()
        return
    # LOGGER.info(update)
    # await bot.send_chat_action(
    #     chat_id=update.chat.id,
    #     action="typing"
    # )
    LOGGER.info(update.from_user)
    url = update.text
    if url.count("|") == 2:
        shomar = random.randint(1, 10000)
        # youtube_dl extractors
        youtube_dl_url, custom_file_name, youtube_dl_format = url.split(" | ")
        tg_send_type = "file"
        if ") FullHD" in custom_file_name:
            await bot.send_message(
                text=Translation.DOWNLOAD_START,
                chat_id=update.chat.id,
                reply_to_message_id=update.message_id,
            )
            description = "@BachehayeManoto FullHD"
            custom_file_name = custom_file_name + ".mp4"
            tmp_directory_for_each_user = DOWNLOAD_LOCATION + "/" + str(shomar)
            if not os.path.isdir(tmp_directory_for_each_user):
                os.makedirs(tmp_directory_for_each_user)
            download_directory = tmp_directory_for_each_user + "/" + custom_file_name
            command_to_exec = [
                "youtube-dl", "-c", "--max-filesize",
                str(TG_MAX_FILE_SIZE), "--embed-subs", "-f", youtube_dl_format,
                "--hls-prefer-ffmpeg", youtube_dl_url, "-o", download_directory
            ]
            command_to_exec.append("--no-warnings")
            # command_to_exec.append("--quiet")
            command_to_exec.append("--restrict-filenames")
            LOGGER.info(command_to_exec)
            start = datetime.now()
            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()
            t_response = stdout.decode().strip()
            LOGGER.info(e_response)
            LOGGER.info(t_response)
            ad_string_to_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 e_response and ad_string_to_replace in e_response:
                error_message = e_response.replace(ad_string_to_replace, "")
                await bot.edit_message_text(chat_id=update.chat.id,
                                            message_id=update.message_id + 1,
                                            text=error_message)
                return False
            if t_response:
                # LOGGER.info(t_response)
                end_one = datetime.now()
                time_taken_for_download = (end_one - start).seconds
                file_size = TG_MAX_FILE_SIZE + 1
                download_directory_dirname = os.path.dirname(
                    download_directory)
                download_directory_contents = os.listdir(
                    download_directory_dirname)
                for download_directory_c in download_directory_contents:
                    current_file_name = os.path.join(
                        download_directory_dirname, download_directory_c)
                    file_size = os.stat(current_file_name).st_size

                    if file_size > TG_MAX_FILE_SIZE:
                        await bot.edit_message_text(
                            chat_id=update.chat.id,
                            text=Translation.RCHD_TG_API_LIMIT.format(
                                time_taken_for_download,
                                humanbytes(file_size)),
                            message_id=update.message_id + 1)
                    else:
                        is_w_f = False
                        images = await generate_screen_shots(
                            current_file_name, tmp_directory_for_each_user,
                            is_w_f, "", 300, 9)
                        LOGGER.info(images)
                        await bot.edit_message_text(
                            text=Translation.UPLOAD_START,
                            chat_id=update.chat.id,
                            message_id=update.message_id + 1)
                        # get the correct width, height, and duration for videos greater than 10MB
                        # ref: message from @BotSupport
                        width = 0
                        height = 0
                        duration = 0
                        start_time = time.time()
                        # try to upload file
                        if tg_send_type == "file":
                            await bot.send_document(
                                chat_id=update.chat.id,
                                document=download_directory,
                                #thumb=thumb_image_path,
                                caption=description,
                                parse_mode="HTML",
                                # reply_markup=reply_markup,
                                reply_to_message_id=update.message_id + 1,
                                progress=progress_for_pyrogram,
                                progress_args=(Translation.UPLOAD_START,
                                               update, start_time))
                        else:
                            LOGGER.info("Did this happen? :\\")
                        end_two = datetime.now()
                        time_taken_for_upload = (end_two - end_one).seconds
                        media_album_p = []
                        if images is not None:
                            i = 0
                            #caption = "© @AnyDLBot"
                            if is_w_f:
                                caption = "/upgrade to Plan D to remove the watermark\n© @AnyDLBot"
                            for image in images:
                                if os.path.exists(image):
                                    if i == 0:
                                        media_album_p.append(
                                            InputMediaPhoto(media=image,
                                                            caption=caption,
                                                            parse_mode="html"))
                                    else:
                                        media_album_p.append(
                                            InputMediaPhoto(media=image))
                                    i = i + 1
                        await bot.send_media_group(
                            chat_id=update.chat.id,
                            disable_notification=True,
                            reply_to_message_id=update.message_id + 1,
                            media=media_album_p)
                    #
                    try:
                        shutil.rmtree(tmp_directory_for_each_user)
                    except:
                        pass
                    await bot.edit_message_text(
                        text=
                        "Downloaded in {} seconds. \nUploaded in {} seconds.".
                        format(time_taken_for_download, time_taken_for_upload),
                        chat_id=update.chat.id,
                        message_id=update.message_id + 1,
                        disable_web_page_preview=True)
        if ") HD" in custom_file_name:
            await bot.send_message(
                text=Translation.DOWNLOAD_START,
                chat_id=update.chat.id,
                reply_to_message_id=update.message_id,
            )
            description = "@BachehayeManoto HD"
            custom_file_name = custom_file_name + ".mp4"
            tmp_directory_for_each_user = DOWNLOAD_LOCATION + "/" + str(shomar)
            if not os.path.isdir(tmp_directory_for_each_user):
                os.makedirs(tmp_directory_for_each_user)
            download_directory = tmp_directory_for_each_user + "/" + custom_file_name
            command_to_exec = [
                "youtube-dl", "-c", "--max-filesize",
                str(TG_MAX_FILE_SIZE), "--embed-subs", "-f", youtube_dl_format,
                "--hls-prefer-ffmpeg", youtube_dl_url, "-o", download_directory
            ]
            command_to_exec.append("--no-warnings")
            # command_to_exec.append("--quiet")
            command_to_exec.append("--restrict-filenames")
            LOGGER.info(command_to_exec)
            start = datetime.now()
            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()
            t_response = stdout.decode().strip()
            LOGGER.info(e_response)
            LOGGER.info(t_response)
            ad_string_to_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 e_response and ad_string_to_replace in e_response:
                error_message = e_response.replace(ad_string_to_replace, "")
                await bot.edit_message_text(chat_id=update.chat.id,
                                            message_id=update.message_id + 1,
                                            text=error_message)
                return False
            if t_response:
                # LOGGER.info(t_response)
                end_one = datetime.now()
                time_taken_for_download = (end_one - start).seconds
                file_size = TG_MAX_FILE_SIZE + 1
                download_directory_dirname = os.path.dirname(
                    download_directory)
                download_directory_contents = os.listdir(
                    download_directory_dirname)
                for download_directory_c in download_directory_contents:
                    current_file_name = os.path.join(
                        download_directory_dirname, download_directory_c)
                    file_size = os.stat(current_file_name).st_size

                    if file_size > TG_MAX_FILE_SIZE:
                        await bot.edit_message_text(
                            chat_id=update.chat.id,
                            text=Translation.RCHD_TG_API_LIMIT.format(
                                time_taken_for_download,
                                humanbytes(file_size)),
                            message_id=update.message_id + 1)
                    else:
                        is_w_f = False
                        images = await generate_screen_shots(
                            current_file_name, tmp_directory_for_each_user,
                            is_w_f, "", 300, 9)
                        LOGGER.info(images)
                        await bot.edit_message_text(
                            text=Translation.UPLOAD_START,
                            chat_id=update.chat.id,
                            message_id=update.message_id + 1)
                        # get the correct width, height, and duration for videos greater than 10MB
                        # ref: message from @BotSupport
                        width = 0
                        height = 0
                        duration = 0
                        start_time = time.time()
                        # try to upload file
                        if tg_send_type == "file":
                            await bot.send_document(
                                chat_id=update.chat.id,
                                document=download_directory,
                                #thumb=thumb_image_path,
                                caption=description,
                                parse_mode="HTML",
                                # reply_markup=reply_markup,
                                reply_to_message_id=update.message_id + 1,
                                progress=progress_for_pyrogram,
                                progress_args=(Translation.UPLOAD_START,
                                               update, start_time))
                        else:
                            LOGGER.info("Did this happen? :\\")
                        end_two = datetime.now()
                        time_taken_for_upload = (end_two - end_one).seconds
                        media_album_p = []
                        if images is not None:
                            i = 0
                            #caption = "© @AnyDLBot"
                            if is_w_f:
                                caption = "/upgrade to Plan D to remove the watermark\n© @AnyDLBot"
                            for image in images:
                                if os.path.exists(image):
                                    if i == 0:
                                        media_album_p.append(
                                            InputMediaPhoto(media=image,
                                                            caption=caption,
                                                            parse_mode="html"))
                                    else:
                                        media_album_p.append(
                                            InputMediaPhoto(media=image))
                                    i = i + 1
                        await bot.send_media_group(
                            chat_id=update.chat.id,
                            disable_notification=True,
                            reply_to_message_id=update.message_id + 1,
                            media=media_album_p)
                    #
                    try:
                        shutil.rmtree(tmp_directory_for_each_user)
                    except:
                        pass
                    await bot.edit_message_text(
                        text=
                        "Downloaded in {} seconds. \nUploaded in {} seconds.".
                        format(time_taken_for_download, time_taken_for_upload),
                        chat_id=update.chat.id,
                        message_id=update.message_id + 1,
                        disable_web_page_preview=True)
    else:
        youtube_dl_username = None
        youtube_dl_password = None
        file_name = None
        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 HTTP_PROXY is not None:
            command_to_exec = [
                "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
                "-j", url, "--proxy", HTTP_PROXY
            ]
        else:
            command_to_exec = [
                "youtube-dl", "--no-warnings", "--youtube-skip-dash-manifest",
                "-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)
        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 update.reply_text(
                text=Translation.NO_VOID_FORMAT_FOUND.format(
                    str(error_message)),
                quote=True,
                parse_mode="html",
                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)
            save_ytdl_json_path = DOWNLOAD_LOCATION + \
                "/" + str(update.from_user.id) + ".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")
                    format_ext = formats.get("ext")
                    approx_file_size = ""
                    if "filesize" in formats:
                        approx_file_size = humanbytes(formats["filesize"])
                    cb_string_video = "{}-{}-{}".format(
                        "video", format_id, format_ext)
                    cb_string_file = "{}-{}-{}".format("file", format_id,
                                                       format_ext)
                    if format_string is not None and not "audio only" in format_string:
                        ikeyboard = [
                            InlineKeyboardButton(
                                "S " + format_string + " video " +
                                approx_file_size + " ",
                                callback_data=(
                                    cb_string_video).encode("UTF-8")),
                            InlineKeyboardButton(
                                "D " + format_ext + " " + approx_file_size +
                                " ",
                                callback_data=(cb_string_file).encode("UTF-8"))
                        ]
                        """if duration is not None:
                            cb_string_video_message = "{}|{}|{}".format(
                                "vm", format_id, format_ext)
                            ikeyboard.append(
                                pyrogram.InlineKeyboardButton(
                                    "VM",
                                    callback_data=(
                                        cb_string_video_message).encode("UTF-8")
                                )
                            )"""
                    else:
                        # special weird case :\
                        ikeyboard = [
                            InlineKeyboardButton(
                                "SVideo [" + "] ( " + approx_file_size + " )",
                                callback_data=(
                                    cb_string_video).encode("UTF-8")),
                            InlineKeyboardButton(
                                "DFile [" + "] ( " + approx_file_size + " )",
                                callback_data=(cb_string_file).encode("UTF-8"))
                        ]
                    inline_keyboard.append(ikeyboard)
                if duration is not None:
                    cb_string_64 = "{}-{}-{}".format("audio", "64k", "mp3")
                    cb_string_128 = "{}-{}-{}".format("audio", "128k", "mp3")
                    cb_string = "{}-{}-{}".format("audio", "320k", "mp3")
                    inline_keyboard.append([
                        InlineKeyboardButton(
                            "MP3 " + "(" + "64 kbps" + ")",
                            callback_data=cb_string_64.encode("UTF-8")),
                        InlineKeyboardButton(
                            "MP3 " + "(" + "128 kbps" + ")",
                            callback_data=cb_string_128.encode("UTF-8"))
                    ])
                    inline_keyboard.append([
                        InlineKeyboardButton(
                            "MP3 " + "(" + "320 kbps" + ")",
                            callback_data=cb_string.encode("UTF-8"))
                    ])
            else:
                format_id = response_json["format_id"]
                format_ext = response_json["ext"]
                cb_string_file = "{}-{}-{}".format("file", format_id,
                                                   format_ext)
                cb_string_video = "{}-{}-{}".format("video", format_id,
                                                    format_ext)
                inline_keyboard.append([
                    InlineKeyboardButton(
                        "SVideo",
                        callback_data=(cb_string_video).encode("UTF-8")),
                    InlineKeyboardButton(
                        "DFile",
                        callback_data=(cb_string_file).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(
                        "video",
                        callback_data=(cb_string_video).encode("UTF-8")),
                    InlineKeyboardButton(
                        "file", callback_data=(cb_string_file).encode("UTF-8"))
                ])
            reply_markup = InlineKeyboardMarkup(inline_keyboard)
            # logger.info(reply_markup)
            thumbnail = DEF_THUMB_NAIL_VID_S
            thumbnail_image = DEF_THUMB_NAIL_VID_S
            if "thumbnail" in response_json:
                if response_json["thumbnail"] is not None:
                    thumbnail = response_json["thumbnail"]
                    thumbnail_image = response_json["thumbnail"]
            thumb_image_path = DownLoadFile(
                thumbnail_image,
                DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg",
                128,
                None,  # bot,
                Translation.DOWNLOAD_START,
                update.message_id,
                update.chat.id)
            await update.reply_text(
                text=Translation.FORMAT_SELECTION.format(thumbnail) + "\n" +
                Translation.SET_CUSTOM_USERNAME_PASSWORD,
                quote=True,
                reply_markup=reply_markup,
                parse_mode="html",
                disable_web_page_preview=True)
        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(
                    "SVideo", callback_data=(cb_string_video).encode("UTF-8")),
                InlineKeyboardButton(
                    "DFile", callback_data=(cb_string_file).encode("UTF-8"))
            ])
            reply_markup = InlineKeyboardMarkup(inline_keyboard)
            await update.reply_text(
                text=Translation.FORMAT_SELECTION.format(""),
                quote=True,
                reply_markup=reply_markup,
                parse_mode="html",
                disable_web_page_preview=True)
Example #10
0
async def upload_worker(update, filename, thumbnail, download_directory,
                        downloaded_in):
    download_directory_dirname = os.path.dirname(download_directory)
    download_directory_contents = os.listdir(download_directory_dirname)
    LOGGER.info(download_directory_contents)
    for download_directory_c in download_directory_contents:
        current_file_name = os.path.join(download_directory_dirname,
                                         download_directory_c)
        file_size = os.stat(current_file_name).st_size

        if file_size > Config.TG_MAX_FILE_SIZE:
            await update.message.edit_text(
                text=String.RCHD_TG_API_LIMIT.format(humanbytes(file_size)))
            return

        custom_thumb_path = os.path.join(Config.WORK_DIR,
                                         str(update.from_user.id) + ".jpg")
        temp_thumb_dir = os.path.join(download_directory_dirname,
                                      "thumbnail.jpg")
        if os.path.isfile(custom_thumb_path):
            LOGGER.info("Custom thumbnail found. Using this now")
            thumb = custom_thumb_path
        else:
            thumb = (await get_thumbnail(thumbnail, temp_thumb_dir)
                     if thumbnail else None)

        mime_type = magic.from_file(filename=current_file_name, mime=True)
        start_upload = datetime.now()
        c_time = time.time()
        width = height = duration = 0
        if mime_type.startswith("audio"):
            duration = media_duration(current_file_name)
            await update.message.reply_audio(
                audio=current_file_name,
                caption=filename,
                parse_mode="HTML",
                duration=duration,
                thumb=thumb,
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        elif mime_type.startswith("video"):
            duration = media_duration(current_file_name)
            if thumb is None:
                thumb = screencapture(current_file_name,
                                      download_directory_dirname,
                                      duration // 2)
                LOGGER.info("Generating thumbnail of the video.")
            if os.path.isfile(thumb):
                width, height = width_and_height(thumb)
            await update.message.reply_video(
                video=current_file_name,
                caption=filename,
                parse_mode="HTML",
                duration=duration,
                width=width,
                height=height,
                supports_streaming=True,
                thumb=thumb,
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        else:
            await update.message.reply_document(
                document=current_file_name,
                thumb=thumb,
                caption=filename,
                parse_mode="HTML",
                progress=progress_for_pyrogram,
                progress_args=(String.UPLOAD_START, update.message, c_time),
            )

        end_upload = datetime.now()
        uploaded_in = (end_upload - start_upload).seconds
        with TemporaryDirectory(prefix="screenshots",
                                dir=download_directory_dirname) as tempdir:
            if mime_type.startswith(
                    "video") and duration > Config.MIN_DURATION:
                media_album_p = generate_screenshots(current_file_name,
                                                     tempdir, duration, 5)
                # LOGGER.info(media_album_p)
                try:
                    await update.message.reply_media_group(
                        media=media_album_p, disable_notification=True)
                except FloodWait as e:
                    await asyncio.sleep(e.x)
        #
        first_name = f"{update.from_user.first_name}"
        user_link = f"tg://user?id={update.from_user.id}"
        await update.message.edit_text(text=String.FINAL_STATUS.format(
            user_link, first_name, downloaded_in, uploaded_in))
        return True