Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created April 19, 2015 19:37
Show Gist options
  • Save takehiko/57fc84804c9e059209ca to your computer and use it in GitHub Desktop.
Save takehiko/57fc84804c9e059209ca to your computer and use it in GitHub Desktop.
A function for Google URL Shortener
# Usage
# ggl http://d.hatena.ne.jp/takehikom/
# ggl -k Google_API_Key http://d.hatena.ne.jp/takehikom/
# ggl -f Google_API_Key_File http://d.hatena.ne.jp/takehikom/
# ggl -F http://d.hatena.ne.jp/takehikom/ http://d.hatena.ne.jp/takehikom/rss
function ggl () {
local longurl json
local apiurl=https://www.googleapis.com/urlshortener/v1/url
while getopts k:f:F OPT
do
case $opt in
"k" ) apiurl="${apiurl}?key=${OPTARG}" ;;
"f" ) apiurl="${apiurl}?key=$(cat ${OPTARG})" ;;
"F" ) apiurl="${apiurl}?key=$(cat ~/.googleapikey)" ;;
esac
done
shift $((OPTIND - 1))
for longurl in $@
do
json="{\"longUrl\": \"${longurl}\"}"
echo "curl \"${apiurl}\" -H 'Content-Type: application/json' -d '${json}'"
curl "${apiurl}" -H 'Content-Type: application/json' -d ${json}
done
}
alias googl_url=ggl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment