Created
February 16, 2011 05:07
-
-
Save ikasamah/828901 to your computer and use it in GitHub Desktop.
Create short URL. see: http://code.google.com/intl/en/apis/urlshortener/
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
#!/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