Last active
September 24, 2015 16:08
-
-
Save quandyfactory/773917 to your computer and use it in GitHub Desktop.
shorturl.py
This file contains hidden or 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
| import httplib2 | |
| try: | |
| import json | |
| except: | |
| import simplejson as json | |
| def short_url(long_url='http://raisethehammer.org') | |
| """ | |
| Takes a long URL and returns a short URL. | |
| Uses the Google URL Shortener API: http://code.google.com/apis/urlshortener/ | |
| """ | |
| h = httplib2.Http('/path/to/.cache') | |
| url = 'https://www.googleapis.com/urlshortener/v1/url?key=<api-key>' | |
| req_headers = {"content-type": "application/json" } | |
| data = { "longUrl": long_url } | |
| response, content = h.request(url, 'POST', json.dumps(data), headers=req_headers) | |
| if response.status != 200: | |
| raise Exception('HTTP Error', response.status) | |
| jsonobj = json.loads(content) | |
| return jsonobj['id'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment