matthew
12/3/2014 - 11:25 PM

gist-download-all-README.md

#!/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

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