Rate limit only background batch fetches, not user requests
fetch_video_metadata and fetch_channel_metadata now take polite=True for background tasks (enforces 5s+ gap via global lock) while user-facing calls (watch page, follow channel, download) use polite=False and run immediately. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -244,8 +244,12 @@ def _best_thumbnail(thumbnails: list | None) -> str | None:
|
||||
return best[0].get("url") if best else None
|
||||
|
||||
|
||||
def fetch_video_metadata(video_id: str) -> dict | None:
|
||||
"""Fetch metadata for a single video by YouTube ID."""
|
||||
def fetch_video_metadata(video_id: str, polite: bool = False) -> dict | None:
|
||||
"""Fetch metadata for a single video by YouTube ID.
|
||||
|
||||
polite=True applies the global rate limiter (for background batch tasks).
|
||||
polite=False (default) runs immediately for user-facing requests.
|
||||
"""
|
||||
url = f"https://www.youtube.com/watch?v={video_id}"
|
||||
cookie_args = _cookie_args()
|
||||
print(f"[fetch_meta] video={video_id} cookie_args={cookie_args!r}", flush=True)
|
||||
@@ -253,13 +257,13 @@ def fetch_video_metadata(video_id: str) -> dict | None:
|
||||
"yt-dlp", url,
|
||||
"--dump-json", "--no-download", "--no-playlist",
|
||||
]
|
||||
stdout, stderr, code = _meta_run([*base_cmd, *cookie_args], timeout=30)
|
||||
runner = _meta_run if polite else _run
|
||||
stdout, stderr, code = runner([*base_cmd, *cookie_args], timeout=30)
|
||||
if code != 0:
|
||||
print(f"[fetch_meta] FAILED code={code} stderr={stderr[:500]!r}", flush=True)
|
||||
# Retry without auth args — broken cookie config shouldn't block public videos
|
||||
if cookie_args:
|
||||
print(f"[fetch_meta] retrying without cookie args", flush=True)
|
||||
stdout, stderr, code = _meta_run(base_cmd, timeout=30)
|
||||
stdout, stderr, code = runner(base_cmd, timeout=30)
|
||||
if code != 0:
|
||||
print(f"[fetch_meta] retry also FAILED code={code}", flush=True)
|
||||
|
||||
@@ -307,7 +311,7 @@ def _rss_dates(uc_channel_id: str) -> dict[str, datetime]:
|
||||
return {}
|
||||
|
||||
|
||||
def fetch_channel_metadata(channel_id: str, max_videos: int = 30, start_video: int = 1) -> dict | None:
|
||||
def fetch_channel_metadata(channel_id: str, max_videos: int = 30, start_video: int = 1, polite: bool = False) -> dict | None:
|
||||
"""Fetch channel info + recent videos.
|
||||
|
||||
Uses --dump-single-json --flat-playlist for speed, then enriches video dates
|
||||
@@ -330,7 +334,8 @@ def fetch_channel_metadata(channel_id: str, max_videos: int = 30, start_video: i
|
||||
end = (start_video - 1 + max_videos) if start_video > 1 else max_videos
|
||||
args += ["--playlist-end", str(end)]
|
||||
|
||||
stdout, _, code = _meta_run(args, timeout=60)
|
||||
runner = _meta_run if polite else _run
|
||||
stdout, _, code = runner(args, timeout=60)
|
||||
if not stdout.strip():
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user