Update traffic badges #70
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update traffic badges | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # Runs daily at 00:00 UTC | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| update-badges: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| persist-credentials: true | |
| fetch-depth: 0 | |
| - name: Install jq | |
| run: sudo apt-get update && sudo apt-get install -y jq | |
| - name: Fetch traffic and update totals | |
| env: | |
| REPO: alphayellow1/AlphaYellowWidescreenFixes | |
| TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| BRANCH: main | |
| run: | | |
| set -e | |
| # Folder for badges and totals | |
| BADGE_DIR=".github/data/badges" | |
| mkdir -p "$BADGE_DIR" | |
| RESPONSE=$(curl -s -H "Authorization: token $TOKEN" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| "https://api.github.com/repos/$REPO/traffic/views") | |
| [ -f "$BADGE_DIR/views_total.txt" ] || echo 0 > "$BADGE_DIR/views_total.txt" | |
| [ -f "$BADGE_DIR/uniques_total.txt" ] || echo 0 > "$BADGE_DIR/uniques_total.txt" | |
| [ -f "$BADGE_DIR/last_date.txt" ] || echo "" > "$BADGE_DIR/last_date.txt" | |
| PREV_VIEWS=$(cat "$BADGE_DIR/views_total.txt") | |
| PREV_UNIQUES=$(cat "$BADGE_DIR/uniques_total.txt") | |
| LAST_DATE=$(cat "$BADGE_DIR/last_date.txt") | |
| if [ -z "$LAST_DATE" ]; then | |
| ADD_VIEWS=$(echo "$RESPONSE" | jq '[.views[]? | .count] | add // 0') | |
| ADD_UNIQUES=$(echo "$RESPONSE" | jq '[.views[]? | .uniques] | add // 0') | |
| else | |
| LAST_TS="${LAST_DATE}T00:00:00Z" | |
| ADD_VIEWS=$(echo "$RESPONSE" | jq --arg last "$LAST_TS" \ | |
| '[.views[]? | select(.timestamp > $last) | .count] | add // 0') | |
| ADD_UNIQUES=$(echo "$RESPONSE" | jq --arg last "$LAST_TS" \ | |
| '[.views[]? | select(.timestamp > $last) | .uniques] | add // 0') | |
| fi | |
| NEW_VIEWS=$((PREV_VIEWS + ADD_VIEWS)) | |
| NEW_UNIQUES=$((PREV_UNIQUES + ADD_UNIQUES)) | |
| MAX_TS=$(echo "$RESPONSE" | jq -r '[.views[]?.timestamp] | max // empty') | |
| if [ -n "$MAX_TS" ]; then | |
| MAX_DATE=$(echo "$MAX_TS" | cut -dT -f1) | |
| echo "$MAX_DATE" > "$BADGE_DIR/last_date.txt" | |
| fi | |
| echo "$NEW_VIEWS" > "$BADGE_DIR/views_total.txt" | |
| echo "$NEW_UNIQUES" > "$BADGE_DIR/uniques_total.txt" | |
| printf '{\n "schemaVersion": 1,\n "label": "views",\n "message": "%s",\n "color": "blue"\n}\n' "$NEW_VIEWS" > "$BADGE_DIR/views-badge.json" | |
| printf '{\n "schemaVersion": 1,\n "label": "visitors",\n "message": "%s",\n "color": "green"\n}\n' "$NEW_UNIQUES" > "$BADGE_DIR/uniques-badge.json" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add "$BADGE_DIR/" | |
| git commit -m "Update traffic badges" || echo "No changes to commit" | |
| git push origin HEAD:${BRANCH} |