From 04fbf7cdd74f2fcf6bac6a02a8c790669370b5d6 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Tue, 4 May 2021 11:30:22 +0800 Subject: [PATCH] add progress bar --- .gitignore | 1 + Dockerfile | 10 ++++++++-- bot.py | 52 +++++++++++++++++++++++++++++++++++------------- requirements.txt | 3 ++- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index b936060..84fa2cc 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,4 @@ dmypy.json /.idea/vcs.xml /.idea/ytdl-bot.iml /.idea/misc.xml +/.idea/workspace.xml diff --git a/Dockerfile b/Dockerfile index 311dfb8..2b17165 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,14 @@ FROM python:alpine as builder -RUN apk update && apk add --no-cache tzdata ca-certificates +RUN apk update && apk add --no-cache tzdata alpine-sdk libffi-dev ca-certificates ADD requirements.txt /tmp/ -RUN pip3 install -r /tmp/requirements.txt && rm /tmp/requirements.txt +RUN pip3 install --user -r /tmp/requirements.txt && rm /tmp/requirements.txt + + +FROM python:alpine +COPY --from=builder /root/.local /usr/local +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo COPY . /ytdl-bot WORKDIR /ytdl-bot diff --git a/bot.py b/bot.py index 537e686..079de97 100644 --- a/bot.py +++ b/bot.py @@ -10,8 +10,11 @@ __author__ = "Benny " import tempfile import os import logging -import youtube_dl +import threading +import asyncio import traceback + +import youtube_dl from telethon import TelegramClient, events from tgbot_ping import get_runtime @@ -23,19 +26,37 @@ app_hash = os.getenv("APP_HASH") or "490" bot = TelegramClient('bot', app_id, app_hash).start(bot_token=token) -def download_callback(current, total): - logging.info('Downloaded %s out of %s, %.2f%%', current, total, current / total * 100) +async def upload_callback(current, total, chat_id, message): + msg = f'Uploading {current / total * 100}: {current}/{total}' + await bot.edit_message(chat_id, message, msg) -def upload_callback(current, total): - logging.info('Uploaded %s out of %s, %.2f%%', current, total, current / total * 100) +async def sync_edit_message(chat_id, message, msg): + await bot.edit_message(chat_id, message, msg) -def ytdl_download(url, tempdir) -> dict: +def go(chat_id, message, msg): + asyncio.run(sync_edit_message(chat_id, message, msg)) + + +def progress_hook(d: dict, chat_id, message): + if d['status'] == 'downloading': + downloaded = d["downloaded_bytes"] + total = d["total_bytes"] + percent = d["_percent_str"] + speed = d["_speed_str"] + msg = f'Downloading {percent}: {downloaded}/{total} @ {speed}' + threading.Thread(target=go, args=(chat_id, message, msg)).start() + + +def ytdl_download(url, tempdir, chat_id, message) -> dict: response = dict(status=None, error=None, filepath=None) os.chdir(tempdir) logging.info("Downloading for %s", url) - ydl_opts = {} + ydl_opts = { + 'progress_hooks': [lambda d: progress_hook(d, chat_id, message)], + 'quiet': True + } try: with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) @@ -77,18 +98,21 @@ async def send_welcome(event): async def echo_all(event): chat_id = event.message.chat_id url = event.message.text - message = await event.reply("Downloading...") - + message = await event.reply("Processing...") temp_dir = tempfile.TemporaryDirectory() - async with bot.action(event.chat_id, 'record-video'): - result = ytdl_download(url, temp_dir.name) + + async with bot.action(chat_id, 'video'): + result = ytdl_download(url, temp_dir.name, chat_id, message) + if result["status"]: - async with bot.action(event.chat_id, 'document'): + async with bot.action(chat_id, 'document'): video_path = result["filepath"] - await bot.send_file(chat_id, video_path, progress_callback=upload_callback) + await bot.edit_message(chat_id, message, 'Download complete. Sending now...') + await bot.send_file(chat_id, video_path, + progress_callback=lambda x, y: upload_callback(x, y, chat_id, message)) await bot.edit_message(chat_id, message, 'Download success!✅') else: - async with bot.action(event.chat_id, 'typing'): + async with bot.action(chat_id, 'typing'): tb = result["error"] await bot.edit_message(chat_id, message, f"{url} download failed❌:\n```{tb}```", parse_mode='markdown') diff --git a/requirements.txt b/requirements.txt index bfa0817..ad0e984 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ telethon youtube-dl -tgbot-ping \ No newline at end of file +tgbot-ping +cryptg \ No newline at end of file