GitHub Traffic Count Update Everyday #29
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: GitHub Traffic Count Update Everyday | |
| on: | |
| schedule: | |
| - cron: "0 */24 * * *" | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: gh login | |
| run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token | |
| - name: parse latest traffic count | |
| run: | | |
| curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| https://api.github.com/repos/${{ github.repository }}/traffic/views \ | |
| > traffic.json | |
| - name: create gist and download previous count | |
| id: set_id | |
| run: | | |
| if gh secret list | grep -q "TRAFFIC_ID" | |
| then | |
| echo "TRAFFIC_ID found" | |
| echo "GIST=${{ secrets.TRAFFIC_ID }}" >> $GITHUB_OUTPUT | |
| curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.TRAFFIC_ID }}/raw/traffic.json > traffic_before.json | |
| if cat traffic_before.json | grep '404: Not Found'; then | |
| echo "TRAFFIC_ID not valid anymore. Creating another gist..." | |
| traffic_id=$(gh gist create traffic.json | awk -F / '{print $NF}') | |
| echo $traffic_id | gh secret set TRAFFIC_ID | |
| echo "GIST=$traffic_id" >> $GITHUB_OUTPUT | |
| cp traffic.json traffic_before.json | |
| fi | |
| else | |
| echo "TRAFFIC_ID not found. Creating a gist..." | |
| traffic_id=$(gh gist create traffic.json | awk -F / '{print $NF}') | |
| echo $traffic_id | gh secret set TRAFFIC_ID | |
| echo "GIST=$traffic_id" >> $GITHUB_OUTPUT | |
| cp traffic.json traffic_before.json | |
| fi | |
| - name: update traffic.json | |
| run: | | |
| curl https://gist.githubusercontent.com/MShawon/d37c49ee4ce03f64b92ab58b0cec289f/raw/traffic.py > traffic.py | |
| python3 traffic.py | |
| - name: Update gist with latest count | |
| run: | | |
| content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "traffic.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g') | |
| echo '{"description": "${{ github.repository }} traffic statistics", "files": {"traffic.json": {"content": "'"$content"'"}}}' > post_traffic.json | |
| curl -s -X PATCH \ | |
| --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ | |
| -H "Content-Type: application/json" \ | |
| -d @post_traffic.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1 |