fix(download): direct file URLs bypass the proxy chain
The ISP/paid proxies exist to dodge YouTube's ban on the server IP. A direct mp4 (catbox, tmpfiles, a CDN, an R2 link) has nothing to dodge and crawled at ~9 KB/s through the static proxy (6 MB in 11 min on 27-aug) while the server's own IP pulls 17 MB/s. Non-YouTube hosts now download direct, with one static proxy as the only fallback and never the per-GB one.
This commit is contained in:
parent
15195f591a
commit
22c991d22b
2 changed files with 37 additions and 2 deletions
26
main.py
26
main.py
|
|
@ -662,14 +662,34 @@ def sanitize_filename(filename):
|
||||||
return truncate_bytes(filename, MAX_TITLE_BYTES)
|
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.
|
"""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
|
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
|
ISP proxies (uncapped 1080p, free bytes), then the per-GB paid proxy
|
||||||
(720p cost cap), and last the conservative fallback strategy through the
|
(720p cost cap), and last the conservative fallback strategy through the
|
||||||
paid proxy (or a static/direct when no paid proxy is configured).
|
paid proxy (or a static/direct when no paid proxy is configured).
|
||||||
``capped`` marks attempts whose bytes are billed per GB."""
|
``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 = []
|
plan = []
|
||||||
if direct_first:
|
if direct_first:
|
||||||
plan.append(('HD-direct', False, None))
|
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),
|
fallback_fmt if label == 'fallback' else _hd_fmt_for(capped),
|
||||||
proxy)
|
proxy)
|
||||||
for label, capped, proxy in plan_download_attempts(
|
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
|
sanitized_title = None
|
||||||
last_err = None
|
last_err = None
|
||||||
|
|
|
||||||
|
|
@ -45,3 +45,16 @@ class TestOrdering:
|
||||||
got = plan(True, STATICS, PAID, True)
|
got = plan(True, STATICS, PAID, True)
|
||||||
for _label, capped, proxy in got:
|
for _label, capped, proxy in got:
|
||||||
assert capped == (proxy == PAID)
|
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")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue