Spaces:
Running
Running
feat(download): hỗ trợ cookies TikTok cho yt-dlp (qua bot-check từ IP server)
Browse filesChẩn đoán sâu lỗi deploy: kể cả yt-dlp mới nhất, extractor TikTok chỉ trả 1
format AUDIO (0 video) cho link ads; tự bóc trang cũng chỉ nhận trang
"verify/challenge" ~1.4KB (TikTok chặn khách vãng lai/IP datacenter). Đường ra
video duy nhất phải qua IP không phải datacenter: proxy (đã thêm) HOẶC phiên
đăng nhập.
Thêm _tiktok_cookiefile(): đọc TIKTOK_COOKIES_FILE / TIKTOK_COOKIES_CONTENT
(giống cơ chế cookies YouTube) và nạp vào yt-dlp cho nguồn tiktok/tiktok_photo
-> cookies phiên đã đăng nhập giúp lấy trang đầy đủ = có luồng video. Tuỳ chọn,
thiếu thì bỏ qua êm.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
195 unmodified lines19619719819920020166 unmodified lines268269270271272273195 unmodified linesreturn {"http": p, "https": p} if p else Nonedef _youtube_cookiefile() -> str | None:"""Trả về đường dẫn file cookies YouTube để yt-dlp dùng.66 unmodified linesor os.environ.get("DOWNLOAD_PROXY", "")if proxy.strip():opts["proxy"] = proxy.strip()if source == "youtube":# Server cloud (HF Spaces...) hay bị YouTube chặn IP datacenter với lỗi# "Sign in to confirm you're not a bot".195 unmodified lines19619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423566 unmodified lines302303304305306307308309310311195 unmodified linesreturn {"http": p, "https": p} if p else None_TIKTOK_COOKIE_PATH = None # cache: chỉ ghi file cookies TikTok 1 lầndef _tiktok_cookiefile() -> str | None:"""File cookies TikTok cho yt-dlp (tuỳ chọn, giúp qua bot-check từ IP server).TikTok trả trang "verify/challenge" rỗng cho khách vãng lai (nhất là IPdatacenter) -> yt-dlp chỉ bóc được audio. Cookies của 1 phiên đã đăng nhậpgiúp lấy trang đầy đủ -> có luồng video. KHÔNG bắt buộc; thiếu thì bỏ qua.Ưu tiên:1. TIKTOK_COOKIES_FILE -> đường dẫn file cookies.txt (dev local).2. TIKTOK_COOKIES_CONTENT -> NỘI DUNG cookies.txt (dán vào HF Secret)."""global _TIKTOK_COOKIE_PATHpath = os.environ.get("TIKTOK_COOKIES_FILE", "")if path and os.path.exists(path):return pathif _TIKTOK_COOKIE_PATH and os.path.exists(_TIKTOK_COOKIE_PATH):return _TIKTOK_COOKIE_PATHcontent = os.environ.get("TIKTOK_COOKIES_CONTENT", "")if content.strip():import tempfilep = os.path.join(tempfile.gettempdir(), "tiktok_cookies.txt")try:with open(p, "w", encoding="utf-8") as f:f.write(content)_TIKTOK_COOKIE_PATH = preturn pexcept Exception:return Nonereturn Nonedef _youtube_cookiefile() -> str | None:"""Trả về đường dẫn file cookies YouTube để yt-dlp dùng.66 unmodified linesor os.environ.get("DOWNLOAD_PROXY", "")if proxy.strip():opts["proxy"] = proxy.strip()if source in ("tiktok", "tiktok_photo") and not cookies_file:# Cookies TikTok (tuỳ chọn) -> qua bot-check, giúp yt-dlp lấy được luồng# video thay vì chỉ audio khi chạy từ IP server.cookies_file = _tiktok_cookiefile()if source == "youtube":# Server cloud (HF Spaces...) hay bị YouTube chặn IP datacenter với lỗi# "Sign in to confirm you're not a bot".
backend/app/services/downloader/ydl.py+38