mirror of
https://github.com/tgbot-collection/ytdlbot.git
synced 2025-02-24 23:34:44 +08:00
add download hook
This commit is contained in:
parent
c486fe8301
commit
11648248cd
3 changed files with 14 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -56,7 +56,7 @@ PLAYLIST_SUPPORT = os.getenv("PLAYLIST_SUPPORT", False)
|
|||
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.
|
||||
|
@ -71,14 +71,14 @@ COFFEE_TOKEN = os.getenv("COFFEE_TOKEN")
|
|||
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):
|
||||
|
|
|
@ -23,6 +23,12 @@ app = Client("premium", APP_ID, APP_HASH, workers=PYRO_WORKERS)
|
|||
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
|
||||
|
@ -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",
|
||||
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue