name: update-yt-dlp

on:
  schedule:
    - cron: '0 0 * * *'

jobs:
  update-yt-dlp :
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v6
        with:
          token: ${{ secrets.AUTOUPDATE_PAT }}
      -
        name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.13'
      -
        name: Install uv
        uses: astral-sh/setup-uv@v7
      -
        name: Update yt-dlp
        run: |
          uv sync --locked
          # Check if yt-dlp has updates available
          LATEST_VERSION=$(uv pip list --outdated --format json | jq -r '.[] | select(.name == "yt-dlp") | .latest_version')
          CURRENT_VERSION=$(uv pip list --format json | jq -r '.[] | select(.name == "yt-dlp") | .version')
          if [ -n "$LATEST_VERSION" ] && [ "$LATEST_VERSION" != "$CURRENT_VERSION" ]; then
            echo "Updating yt-dlp from $CURRENT_VERSION to $LATEST_VERSION"
            uv lock --upgrade-package "yt-dlp[default,curl-cffi]"
            git config --global user.email "updater@metube"
            git config --global user.name "AutoUpdater"
            git add uv.lock
            git commit -m "upgrade yt-dlp from $CURRENT_VERSION to $LATEST_VERSION"
            git push
          else
            echo "yt-dlp is already up to date ($CURRENT_VERSION)"
          fi
