-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the total views? #4
Comments
@vandan-revanur Just for instance, another project to do it: https://github.com/andry81-devops/github-accum-stats |
Thanks for the reference. However, in that repo there are multiple repos that need to setup and it is quite cumbersome. I would like to set it up like how you have done in your repo. It would be very helpful if you could show it could be done. |
@vandan-revanur create a new workflow from the 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 ::set-output name=GIST::${{ secrets.TRAFFIC_ID }}
curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.TRAFFIC_ID }}/raw/traffic.json > clone_before.json
if cat clone_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 ::set-output name=GIST::$traffic_id
cp traffic.json traffic_before.json
git rm --ignore-unmatch TRAFFIC.md
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 ::set-output name=GIST::$traffic_id
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
if [ ! -f TRAFFIC.md ]; then
shields="https://img.shields.io/badge/dynamic/json?color=success&label=Views&query=count&url="
url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/traffic.json"
repo="https://github.com/MShawon/github-clone-count-badge"
echo ''> TRAFFIC.md
echo '
**Markdown**
```markdown' >> TRAFFIC.md
echo "[![GitHub Traffic]($shields$url&logo=github)]($repo)" >> TRAFFIC.md
echo '
```
**HTML**
```html' >> TRAFFIC.md
echo "<a href='$repo'><img alt='GitHub Traffic' src='$shields$url&logo=github'></a>" >> TRAFFIC.md
echo '```' >> TRAFFIC.md
git add TRAFFIC.md
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
git commit -m "create traffic count badge"
fi
- name: Push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }} |
This implementation has a bug (well, more like a typo: not all occurrences of Here is the corrected version for anyone who is interested: 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 ::set-output name=GIST::${{ secrets.TRAFFIC_ID }}
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 ::set-output name=GIST::$traffic_id
cp traffic.json traffic_before.json
git rm --ignore-unmatch TRAFFIC.md
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 ::set-output name=GIST::$traffic_id
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
if [ ! -f TRAFFIC.md ]; then
shields="https://img.shields.io/badge/dynamic/json?color=success&label=Views&query=count&url="
url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/traffic.json"
repo="https://github.com/MShawon/github-clone-count-badge"
echo ''> TRAFFIC.md
echo '
**Markdown**
```markdown' >> TRAFFIC.md
echo "[![GitHub Traffic]($shields$url&logo=github)]($repo)" >> TRAFFIC.md
echo '
```
**HTML**
```html' >> TRAFFIC.md
echo "<a href='$repo'><img alt='GitHub Traffic' src='$shields$url&logo=github'></a>" >> TRAFFIC.md
echo '```' >> TRAFFIC.md
git add TRAFFIC.md
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
git commit -m "create traffic count badge"
fi
- name: Push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }} |
The workflow currently on this repo contains commands to extract and update the latest clone count.
How do I do the same for the total number of views?
Could you please possibly update the readme to reflect the code updating the view count?
The text was updated successfully, but these errors were encountered: