diff --git a/.env.example b/.env.example index c46b9245..a8c7d403 100644 --- a/.env.example +++ b/.env.example @@ -64,3 +64,6 @@ CAPTION_URL_LENGTH_LIMIT=150 POTOKEN=11 BROWSERS=firefox + +# Proxy URL for downloads (e.g., http://proxy.example.com:8080 or socks5://proxy.example.com:1080) +PROXY= diff --git a/README.md b/README.md index 56e5e3e4..342bd085 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ This bot can be deployed on any platform that supports Python. - `FREE_DOWNLOAD`: Free downloads allowed per user - `RATE_LIMIT`: Rate limit for requests - `TMPFILE_PATH`: Path for temporary/download files (ensure the directory exists and is writable) + - `PROXY`: Proxy URL for downloads (e.g., http://proxy.example.com:8080 or socks5://proxy.example.com:1080) - `TG_NORMAL_MAX_SIZE`: Maximum size for Telegram uploads in MB - `CAPTION_URL_LENGTH_LIMIT`: Maximum URL length in captions - `POTOKEN`: Your PO Token. [PO-Token-Guide](https://github.com/yt-dlp/yt-dlp/wiki/PO-Token-Guide) diff --git a/src/config/config.py b/src/config/config.py index ccf130f9..7ede6a93 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -42,6 +42,9 @@ def get_env(name: str, default=None): RCLONE_PATH = get_env("RCLONE") +# proxy settings +PROXY = get_env("PROXY") + # payment settings ENABLE_VIP = get_env("ENABLE_VIP") PROVIDER_TOKEN = get_env("PROVIDER_TOKEN") diff --git a/src/engine/generic.py b/src/engine/generic.py index bbcf3d7b..dfcd099c 100644 --- a/src/engine/generic.py +++ b/src/engine/generic.py @@ -9,7 +9,7 @@ import yt_dlp -from config import AUDIO_FORMAT +from config import AUDIO_FORMAT, PROXY from utils import is_youtube from database.model import get_format_settings, get_quality_settings from engine.base import BaseDownloader @@ -104,6 +104,9 @@ def _download(self, formats) -> list: "embed_thumbnail": True, "writethumbnail": False, } + # add proxy if configured + if PROXY: + ydl_opts["proxy"] = PROXY # setup cookies for youtube only if is_youtube(self._url): # use cookies from browser firstly diff --git a/src/main.py b/src/main.py index 67759d0b..bda0a800 100644 --- a/src/main.py +++ b/src/main.py @@ -34,6 +34,7 @@ OWNER, PROVIDER_TOKEN, TOKEN_PRICE, + PROXY, BotText, ) from database.model import ( @@ -341,7 +342,10 @@ def ytdl_handler(client: Client, message: types.Message): def check_link(url: str): - ytdl = yt_dlp.YoutubeDL() + opts = {} + if PROXY: + opts["proxy"] = PROXY + ytdl = yt_dlp.YoutubeDL(opts) if re.findall(r"^https://www\.youtube\.com/channel/", url) or "list" in url: # TODO maybe using ytdl.extract_info raise ValueError("Playlist or channel download are not supported at this moment.") diff --git a/src/test.py b/src/test.py index f8a0f71e..2d513237 100644 --- a/src/test.py +++ b/src/test.py @@ -4,12 +4,15 @@ # ytdlbot - test.py import yt_dlp +from config import PROXY url = "https://www.youtube.com/watch?v=e19kTVgb2c8" opts = { "cookiefile": "cookies.txt", "cookiesfrombrowser": ["firefox"], } +if PROXY: + opts["proxy"] = PROXY with yt_dlp.YoutubeDL(opts) as ydl: ydl.download([url])