25 lines
698 B
Text
25 lines
698 B
Text
# Create short urls via http://goo.gl using curl(1).
|
|
# Contributed back to grml zshrc
|
|
# API reference: https://code.google.com/apis/urlshortener/
|
|
function zurl {
|
|
if [[ -z $1 ]]; then
|
|
print "USAGE: $0 <URL>"
|
|
return 1
|
|
fi
|
|
|
|
local url=$1
|
|
local api='https://www.googleapis.com/urlshortener/v1/url'
|
|
local data
|
|
|
|
# Prepend "http://" to given URL where necessary for later output.
|
|
if [[ $url != http(s|)://* ]]; then
|
|
url="http://$url"
|
|
fi
|
|
local json="{\"longUrl\": \"$url\"}"
|
|
|
|
data=$(curl --silent -H "Content-Type: application/json" -d $json $api)
|
|
# Match against a regex and print it
|
|
if [[ $data =~ '"id": "(http://goo.gl/[[:alnum:]]+)"' ]]; then
|
|
print $match
|
|
fi
|
|
}
|