Skip to content

Instantly share code, notes, and snippets.

@bbaster
Created January 1, 2024 22:46
Show Gist options
  • Save bbaster/b7de067d262b6f893326f08b4b207dc1 to your computer and use it in GitHub Desktop.
Save bbaster/b7de067d262b6f893326f08b4b207dc1 to your computer and use it in GitHub Desktop.
Program for counting the common repositories starred by users who have also starred a specified repo
#!/usr/bin/env bash
#Program for counting the common repositories starred by users who have also starred a specified repo.
#Ensure that you have both "jq" and "gh" installed and that gh is authenticated with your account - anonymous requests have a much lower rate limit.
#WARNING: As an API request is sent for every single stargazer, your GitHub API rate limit may be significantly consumed! Use with caution.
destdir=~/analyzed-repos
mkdir -p $destdir
echo -n "Input the repository owners' username and the name of the repository, using the syntax \"<owner>/<repository_name>\": "
read -r input
[[ $input =~ ([A-Za-z0-9-]+)\/([A-Za-z0-9-]+) ]]
owner=${BASH_REMATCH[1]}
repository=${BASH_REMATCH[2]}
echo "Analyzing data from \"$owner/$repository\" - press any key to continue"
read
if [ -e $destdir/unprocessed-${owner}-${repository}.txt ]; then
echo -n "File already exists, do you want to update it? (y/N): "
read -r input
case $input in
y | Y)
verdict=true;;
n | N | "")
verdict=false;;
esac
else verdict=true; fi
if [ $verdict = true ]; then
time for user in \
$(for (( i=1; i<=$(gh api /repos/${owner}/${repository} | jq -r '.stargazers_count')/100+1; i++ )); do
gh api "/repos/${owner}/${repository}/stargazers?per_page=100&page=${i}" | jq -r '.[] | .login';
done);
do gh api /users/${user}/starred; done | jq -r '.[] | .html_url' | tee $destdir/unprocessed-${owner}-${repository}.txt; fi
awk '{a[$1]++} END {for (k in a) { if (a[k] > 1) print k, a[k]}}' $destdir/unprocessed-${owner}-${repository}.txt | sort -k 2rn | tee $destdir/results-${owner}-${repository}.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment