Skip to content

Instantly share code, notes, and snippets.

@ikasamah
Created February 16, 2011 05:07
Show Gist options
  • Save ikasamah/828901 to your computer and use it in GitHub Desktop.
Save ikasamah/828901 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib2,json
#
# to get your API key, access following:
# https://code.google.com/apis/console/
#
API_URL = 'https://www.googleapis.com/urlshortener/v1/url'
API_KEY = 'YOUR_API_KEY'
def main():
if len(sys.argv) < 2:
usage()
sys.exit()
print shorturl(sys.argv[1])
def usage():
print "urlshortener.py URL"
def shorturl(longurl):
data = json.dumps({'longUrl': sys.argv[1]})
url = '%s?key=%s' % (API_URL, API_KEY)
req = urllib2.Request(url, data)
req.add_header('Content-Type', 'application/json')
raw = urllib2.urlopen(req).read()
return json.loads(raw)['id']
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment