clipwright/tests/test_download_plan.py
juancarlos.cavero 22c991d22b 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.
2026-08-27 18:57:37 +02:00

60 lines
2.3 KiB
Python

"""Download attempt ordering: cheapest bandwidth first.
Direct (server IP) -> flat-rate static ISP proxies (uncapped 1080p, free
bytes) -> per-GB paid proxy (720p cost cap) -> conservative fallback. The
``capped`` flag marks the attempts whose bytes are billed per GB (drives the
720p format cap and the PROXY_BYTES monthly counter).
"""
import pytest
main = pytest.importorskip("main")
plan = main.plan_download_attempts
STATICS = ["http://s1", "http://s2", "http://s3"]
PAID = "http://paid"
class TestOrdering:
def test_full_chain(self):
got = plan(True, STATICS, PAID, True)
assert got == [
('HD-direct', False, None),
('HD-static1', False, "http://s1"),
('HD-static2', False, "http://s2"),
('HD-static3', False, "http://s3"),
('HD', True, PAID),
('fallback', True, PAID),
]
def test_statics_only_fallback_uses_a_static(self):
got = plan(False, STATICS, None, True)
assert got[-1] == ('fallback', False, "http://s1")
assert ('HD', False, None) in got # direct HD still tried, uncapped
def test_selfhost_no_proxies_matches_legacy_chain(self):
assert plan(False, [], None, True) == [
('HD', False, None),
('fallback', False, None),
]
def test_no_hd_args_still_has_fallback(self):
assert plan(False, [], PAID, False) == [('fallback', True, PAID)]
def test_paid_attempts_are_the_only_capped_ones(self):
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")