A short and sweet Python "API" for goo.gl

#!/usr/bin/python
# use Google's http://goo.gl/ URL shortener
# v0.3
# requires urllib, urllib2, re, simplejson
def shorten(url):
  try:
    from re import match
    from urllib2 import urlopen, Request, HTTPError
    from urllib import quote
    from simplejson import loads
  except ImportError, e:
    raise Exception('Required module missing: %s' % e.args[0])
  if not match('http://',url):
    raise Exception('URL must start with "http://"')
  try:
    urlopen(Request('http://goo.gl/api/url','url=%s' % quote(url),{'User-Agent':'toolbar'}))
  except HTTPError, e:
    j = loads(e.read())
    if 'short_url' not in j:
      try:
        from pprint import pformat
        j = pformat(j)
      except ImportError:
        j = j.__dict__
      raise Exception('Didn\'t get a correct-looking response. How\'s it look to you?\n\n%s' % j)
    return j['short_url']
  raise Exception('Unknown error forming short URL.')

if __name__ == '__main__':
  from sys import argv
  print shorten(argv[1])

Licensed under the WTFPL v2.