Created
April 19, 2015 19:37
-
-
Save takehiko/57fc84804c9e059209ca to your computer and use it in GitHub Desktop.
A function for Google URL Shortener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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