clipwright/tests/test_yt_clients.py
juancarlos.cavero d13fb8e359 fix(probe): retry the duration probe anonymously before paying per GB
With YOUTUBE_COOKIES attached, YouTube answers UNPLAYABLE for every client
(web_embedded, tv_downgraded, web AND mweb) on a share of videos, and yt-dlp
reports that as "Video unavailable". Measured in prod on 9-sep-2026 with
e_04ZrNroTo, same static IP minutes apart: cookies -> UNPLAYABLE on all three
statics; anonymous -> 1080p (137+140). The 6-sep default,mweb fix is no longer
enough on its own.

The download already recovers from this through its anonymous fallback-static
attempt. The probe carried the cookies on every route including the paid one,
so it read a cookie problem as an IP problem ("video unavailable" is in
_IP_SPECIFIC_HINTS) and escalated to DataImpulse, which fails identically with
the same cookies. That is what fired the two paid-proxy alerts that morning,
and a probe that dies outright 400s a video the download would have fetched.

Probe strategies are now (extractor_args, use_cookies): first attempt on a
route authenticated, second anonymous, mirroring the download. With no HD path
(self-host without a PO token provider) the fallback keeps the operator's
cookies, since it is then the only attempt.

Claude-Session: https://claude.ai/code/session_01Qfgs1gca8UeEEZ2pvjjC8i
2026-09-09 12:12:45 +02:00

69 lines
2.9 KiB
Python

"""YouTube client selection shared by the download and the duration probe.
Measured 6-sep-2026 in the prod container, same static proxy, same video:
cookies + yt-dlp's authed defaults -> "Video unavailable" (no formats);
cookies + mweb with a PO token, or no cookies at all -> 1080p. The explicit
`default,mweb` list is what keeps those videos off the per-GB proxy.
"""
import pytest
yt_clients = pytest.importorskip("yt_clients")
def test_hd_needs_a_pot_provider():
assert yt_clients.hd_extractor_args("", "") is None
def test_hd_lists_default_and_mweb_with_the_provider():
got = yt_clients.hd_extractor_args("", "/opt/gen.js")
assert got["youtube"]["player_client"] == ["default", "mweb"]
assert got["youtubepot-bgutilscript"] == {"script_path": ["/opt/gen.js"]}
http = yt_clients.hd_extractor_args("http://pot:4416", "/opt/gen.js")
assert http["youtubepot-bgutilhttp"] == {"base_url": ["http://pot:4416"]}
assert "youtubepot-bgutilscript" not in http
def test_fallback_never_skips_the_webpage():
# player_skip=webpage drops the account's Data Sync ID and mweb then
# cannot get its PO token: only the 360p progressive format survives.
for args in (yt_clients.fallback_extractor_args("", ""),
yt_clients.fallback_extractor_args("", "/opt/gen.js")):
assert "player_skip" not in args["youtube"]
assert "mweb" in args["youtube"]["player_client"]
for dead in ("tv_embed", "android"):
assert dead not in args["youtube"]["player_client"]
def test_fallback_keeps_the_provider_when_configured():
assert "youtubepot-bgutilscript" in yt_clients.fallback_extractor_args("", "/x.js")
assert "youtubepot-bgutilscript" not in yt_clients.fallback_extractor_args("", "")
def test_lists_are_copies():
a = yt_clients.hd_extractor_args("", "/x.js")["youtube"]["player_client"]
a.append("web_safari")
assert yt_clients.hd_extractor_args("", "/x.js")["youtube"]["player_client"] == ["default", "mweb"]
def test_probe_uses_the_same_lists(monkeypatch):
metering = pytest.importorskip("cloud.metering")
monkeypatch.setenv("BGUTIL_SCRIPT_PATH", "/opt/gen.js")
monkeypatch.delenv("BGUTIL_BASE_URL", raising=False)
monkeypatch.setenv("STATIC_PROXY_URLS", "")
monkeypatch.setenv("PROXY_URL", "")
monkeypatch.delenv("YOUTUBE_COOKIES", raising=False)
seen = {}
def fake(url, proxies, strategies, static_errors, paid, ck_path):
seen["strategies"] = strategies
return 1.0
monkeypatch.setattr(metering, "_probe_with_proxies", fake)
metering.probe_url_minutes("https://www.youtube.com/watch?v=abc")
# (extractor args, send the cookies): the fallback step goes out anonymously,
# like the download's 'fallback-static' attempt — see
# tests/test_probe_anonymous_fallback.py.
assert seen["strategies"] == [
(yt_clients.hd_extractor_args("", "/opt/gen.js"), True),
(yt_clients.fallback_extractor_args("", "/opt/gen.js"), False),
]