name: Fetch weekly artist charts
on:
  workflow_dispatch:
  schedule:
    - cron: '00 09 * * 5'
jobs:
  FetchArtistCharts:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout out this repo
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
      - name: Fetch charts
        run: |
          echo "CHART_DATA=$(curl 'https://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=coryd_&api_key=${{ secrets.LASTFM_API_KEY }}&format=json')" >> "$GITHUB_ENV"
      - name: Update charts
        run: |
          echo "CHARTS=$(cat src/_data/json/weekly-artist-charts.json | jq -c --argjson jq_data "$CHART_DATA" '.charts += [$jq_data]')" >> "$GITHUB_ENV"
      - name: Write charts
        run: |
          echo $CHARTS > src/_data/json/weekly-artist-charts.json
      - name: Commit
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: Update artist charts for the week.
          commit_user_name: cdransf
          commit_user_email: coryd@hey.com
