Skip to content

Instantly share code, notes, and snippets.

@Chandru-B
Last active September 11, 2018 08:29
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 Chandru-B/7807182670c59fe08965463baf8b4189 to your computer and use it in GitHub Desktop.
Save Chandru-B/7807182670c59fe08965463baf8b4189 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
programname=$0
authtoken=$1
username=$2
option=$3
function usage {
echo "usage: $programname [authtoken] [username] [-a|-n]"
exit 1
}
if [ $# -lt 2 ]
then
usage
fi
if [ -z $option ]; then
option="-n"
fi
response=$(curl -s -H "Authorization: token $authtoken" https://api.github.com/users/$username/gists)
last_created_at="${username}_last_created_at.txt"
if [ ! -f $last_created_at ]; then
echo $response | jq '.[].created_at' | sort -n -k 1 | tail -n 1 > "$last_created_at"
echo $response | jq '.[].files[].raw_url' | xargs curl
elif [ "$option" == "-n" ]; then
last_created_dt=$(cat $last_created_at | sed -e 's/^"//' -e 's/"$//')
newgists=$(echo $response | jq --arg ld $last_created_dt '.[] | select(.created_at | . > $ld) | .files[].raw_url')
if [ ! -z $newgists ]; then
echo $newgists | xargs curl
echo $response | jq '.[].created_at' | sort -n -k 1 | tail -n 1 > "$last_created_at"
else
echo -e "No new gist published by $username since $last_created_dt.\nTo get all public gists \n\nusage: $programname [authtoken] [username] -a"
fi
elif [ "$option" == "-a" ]; then
echo $response | jq '.[].files[].raw_url' | xargs curl
fi
@Chandru-B
Copy link
Author

Chandru-B commented Sep 10, 2018

get the latest published gist for any user.
Please install jq before running the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment