-
-
Save myano/7456396 to your computer and use it in GitHub Desktop.
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
#Given to the public domain | |
#No warranties | |
import urllib2 | |
import simplejson | |
def shorturl(urltoshorten): | |
"""Compress the URL using goo.gl take a look at https://developers.google.com/url-shortener/v1/getting_started | |
>>> shorturl('http://igor.tamarapatino.org') | |
'http://goo.gl/FxHOn' | |
""" | |
try: | |
apiurl = "https://www.googleapis.com/urlshortener/v1/url" | |
req = urllib2.Request(apiurl, | |
headers={'Content-Type': 'application/json'}, | |
data='{{"longUrl": "{0}"}}'.format(urltoshorten)) | |
shorturl = simplejson.loads(urllib2.urlopen(req).read())['id'] | |
except: | |
shorturl = urltoshorten | |
return shorturl | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment