Skip to content

Commit

Permalink
add download hook
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Dec 20, 2023
1 parent c486fe8 commit 1164824
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Websites [supported by yt-dlp](https://github.com/yt-dlp/yt-dlp/blob/master/supp

Due to limitations on servers and bandwidth, there are some restrictions on this free service.

* Each user is limited to 20 free downloads per 24-hour period
* Each user is limited to 10 free downloads per 24-hour period
* Maximum of three subscriptions allowed for YouTube channels.

If you need more downloads, you can purchase additional tokens. Additionally, you have the option of deploying your
Expand Down
6 changes: 3 additions & 3 deletions ytdlbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
M3U8_SUPPORT = os.getenv("M3U8_SUPPORT", False)
ENABLE_ARIA2 = os.getenv("ENABLE_ARIA2", False)

RATE_LIMIT = os.getenv("RATE_LIMIT", 120)
RATE_LIMIT = os.getenv("RATE_LIMIT", 60)
RCLONE_PATH = os.getenv("RCLONE")
# This will set the value for the tmpfile path(download path) if it is set.
# If TMPFILE is not set, it will return None and use system’s default temporary file path.
Expand All @@ -71,14 +71,14 @@
AFD_TOKEN = os.getenv("AFD_TOKEN")
AFD_USER_ID = os.getenv("AFD_USER_ID")
PROVIDER_TOKEN = os.getenv("PROVIDER_TOKEN") or "1234"
FREE_DOWNLOAD = os.getenv("FREE_DOWNLOAD", 20)
FREE_DOWNLOAD = os.getenv("FREE_DOWNLOAD", 10)
TOKEN_PRICE = os.getenv("BUY_UNIT", 20) # one USD=20 credits
TRONGRID_KEY = os.getenv("TRONGRID_KEY", "").split(",")
# the default mnemonic is for nile testnet
TRON_MNEMONIC = os.getenv("TRON_MNEMONIC", "cram floor today legend service drill pitch leaf car govern harvest soda")
TRX_SIGNAL = signal("trx_received")

PREMIUM_USER = int(os.getenv("PREMIUM_USER"))
PREMIUM_USER = int(os.getenv("PREMIUM_USER", "0"))


class FileTooBig(Exception):
Expand Down
13 changes: 10 additions & 3 deletions ytdlbot/premium.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
BOT_ID = int(TOKEN.split(":")[0])


def download_hook(d: dict):
downloaded = d.get("downloaded_bytes", 0)
total = d.get("total_bytes") or d.get("total_bytes_estimate", 0)
print(downloaded, total)


@app.on_message(filters.user(BOT_ID) & filters.incoming)
async def hello(client: Client, message: types.Message):
text = message.text
Expand All @@ -35,7 +41,7 @@ async def hello(client: Client, message: types.Message):

tempdir = tempfile.TemporaryDirectory(prefix="ytdl-")
output = pathlib.Path(tempdir.name, "%(title).70s.%(ext)s").as_posix()
ydl_opts = {"restrictfilenames": False, "quiet": True, "outtmpl": output}
ydl_opts = {"restrictfilenames": False, "quiet": True, "outtmpl": output, "progress_hooks": [download_hook]}
formats = [
# webm , vp9 and av01 are not streamable on telegram, so we'll extract only mp4
"bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio",
Expand All @@ -50,12 +56,13 @@ async def hello(client: Client, message: types.Message):
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
break
except Exception:
logging.error("Download failed for %s. Try other options...", url)
except Exception as e:
logging.error("Download failed for %s: %s", url, e)

payment = Payment()
settings = payment.get_user_settings(user_id)
video_path = next(pathlib.Path(tempdir.name).glob("*"))
logging.info("filesize: %s", video_path.stat().st_size)
if settings[2] == "video" or isinstance(settings[2], MagicMock):
logging.info("Sending as video")
await client.send_video(
Expand Down

0 comments on commit 1164824

Please sign in to comment.