Пример #1
0
async def catch_youtube_dldata(bot, update):
    thumb_image_path = os.getcwd() + "/" + "thumbnails" + "/" + str(
        update.from_user.id) + ".jpg"
    yt_thumb_image_path = os.getcwd() + "/" + "YouTubeThumb" + "/" + str(
        update.from_user.id) + ".jpg"
    if os.path.exists(thumb_image_path):
        thumb_image = thumb_image_path
    else:
        thumb_image = yt_thumb_image_path
    file_name = str(Config.PRE_FILE_TXT)
    cb_data = update.data
    # Callback Data Check (for Youtube formats)
    if cb_data.startswith(("video", "audio", "docaudio", "docvideo")):
        yturl = cb_data.split("||")[-1]
        format_id = cb_data.split("||")[-2]
        if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")):
            print("no data found")
            raise ContinuePropagation

        new_filext = "%(title)s.%(ext)s"
        filext = file_name + new_filext
        saved_file_path = os.getcwd() + "/" + "downloads" + "/" + str(
            update.from_user.id) + "/"
        if not os.path.isdir(saved_file_path):
            os.makedirs(saved_file_path)
        dl_folder = [f for f in os.listdir(saved_file_path)]
        for f in dl_folder:
            try:
                os.remove(os.path.join(saved_file_path, f))
            except IndexError:
                pass
        await update.edit_message_text(text=Translation.DOWNLOAD_START)
        filepath = os.path.join(saved_file_path, filext)

        audio_command = [
            "youtube-dl",
            "-c",
            "--prefer-ffmpeg",
            "--extract-audio",
            "--audio-format",
            "mp3",
            "--audio-quality",
            format_id,
            "-o",
            filepath,
            yturl,
        ]

        video_command = [
            "youtube-dl", "-c", "--embed-subs", "-f", f"{format_id}+bestaudio",
            "-o", filepath, "--hls-prefer-ffmpeg", yturl
        ]

        loop = asyncio.get_event_loop()
        med = None
        if cb_data.startswith("audio"):
            filename = await downloadaudiocli(audio_command)
            med = InputMediaAudio(media=filename,
                                  caption=os.path.basename(filename),
                                  title=os.path.basename(filename),
                                  thumb=thumb_image)

        if cb_data.startswith("video"):
            description = Translation.CUSTOM_CAPTION_VIDEO
            filename = await downloadvideocli(video_command)
            dur = round(duration(filename))
            med = InputMediaVideo(media=filename,
                                  duration=dur,
                                  caption=description,
                                  thumb=thumb_image,
                                  supports_streaming=True)

        if cb_data.startswith("docaudio"):
            filename = await downloadaudiocli(audio_command)
            med = InputMediaDocument(media=filename,
                                     caption=os.path.basename(filename),
                                     thumb=thumb_image)

        if cb_data.startswith("docvideo"):
            description = Translation.CUSTOM_CAPTION_DOC
            filename = await downloadvideocli(video_command)
            dur = round(duration(filename))
            med = InputMediaDocument(media=filename,
                                     caption=description,
                                     thumb=thumb_image)

        if med:
            loop.create_task(send_file(bot, update, med))

        else:
            print("med not found")

######################################### CB Data query for Bot Settings ###############################################
    else:
        # Callback Data Check (for bot settings)
        if cb_data.startswith(
            ("close", "view_thumb", "del_thumb", "conf_thumb", "start_help",
             "settings", "rename_doc", "convert_video", "d_copy", "v_copy",
             "clear_med")):
            if "close" in cb_data:
                await close_button(bot, update)
            elif "view_thumb" in cb_data:
                await view_thumbnail(bot, update)
            elif "del_thumb" in cb_data:
                await delete_thumbnail(bot, update)
            elif "conf_thumb" in cb_data:
                await del_thumb_confirm(bot, update)
            elif "start_help" in cb_data:
                await start_bot(bot, update)
            elif "settings" in cb_data:
                await bot_settings(bot, update)
            elif "rename_doc" in cb_data:
                await rename_file(bot, update)
            elif "convert_video" in cb_data:
                await convert_to_video(bot, update)
            elif "d_copy" in cb_data:
                await convert_to_doc_copy(bot, update)
            elif "v_copy" in cb_data:
                await convert_to_video_copy(bot, update)
            elif "clear_med" in cb_data:
                await clear_media(bot, update)
async def catch_youtube_dldata(c, q):
    filename = None
    cb_data = q.data.strip()
    # Callback Data Check
    yturl = cb_data.split("||")[-1]
    format_id = cb_data.split("||")[-2]
    if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")):
        print("no data found")
        raise ContinuePropagation

    filext = "%(title)s.%(ext)s"
    userdir = os.path.join(os.getcwd(), "downloads", str(q.message.chat.id))

    if not os.path.isdir(userdir):
        os.makedirs(userdir)
    await q.edit_message_reply_markup(
        InlineKeyboardMarkup([[InlineKeyboardButton("Downloading...", callback_data="down")]]))
    filepath = os.path.join(userdir, filext)
    # await q.edit_message_reply_markup([[InlineKeyboardButton("Processing..")]])

    audio_command = [
        "youtube-dl",
        "-c",
        "--prefer-ffmpeg",
        "--extract-audio",
        "--audio-format", "mp3",
        "--audio-quality", format_id,
        "-o", filepath,
        yturl,

    ]

    video_command = [
        "youtube-dl",
        "-c",
        "--embed-subs",
        "-f", f"{format_id}+bestaudio",
        "-o", filepath,
        "--hls-prefer-ffmpeg", yturl]

    loop = asyncio.get_event_loop()

    med = None
    if cb_data.startswith("audio"):
        filename = await downloadaudiocli(audio_command)
        med = InputMediaAudio(
            media=filename,
            caption=os.path.basename(filename),
            title=os.path.basename(filename)
        )

    if cb_data.startswith("video"):
        filename = await downloadvideocli(video_command)
        dur = round(duration(filename))
        med = InputMediaVideo(
            media=filename,
            duration=dur,
            caption=os.path.basename(filename),
            supports_streaming=True
        )

    if cb_data.startswith("docaudio"):
        filename = await downloadaudiocli(audio_command)
        med = InputMediaDocument(
            media=filename,
            caption=os.path.basename(filename),
        )

    if cb_data.startswith("docvideo"):
        filename = await downloadvideocli(video_command)
        dur = round(duration(filename))
        med = InputMediaDocument(
            media=filename,
            caption=os.path.basename(filename),
        )
    if med:
        loop.create_task(send_file(c, q, med, filename))
    else:
        print("med not found")
Пример #3
0
async def catch_youtube_dldata(c, q):
    cb_data = q.data.strip()
    #print(q.message.chat.id)
    # Callback Data Check
    yturl = cb_data.split("||")[-1]
    format_id = cb_data.split("||")[-2]
    thumb_image_path = "/app/downloads" + \
        "/" + str(q.message.chat.id) + ".jpg"
    print(thumb_image_path)
    if os.path.exists(thumb_image_path):
        width = 0
        height = 0
        metadata = extractMetadata(createParser(thumb_image_path))
        #print(metadata)
        if metadata.has("width"):
            width = metadata.get("width")
        if metadata.has("height"):
            height = metadata.get("height")
        img = Image.open(thumb_image_path)
        if cb_data.startswith(("audio", "docaudio", "docvideo")):
            img.resize((320, height))
        else:
            img.resize((90, height))
        img.save(thumb_image_path, "JPEG")
    #   print(thumb_image_path)
    if not cb_data.startswith(("video", "audio", "docaudio", "docvideo")):
        print("no data found")
        raise ContinuePropagation

    filext = "%(title)s.%(ext)s"
    userdir = os.path.join(os.getcwd(), "downloads", str(q.message.chat.id))

    if not os.path.isdir(userdir):
        os.makedirs(userdir)
    await q.edit_message_reply_markup(
        InlineKeyboardMarkup(
            [[InlineKeyboardButton("Downloading...", callback_data="down")]]))
    filepath = os.path.join(userdir, filext)
    # await q.edit_message_reply_markup([[InlineKeyboardButton("Processing..")]])

    audio_command = [
        "youtube-dl",
        "-c",
        "--prefer-ffmpeg",
        "--extract-audio",
        "--audio-format",
        "mp3",
        "--audio-quality",
        format_id,
        "-o",
        filepath,
        yturl,
    ]

    video_command = [
        "youtube-dl", "-c", "--embed-subs", "-f", f"{format_id}+bestaudio",
        "-o", filepath, "--hls-prefer-ffmpeg", yturl
    ]

    loop = asyncio.get_event_loop()

    med = None
    if cb_data.startswith("audio"):
        filename = await downloadaudiocli(audio_command)
        med = InputMediaAudio(media=filename,
                              thumb=thumb_image_path,
                              caption=os.path.basename(filename),
                              title=os.path.basename(filename))

    if cb_data.startswith("video"):
        filename = await downloadvideocli(video_command)
        dur = round(duration(filename))
        med = InputMediaVideo(media=filename,
                              duration=dur,
                              width=width,
                              height=height,
                              thumb=thumb_image_path,
                              caption=os.path.basename(filename),
                              supports_streaming=True)

    if cb_data.startswith("docaudio"):
        filename = await downloadaudiocli(audio_command)
        med = InputMediaDocument(
            media=filename,
            thumb=thumb_image_path,
            caption=os.path.basename(filename),
        )

    if cb_data.startswith("docvideo"):
        filename = await downloadvideocli(video_command)
        dur = round(duration(filename))
        med = InputMediaDocument(
            media=filename,
            thumb=thumb_image_path,
            caption=os.path.basename(filename),
        )
    if med:
        loop.create_task(send_file(c, q, med, filename))
    else:
        print("med not found")