Created
June 10, 2011 00:05
-
-
Save tebeka/1018030 to your computer and use it in GitHub Desktop.
goo.gl url shortener
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
| #!/usr/bin/env python | |
| # Shorten URL using goo.gl | |
| from httplib import HTTPSConnection | |
| import json | |
| def shorten(url): | |
| con = HTTPSConnection("www.googleapis.com", timeout=3) | |
| con.request("POST", "/urlshortener/v1/url", | |
| body=json.dumps({"longUrl" : url}), | |
| headers={"Content-type" : "application/json"}) | |
| fo = con.getresponse() | |
| result = json.load(fo) | |
| return result["id"] | |
| def main(argv=None): | |
| import sys | |
| from argparse import ArgumentParser | |
| argv = argv or sys.argv | |
| parser = ArgumentParser(description="Shorten URL using goo.gl") | |
| parser.add_argument("url", help="URL to shorten") | |
| args = parser.parse_args(argv[1:]) | |
| print(shorten(args.url)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment