1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# a simple module to shorten URLs, using configuration from settings
import settings
def get_goo_gl(url):
shortUrl = None
try:
data = '{longUrl:"%s", key:"%s"}' % (url, settings.GOO_GL_API_KEY)
import urllib2
req = urllib2.Request(settings.GOO_GL_API_URL, data)
req.add_header('Content-type', 'application/json')
result = urllib2.urlopen(req)
import django.utils.simplejson as json
shortUrl = json.loads(result.read()).get('id')
except:
import sys
print 'An error occurred:', sys.exc_info()
return shortUrl
# eof
|