Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tea.py

DZone's Guide to

Tea.py

·
Free Resource
// Converts long URLs to tiny URLs with URLTea.


#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="22 Jun 2007"
__copyright__="Copyright 2007 Andrew Pennebaker"
__version__="0.0.1"
__URL__="http://urltea.com/to8"
__credits__="http://tinyurl.com/yswqg3"

import sys, urllib, getopt

CREATE_URL="http://urltea.com/api/text/?url="
COMMENT_DELIMETER="?"

def tiny(url, description=""):
	global CREATE_URL
	global COMMENT_DELIMETER

	try:
		encodedurl=CREATE_URL+urllib.urlencode({"url":url})
		instream=urllib.urlopen(encodedurl)
		tinyurl=instream.read()
		instream.close()

		if len(tinyurl)==0:
			return url

		if len(description)>0:
			tinyurl+=COMMENT_DELIMETER+description

		return tinyurl
	except IOError, e:
		raise "Could not connect."

def usage():
	print "Usage: %s 
   
    
    
      ..." % (sys.argv[0])
	print "-d|--description 
     
      "
	print "-h|--help (usage)"

	sys.exit()

def main():
	systemArgs=sys.argv[1:]
	oplist, args=[], []

	comment=""

	try:
		optlist, args=getopt.getopt(systemArgs, "d:h", ["description=", "help"])
	except:
		usage()

	for option, value in optlist:
		if option=="-h" or option=="--help":
			usage()
		elif option=="-d" or option=="--description":
			comment=value

	if len(args)<1:
		usage()

	for u in args:
		print tiny(u, comment)

if __name__=="__main__":
	try:
		main()
	except KeyboardInterrupt, e:
		pass

     
    
   
  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}