Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created June 10, 2011 00:05
Show Gist options
  • Select an option

  • Save tebeka/1018030 to your computer and use it in GitHub Desktop.

Select an option

Save tebeka/1018030 to your computer and use it in GitHub Desktop.
goo.gl url shortener
#!/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