clipwright/tests/test_yt_clients.py
juancarlos.cavero 959407c30e fix(download): explicit default,mweb clients so account cookies stop yielding "Video unavailable"
With account cookies yt-dlp's authed defaults are tv_downgraded + web, and on
a share of videos both come back UNPLAYABLE / SABR-only, reported as "Video
unavailable" with no formats. That happened on every static IP too, so it was
mistaken for an IP ban and fed ~26 downloads a week to the per-GB proxy,
which then fetched 360p through the same dead client list. Measured in the
prod container on 6-sep-2026, same static proxy, same video: cookies +
defaults -> unavailable; cookies + default,mweb (+ PO token) -> 1080p; no
cookies -> 1080p.

- yt_clients.py: one client list (default,mweb + bgutil provider) shared by
  the download and the duration probe. Never player_skip=webpage: it drops
  the Data Sync ID and mweb loses its PO token (360p only).
- main.py: the fallback list (tv_embed, android) is gone from yt-dlp; the
  fallback now runs anonymously when the cookie attempt already failed on
  that route, and every attempt asks for the 1080p spec (the fallback's
  best[ext=mp4] was itself the 360p progressive file).
- metering: an uploader's country block is a content error; the residential
  pool failed identically in 5 of 6 paid probes (3-5 sep). The probe alert
  now says when the paid proxy failed too.

Claude-Session: https://claude.ai/code/session_013ubPhpBw8kAUcLXCCfH4C5
2026-09-06 03:43:46 +02:00

66 lines
2.7 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")
assert seen["strategies"] == [
yt_clients.hd_extractor_args("", "/opt/gen.js"),
yt_clients.fallback_extractor_args("", "/opt/gen.js"),
]