Created
February 4, 2011 12:17
-
-
Save suda/811044 to your computer and use it in GitHub Desktop.
[Python/Django] Goo.gl URL shortener (using official API)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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