Skip to content

Instantly share code, notes, and snippets.

@hrwgc
Last active May 3, 2020 01:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hrwgc/4507055 to your computer and use it in GitHub Desktop.
Save hrwgc/4507055 to your computer and use it in GitHub Desktop.
download all of your gists from gist.github.com

gist list

A command line script to retrieve json for all of your gists.

usage:

github.sh [username] [password] [total number of gists] [oath or user:password]

You can either authenticate with oath, or by using your existing github username/password.

ex:

Oath example

github.sh hrwgc $GH_PW 399 oath

user:pw example

github.sh hrwgc $GH_PW 399 user:pw

Good Resources

#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
echo "github.sh [username] [password] [total gists] [oath or user:password]"
exit
fi
GHUSER="$1"
GHPASS="$2"
NUM_GISTS="$3"
DECIDER="$4"
if [ "$DECIDER" == 'oath' ]; then
# check to see if existing oath token created for gist downloads
curl \
-o oath.json \
https://api.github.com/authorizations \
--user "$GHUSER:$GHPASS"
TOKEN=$(cat oath.json | tr -s '\n' ' ' | sed -nE 's/^.* +"note": "read my gists", {1,}"token": "([^\"]+)".*/\1/p')
if [ "${#TOKEN}" -gt 0 ]; then
GITHUB_OATH="$TOKEN"
else
## obtain oath token for gist
curl \
-o oath.json \
https://api.github.com/authorizations \
--user "$GHUSER:$GHPASS" \
--data '{"scopes":["gist"],"note":"read my gists"}'
GITHUB_OATH=$(cat oath.json | sed -nE 's/^ +"token": "([^\"]+)"/\1/p');
fi
AUTHENTICATION_STRING="-H \'Authorization: token $GITHUB_OATH\'"
rm oath.json
else
AUTHENTICATION_STRING="--user '$GHUSER:$GHPASS'"
fi
float=$NUM_GISTS
PAGINATOR=$(echo "($float/100) + 1" | bc)
while [ "$PAGINATOR" -gt 0 ]; do
curl \
-o gists-${PAGINATOR}.json \
"$AUTHENTICATION_STRING"\
"https://api.github.com/gists?page=${PAGINATOR}&per_page=100";
PAGINATOR=$(echo "$PAGINATOR - 1" | bc);
echo $PAGINATOR
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment