FROM python:3.11-slim

# System deps: ffmpeg for audio, nodejs（旧 JS 提取保留）, unzip 用于解压 deno
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    curl \
    nodejs \
    unzip \
    && rm -rf /var/lib/apt/lists/*

# yt-dlp as a standalone binary (latest release)
RUN curl -sSL https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp \
    -o /usr/local/bin/yt-dlp && chmod +x /usr/local/bin/yt-dlp

# Deno：新版 yt-dlp 的 EJS 子系统用它解 YouTube 的 n-challenge(nsig)，
# 不再识别 node 作为 JS 运行时，缺 deno 会导致 YouTube 格式不可用。
RUN curl -fsSL https://github.com/denoland/deno/releases/latest/download/deno-x86_64-unknown-linux-gnu.zip \
    -o /tmp/deno.zip \
    && unzip -o /tmp/deno.zip -d /usr/local/bin/ \
    && chmod +x /usr/local/bin/deno \
    && rm /tmp/deno.zip \
    && deno --version

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# YouTube OAuth2 device-auth plugin (token auto-refreshed, no cookie export needed)
RUN pip install --no-cache-dir yt-dlp-youtube-oauth2

# Playwright + Chromium for Douyin/XHS (bypasses X-Bogus WAF signature requirement)
# Pin browser path to /opt/playwright so XDG_CACHE_HOME override doesn't affect it
ENV PLAYWRIGHT_BROWSERS_PATH=/opt/playwright
RUN pip install --no-cache-dir playwright
RUN playwright install chromium --with-deps

COPY app/ app/

RUN mkdir -p /app/tmp /app/cookies

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
