diff --git a/main.py b/main.py index bcaf96f..9a91f07 100644 --- a/main.py +++ b/main.py @@ -662,14 +662,34 @@ def sanitize_filename(filename): return truncate_bytes(filename, MAX_TITLE_BYTES) -def plan_download_attempts(direct_first, statics, paid, have_hd): +def is_youtube_url(url): + """True for the hosts the proxy chain exists for. Anything else (a CDN + mp4, tmpfiles/catbox, an R2 link) has no IP ban to dodge and downloads + 5-10x faster from the server's own IP than through the ISP proxies.""" + try: + from urllib.parse import urlparse + host = (urlparse(url).hostname or "").lower() + except Exception: + return True + return host.endswith(("youtube.com", "youtu.be", "youtube-nocookie.com", "googlevideo.com")) + + +def plan_download_attempts(direct_first, statics, paid, have_hd, youtube=True): """Ordered (label, capped, proxy) download plan — pure, unit-tested. + ``youtube=False`` (a direct file URL): the server's own IP first, then one + static proxy as the only fallback; the paid per-GB proxy is never used. + Cheapest bandwidth first: the server's own IP, then the flat-rate static ISP proxies (uncapped 1080p, free bytes), then the per-GB paid proxy (720p cost cap), and last the conservative fallback strategy through the paid proxy (or a static/direct when no paid proxy is configured). ``capped`` marks attempts whose bytes are billed per GB.""" + if not youtube: + plan = [('direct', False, None)] + if statics: + plan.append(('static-fallback', False, statics[0])) + return plan plan = [] if direct_first: plan.append(('HD-direct', False, None)) @@ -830,8 +850,10 @@ def download_youtube_video(url, output_dir="."): fallback_fmt if label == 'fallback' else _hd_fmt_for(capped), proxy) for label, capped, proxy in plan_download_attempts( - _direct_first, _statics, _proxy, bool(hd_args)) + _direct_first, _statics, _proxy, bool(hd_args), youtube=is_youtube_url(url)) ] + if not is_youtube_url(url): + print("🌐 Direct file URL: downloading from the server's own IP (no proxy).") sanitized_title = None last_err = None diff --git a/tests/test_download_plan.py b/tests/test_download_plan.py index 5e33d82..0eb084b 100644 --- a/tests/test_download_plan.py +++ b/tests/test_download_plan.py @@ -45,3 +45,16 @@ class TestOrdering: got = plan(True, STATICS, PAID, True) for _label, capped, proxy in got: assert capped == (proxy == PAID) + + +def test_direct_file_urls_skip_the_proxy_chain(): + """A catbox/tmpfiles/CDN mp4 has no YouTube ban to dodge: own IP first, + one static as fallback, never the per-GB proxy (prod 27-aug: 9 KB/s + through the ISP proxy vs 17 MB/s direct).""" + got = plan(False, ["s1", "s2"], "paid", True, youtube=False) + assert got == [("direct", False, None), ("static-fallback", False, "s1")] + assert plan(False, [], "paid", True, youtube=False) == [("direct", False, None)] + assert main.is_youtube_url("https://www.youtube.com/watch?v=x") + assert main.is_youtube_url("https://youtu.be/x") + assert not main.is_youtube_url("https://litter.catbox.moe/u90j4q.mp4") + assert not main.is_youtube_url("https://tmpfiles.org/dl/1/2/v.mp4")