name: Auto Update IPTV

on:
  push:
    branches:
      - main
  workflow_dispatch:
  schedule:
    - cron: "0 * * * *"

jobs:
  iptv:
    runs-on: ubuntu-latest

    permissions: write-all

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: 3.11

      - name: Install dependencies
        run: |
            pip install -r requirements.txt

      - name: Get Currrent Date
        run: echo "DATE=$(date +'%Y-%m-%d %H:%M:%S %Z')" >> $GITHUB_ENV

      - name: Run Update IPTV
        run: |
            python  iptv.py
        env:
            SALT: ${{ secrets.SALT }}
            PROXY_URL: ${{ secrets.PROXY_URL }}

      - name: Update Deployment Time
        run: |
          if grep -q "Last deployment time:" README.md; then
            sed -i "/Last deployment time:/c\Last deployment time: $DATE" README.md
          else
            echo "Last deployment time: $DATE" >> README.md
          fi

      - name: Git Auto Commit
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: Auto Update IPTV
          branch: main
          push_options: '--force'

      - name: Delete workflow runs
        uses: MajorScruffy/delete-old-workflow-runs@v0.3.0
        with:
          repository: ${{ github.repository }}
          workflow: ".github/workflows/iptv.yml"
          older-than-seconds: 3600
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
