Google URL shortener API

Published: by

  • Categories:

On the official site, its written that you can use goo.gl with google products only. So, the site actually doesnt provide any API for getting shortened URLs.

Here is a very very simple python code that you can use to get shortened URLs. May be I shouldnt blog this bcos its extremely simple but still you dont find while searching on google, so I thought of sharing it.. It may be of help to someone.. :)

 #!/usr/bin/python

import sys
import urllib

SHORTNER_URL = "http://ggl-shortener.appspot.com/"
if sys.argv[1].count('http://') is 0:
    URL_TO_SHORTEN = urllib.urlencode({'url':"http://"+sys.argv[1]})
else:     
    URL_TO_SHORTEN = urllib.urlencode({'url':sys.argv[1]})

url = SHORTNER_URL+"?"+URL_TO_SHORTEN
print "Sending URL: "+url

f = urllib.urlopen(url)

print "Shortened URL: "+f.read()[14:-2]

Usage:-

shadyabhi@shadyabhi-desktop:~$ ./url_shortner.py linux-junky.blogspot.com
Sending URL: http://ggl-shortener.appspot.com/?url=http%3A%2F%2Flinux-junky.blogspot.com
Shortened URL: http://goo.gl/Sn3x
shadyabhi@shadyabhi-desktop:~$