Skip to content

Instantly share code, notes, and snippets.

@conspirator
Last active December 31, 2015 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conspirator/8035808 to your computer and use it in GitHub Desktop.
Save conspirator/8035808 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Grab a random gif from giphy.com
#
# Example:
# $ gif // places a gif url on your clipboard
# $ gif -o // opens a gif url in your default browser
# $ gif chicago+bulls // places a Bulls-related gif url on your clipboard
# $ gif chicago+bulls -o // opens a Bulls-related gif url in your default browser
#
# Author: Christopher Webb <hello@conspirator.co>
# Website: conspirator.co
# Updated: 12/18/13
#
function jsonval {
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
}
api="http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${*//-o/}"
json=`curl -s -X GET $api`
prop='data'
dataCheck=`jsonval`
if [[ ${dataCheck//data:/} == *[]* ]]
then
echo "No results"
else
prop='image_original_url'
url=`jsonval`
if [[ $* == *-o* ]]
then
open $url
else
echo $url | pbcopy
echo "In clipboard: $url"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment