fix(download): free fallback-static attempt before any per-GB proxy

Since the static IPs moved to three countries, every remaining paid rescue
had the same shape: all three statics answer 'Video unavailable' and the
paid fallback then downloads the video fine. Tested on the morning's case
(2eCIMQMy0AQ): the video is fine from a residential IP, the HD/web client
gets the fake unavailable on all three statics, and the conservative
clients (tv_embed/android) pass on those very same IPs. So it is the
client, not the IP, and the fix is free: try the fallback strategy through
a static before spending a per-GB byte. No quality trade: the paid path
was capped at 720p anyway.

Claude-Session: https://claude.ai/code/session_014i9z6mLTy978uRCfSsMfBq
This commit is contained in:
juancarlos.cavero 2026-09-04 11:15:08 +02:00
parent 3e6d5c3bee
commit a85a41bb6a
3 changed files with 27 additions and 3 deletions

View file

@ -336,7 +336,10 @@ Async job queue with semaphore-based concurrency control. Configure via `MAX_CON
Downloads go direct → static ISP proxies (flat rate) → DataImpulse (per GB), Downloads go direct → static ISP proxies (flat rate) → DataImpulse (per GB),
and the duration probe (`cloud/metering.probe_url_minutes`) follows the same and the duration probe (`cloud/metering.probe_url_minutes`) follows the same
order. Two rules keep the per-GB proxy at zero on a normal day: the probe order, with one extra free step before any per-GB attempt: the conservative
clients (tv_embed/android) through a static (`fallback-static`), because
YouTube serves the HD/web client a fake "Video unavailable" from
datacenter-ISP IPs on some videos while those clients pass on the same IPs. Two rules keep the per-GB proxy at zero on a normal day: the probe
reaches it **only** when a static route failed for a reason another IP can reaches it **only** when a static route failed for a reason another IP can
fix (`static_failure_warrants_paid`: bot-check, 403/429, proxy/network fix (`static_failure_warrants_paid`: bot-check, 403/429, proxy/network
errors), never for a private/removed/members-only video or a live stream errors), never for a private/removed/members-only video or a live stream

13
main.py
View file

@ -707,6 +707,15 @@ def plan_download_attempts(direct_first, statics, paid, have_hd, youtube=True):
if have_hd: if have_hd:
for i, s in enumerate(statics): for i, s in enumerate(statics):
plan.append((f'HD-static{i + 1}', False, s)) plan.append((f'HD-static{i + 1}', False, s))
if statics and paid:
# The conservative clients (tv_embed/android) through a FREE static,
# before any per-GB attempt: YouTube serves a fake "Video unavailable"
# to the web/HD client from datacenter-ISP ranges on some videos while
# the fallback clients pass on the very same IPs (verified 4-sep-2026,
# all three statics, three countries). Costs nothing and the paid path
# was capped to 720p anyway, so there is no quality trade.
plan.append(('fallback-static', False, statics[0]))
if have_hd:
plan.append(('HD', bool(paid), paid)) plan.append(('HD', bool(paid), paid))
plan.append(('fallback', bool(paid), plan.append(('fallback', bool(paid),
paid if paid else (statics[0] if statics else None))) paid if paid else (statics[0] if statics else None)))
@ -863,8 +872,8 @@ def download_youtube_video(url, output_dir="."):
attempts = [ attempts = [
(label, (label,
fallback_args if label == 'fallback' else hd_args, fallback_args if label.startswith('fallback') else hd_args,
fallback_fmt if label == 'fallback' else _hd_fmt_for(capped), fallback_fmt if label.startswith('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), youtube=is_youtube_url(url)) _direct_first, _statics, _proxy, bool(hd_args), youtube=is_youtube_url(url))

View file

@ -23,6 +23,10 @@ class TestOrdering:
('HD-static1', False, "http://s1"), ('HD-static1', False, "http://s1"),
('HD-static2', False, "http://s2"), ('HD-static2', False, "http://s2"),
('HD-static3', False, "http://s3"), ('HD-static3', False, "http://s3"),
# Free rescue for the fake "Video unavailable" the HD client gets
# from datacenter-ISP IPs (4-sep-2026): conservative clients
# through a static BEFORE any per-GB attempt.
('fallback-static', False, "http://s1"),
('HD', True, PAID), ('HD', True, PAID),
('fallback', True, PAID), ('fallback', True, PAID),
] ]
@ -47,6 +51,14 @@ class TestOrdering:
assert capped == (proxy == PAID) assert capped == (proxy == PAID)
def test_fallback_static_only_exists_when_paid_is_configured():
# Without a paid proxy the final fallback already runs through a static;
# a fallback-static before it would be the same attempt twice.
got = plan(False, STATICS, None, True)
assert ('fallback-static', False, "http://s1") not in got
assert got[-1] == ('fallback', False, "http://s1")
def test_direct_file_urls_skip_the_proxy_chain(): def test_direct_file_urls_skip_the_proxy_chain():
"""A catbox/tmpfiles/CDN mp4 has no YouTube ban to dodge: own IP first, """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 one static as fallback, never the per-GB proxy (prod 27-aug: 9 KB/s