This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
docker compose build ytdl-bot && docker compose up -d ytdl-bot
docker compose logs -f ytdl-botpython -m pytest tests/
python -m pytest tests/test_downloader_utils.py # single test fileA Telegram bot that downloads videos/audio via yt-dlp, runs as a Docker container alongside a local Telegram Bot API server and a Cloudflare WARP proxy.
- handlers.py — Parses commands, validates permissions, creates a
status_msg, pushes(chat_id, url, message_id, quality, status_msg, ...)tuples into async queues - queue_processor.py — Consumes queues sequentially. Runs yt-dlp in
run_in_executor(blocking I/O). Manages live stream recording via streamlink subprocesses. Handles segmented upload for large files. - downloader.py — yt-dlp wrapper with proxy rotation.
_apply_cookie()is the central hook for all yt-dlp invocations (injects cookies, EJS runtime, logging).RETRY_ERRORS+is_retryable_error()control which errors trigger proxy fallover and WARP IP rotation. - uploader.py — Handles chunked streaming upload to Local Bot API, file splitting via ffmpeg, thumbnail cropping.
- subscription.py —
SubscriptionMonitorpolls channels every N seconds, deduplicates viais_video_processed()in SQLite, tracks active live recordings to prevent duplicates.
- Single status message: One message per task, edited in-place. Never spam multiple messages. Pass
status_msgthrough the queue. - tg_retry(): All Telegram API calls in high-frequency paths must use this wrapper (handles flood control + NetworkError).
- Proxy rotation:
get_proxy_list()returns ordered list. On retryable errors, iterate to next proxy. WARP proxy failures trigger/restartendpoint for IP rotation. - yt-dlp is synchronous: Always wrap in
loop.run_in_executor(None, lambda: ...). - Memory management:
PYTHONMALLOC=mallocin Dockerfile (musl returns pages to OS on free)._free_memory()called after every task. - Live stream recording: Starts streamlink for the live edge and yt-dlp
--live-from-startin parallel. If the from-start archive records normally for 10 minutes, streamlink is stopped and its duplicate temporary files are discarded; only the full from-start archive continues. A missing DVR/VOD only stops the from-start archive, so the live-edge recorder remains as the fallback. Both use 1.9GB segments. Graceful shutdown: SIGINT → SIGTERM → SIGKILL.
config.pyreads all settings from env vars (BOT_TOKEN, API_URL, PROXY_LIST, ALLOWED_CHAT_IDS, etc.)- Cookie file at
./data/cookies.txtenables EJS challenge solver (deno runtime at/opt/deno/bin, only added to PATH when cookies are present) - Local Bot API allows uploads up to ~1.95GB; standard API caps at 49MB
- Docker compose: ytdl-bot (mem_limit 512m, swap to 1g), telegram-bot-api, warp-proxy, autoheal
SQLite at ./data/subscriptions.db. Tables: subscriptions (channel monitoring), processed_videos (deduplication).
The user communicates in Traditional Chinese (zh-TW). Respond in Chinese unless code/technical terms are involved.