From: Jerome Leclanche Date: Sun, 1 Apr 2012 20:33:18 +0000 (+0100) Subject: Re-implement zurl() under the goo.gl API X-Git-Tag: v0.6.0~3 X-Git-Url: http://git.grml.org/?p=grml-etc-core.git;a=commitdiff_plain;h=49339f228f2f14d4ca42f0f4a66099d9a806a873 Re-implement zurl() under the goo.gl API --- diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index f5b943f..a33c6df 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -2640,46 +2640,39 @@ xtrename() { return 0 } -# TODO: -# Rewrite this by either using tinyurl.com's API -# or using another shortening service to comply with -# tinyurl.com's policy. -# -# Create small urls via http://tinyurl.com using wget(1). -#function zurl() { -# emulate -L zsh -# [[ -z $1 ]] && { print "USAGE: zurl " ; return 1 } -# -# local PN url tiny grabber search result preview -# PN=$0 -# url=$1 -## Check existence of given URL with the help of ping(1). -## N.B. ping(1) only works without an eventual given protocol. -# ping -c 1 ${${url#(ftp|http)://}%%/*} >& /dev/null || \ -# read -q "?Given host ${${url#http://*/}%/*} is not reachable by pinging. Proceed anyway? [y|n] " -# -# if (( $? == 0 )) ; then -## Prepend 'http://' to given URL where necessary for later output. -# [[ ${url} != http(s|)://* ]] && url='http://'${url} -# tiny='http://tinyurl.com/create.php?url=' -# if check_com -c wget ; then -# grabber='wget -O- -o/dev/null' -# else -# print "wget is not available, but mandatory for ${PN}. Aborting." -# fi -## Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code. -# search='copy\(?http://tinyurl.com/[[:alnum:]]##*' -# result=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http:*} -## TinyURL provides the rather new feature preview for more confidence. -# preview='http://preview.'${result#http://} -# -# printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL ." -# printf '%s\t%s\n\n' 'Given URL:' ${url} -# printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview} -# else -# return 1 -# fi -#} +# Create small urls via http://goo.gl using curl(1). +# API reference: https://code.google.com/apis/urlshortener/ +function zurl() { + emulate -L zsh + if [[ -z $1 ]]; then + print "USAGE: zurl " + return 1 + fi + + local PN url prog api json data + PN=$0 + url=$1 + + # Prepend 'http://' to given URL where necessary for later output. + if [[ ${url} != http(s|)://* ]]; then + url='http://'${url} + fi + + if check_com -c curl; then + prog=curl + else + print "curl is not available, but mandatory for ${PN}. Aborting." + return 1 + fi + api='https://www.googleapis.com/urlshortener/v1/url' + contenttype="Content-Type: application/json" + json="{\"longUrl\": \"${url}\"}" + data=$($prog --silent -H ${contenttype} -d ${json} $api) + # Match against a regex and print it + if [[ $data =~ '"id": "(http://goo.gl/[[:alnum:]]+)"' ]]; then + print $match; + fi +} #f2# Find history events by search pattern and list them by date. whatwhen() {