Skip to content

Instantly share code, notes, and snippets.

@suda
Created February 4, 2011 12:17
Show Gist options
  • Save suda/811044 to your computer and use it in GitHub Desktop.
Save suda/811044 to your computer and use it in GitHub Desktop.
[Python/Django] Goo.gl URL shortener (using official API)
"""
Add GOOGL_KEY to your settings.py
"""
import urllib2, simplejson
from django.conf import settings
def googl(url):
params = simplejson.dumps({'longUrl': url})
headers = { 'Content-Type' : 'application/json' }
req = urllib2.Request('https://www.googleapis.com/urlshortener/v1/url?key='+settings.GOOGL_KEY, params, headers)
f = urllib2.urlopen(req)
return simplejson.loads(f.read())['id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment