Skip to content

Instantly share code, notes, and snippets.

@gniemetz
Forked from sebble/stars.sh
Last active April 14, 2021 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gniemetz/e5e86c411f1d1642d72b46054ceff754 to your computer and use it in GitHub Desktop.
Save gniemetz/e5e86c411f1d1642d72b46054ceff754 to your computer and use it in GitHub Desktop.
List all starred repositories of a GitHub user.
#!/bin/bash
USER=${1:-gniemetz}
declare -i PER_PAGE=100
declare -r ANSWER="$(
curl \
-s `# Silent or quiet mode. Don't show progress meter or error messages.` \
-I `# (HTTP FTP FILE) Fetch the headers only!` \
"https://api.github.com/users/${USER}/starred?per_page=1"
)"
declare -ir LIMIT="$(
awk \
-F ': ' `# field seperator` \
-v RS=$'\r\n' `# record seperator` \
'/^x-ratelimit-limit/ {
print $2
}
' <<<"${ANSWER}"
)"
declare -ir LIMITUSED="$(
awk \
-F ': ' `# field seperator` \
-v RS=$'\r\n' `# record seperator` \
'/^x-ratelimit-used/ {
print $2
}
' <<<"${ANSWER}"
)"
declare -ir LIMITREMAINING="$(
awk \
-F ': ' `# field seperator` \
-v RS=$'\r\n' `# record seperator` \
'/^x-ratelimit-remaining/ {
print $2
}
' <<<"${ANSWER}"
)"
if ((LIMITUSED >= LIMIT)); then
declare -ir LIMITRESET="$(
awk \
-F ': ' `# field seperator` \
-v RS=$'\r\n' `# record seperator` \
'/^x-ratelimit-reset/ {
print $2
}
' <<<"${ANSWER}"
)"
declare -r LIMITRESET_DT="$(
date \
--date=@${LIMITRESET} `# date in seconds since epoch` \
+'%Y-%m-%d %T'
)"
echo "Limit exceeded, next query allowed at ${LIMITRESET_DT}"
exit 1
fi
declare -i STARS=10#$(
sed \
-n `# suppress automatic printing of pattern space` \
-e `# add the script to the commands to be executed` \
'/^link/ s/\(.*>; rel="next", .*page=\)\([0-9]*\)\(>; rel="last"\r$\)/\2/p' <<<"${ANSWER}"
)
declare -i PAGES=$((${STARS}/${PER_PAGE}+1))
echo -e "You have ${STARS} starred repositories (${LIMITREMAINING} queries remaining)\n"
for ((PAGE=1; PAGE <= PAGES; PAGE++)); do
curl \
-s `# Silent or quiet mode. Don't show progress meter or error messages.` \
-H `# (HTTP) Extra header to include in the request when sending HTTP to a server.` \
"Accept: application/vnd.github.v3.star+json" \
"https://api.github.com/users/${USER}/starred?per_page=${PER_PAGE}&page=${PAGE}"
done |\
jq \
-r `# With this option, if the filter´s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes.` \
'(.[] |
[.repo.full_name,
.repo.description,
(.repo.created_at | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")),
(.repo.updated_at | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")),
(.starred_at | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")),
.repo.archived,
.repo.disabled,
.repo.open_issues,
.repo.watchers,
.repo.homepage]
) |
@csv'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment