Last active
August 29, 2022 07:38
-
-
Save simeji/2d68d6fe2212934ad18068f5eedb34bb to your computer and use it in GitHub Desktop.
Get count of Github repository stars
This file contains 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
#!/bin/bash | |
USER=$1 | |
REPO=$2 | |
if [ -z "$USER" -o -z "$REPO" ]; then | |
echo '$1 : Github username, $2: Github repository name (only public repo)' | |
exit 1 | |
fi | |
FILENAME=star_${USER}_${REPO} | |
star_cnt=`curl -s https://api.github.com/repos/$USER/$REPO | jq .stargazers_count` | |
page_cnt=$((($star_cnt / 100) + 1)) | |
i=1 | |
while [ $i -le $page_cnt ]; do | |
curl -s "https://api.github.com/repos/$USER/$REPO/stargazers?page=$i&per_page=100" -H "Accept: application/vnd.github.v3.star+json" | jq -r '.[].starred_at' >> $FILENAME.txt | |
i=$(($i + 1)) | |
done | |
cat $FILENAME.txt | tr 'T' "\t" | tr -d "Z" > $FILENAME.tsv |
As function in your .bashrc
:
ghstars() {
user_repo="$@"
curl -s https://api.github.com/repos/${user_repo} | \
grep 'stargazers_count' | \
grep -o -P '(?<=:).*(?=,)' | \
grep -o -P '(?<= ).*'
# curl -s https://api.github.com/repos/${user_repo} | \
# jq .stargazers_count
}
call it for syncthing/syncthing as example:
$ ghstars syncthing/syncthing
# as of 2021-07-24
39375
Using jq
to parse JSON will save you a lot of headaches caused by using grep to parse JSON.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -s https://api.github.com/repos/$USER/$REPO | jq .stargazers_count