Skip to content

Instantly share code, notes, and snippets.

@simeji
Last active August 29, 2022 07:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simeji/2d68d6fe2212934ad18068f5eedb34bb to your computer and use it in GitHub Desktop.
Save simeji/2d68d6fe2212934ad18068f5eedb34bb to your computer and use it in GitHub Desktop.
Get count of Github repository stars
#!/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
@sweetycode
Copy link

sweetycode commented Jan 16, 2020

what the f**K?
just use
https://api.github.com/repos/$USER/$REPO?page=$i&per_page=100 | grep 'stargazers_count'

@Loki-Astari
Copy link

Loki-Astari commented Jul 22, 2020

@cs01
Copy link

cs01 commented Sep 6, 2020

curl -s https://api.github.com/repos/$USER/$REPO | jq .stargazers_count

@eevmanu
Copy link

eevmanu commented Jul 24, 2021

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

@Loki-Astari
Copy link

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