<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: tea code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 19:06:12 GMT</pubDate>
    <description>DZone Snippets: tea code</description>
    <item>
      <title>tiny.py</title>
      <link>http://snippets.dzone.com/posts/show/4195</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;"""Converts long URLs to tiny URLs, with either tinyurl, urltea, or a custom url."""&lt;br /&gt;&lt;br /&gt;__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"&lt;br /&gt;__date__="22 Jun 2007 - 24 Jun 2007"&lt;br /&gt;__copyright__="Copyright 2007 Andrew Pennebaker"&lt;br /&gt;__license__="GPL"&lt;br /&gt;__version__="0.0.1"&lt;br /&gt;__URL__="http://snippets.dzone.com/posts/show/4195"&lt;br /&gt;__credits__="http://lateral.netmanagers.com.ar/weblog/2007/04/08.html#BB548"&lt;br /&gt;&lt;br /&gt;import sys, getopt, urllib&lt;br /&gt;&lt;br /&gt;import configreader&lt;br /&gt;&lt;br /&gt;def tiny(url, settings):&lt;br /&gt;	try:&lt;br /&gt;		encodedurl=settings["posting url"]+urllib.urlencode({"url":url})&lt;br /&gt;		instream=urllib.urlopen(encodedurl)&lt;br /&gt;		tinyurl=instream.read()&lt;br /&gt;		instream.close()&lt;br /&gt;&lt;br /&gt;		if len(tinyurl)==0:&lt;br /&gt;			return url&lt;br /&gt;&lt;br /&gt;		if settings["service"]=="urltea" and len(settings["description"])&gt;0:&lt;br /&gt;				tinyurl+=settings["description delimeter"]+settings["description"]&lt;br /&gt;&lt;br /&gt;		return tinyurl&lt;br /&gt;	except IOError, e:&lt;br /&gt;		raise "Could not connect."&lt;br /&gt;&lt;br /&gt;def usage():&lt;br /&gt;	print "Usage: %s [options] &lt;url1&gt; &lt;url2&gt; &lt;url3&gt; ..." % (sys.argv[0])&lt;br /&gt;	print "\nDefaults to urlTea unless specified in options or a config file."&lt;br /&gt;	print "\n-s|--service [tinyurl|urltea]"&lt;br /&gt;	print "-u|--custom-url &lt;posting url&gt;"&lt;br /&gt;	print "-d|--description &lt;comment&gt; May only be used with urltea."&lt;br /&gt;	print "-c|--config &lt;configfile&gt;"&lt;br /&gt;	print "-h|--help (usage)"&lt;br /&gt;&lt;br /&gt;	sys.exit()&lt;br /&gt;&lt;br /&gt;def main():&lt;br /&gt;	systemArgs=sys.argv[1:]&lt;br /&gt;	oplist, args=[], []&lt;br /&gt;&lt;br /&gt;	settings={&lt;br /&gt;		"config":"tiny.conf",&lt;br /&gt;		"service":"urltea",&lt;br /&gt;		"urltea url":"http://urltea.com/api/text/?url=",&lt;br /&gt;		"tinyurl url":"http://tinyurl.com/api-create.php?",&lt;br /&gt;		"description delimeter":"?",&lt;br /&gt;		"description":""&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	try:&lt;br /&gt;		optlist, args=getopt.getopt(systemArgs, "s:u:d:c:h", ["service=", "custom-url=", "description=", "config=", "help"])&lt;br /&gt;	except:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	for option, value in optlist:&lt;br /&gt;		if option=="-c" or option=="--config":&lt;br /&gt;			settings["config"]=value&lt;br /&gt;&lt;br /&gt;	try:&lt;br /&gt;		configreader.load(open(settings["config"], "r"), settings)&lt;br /&gt;	except IOError, e:&lt;br /&gt;		pass&lt;br /&gt;&lt;br /&gt;	for option, value in optlist:&lt;br /&gt;		if option=="-h" or option=="--help":&lt;br /&gt;			usage()&lt;br /&gt;		elif option=="-s" or option=="--service":&lt;br /&gt;			settings["service"]=value&lt;br /&gt;		elif option=="-d" or option=="--description":&lt;br /&gt;			settings["description"]=value&lt;br /&gt;&lt;br /&gt;	if settings["service"]!="urltea" and len(settings["description"])&gt;0:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	try:&lt;br /&gt;		settings["posting url"]=settings[settings["service"]+" url"]&lt;br /&gt;	except:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	for option, value in optlist:&lt;br /&gt;		if option=="-u" or option=="--custom-url":&lt;br /&gt;			settings["posting url"]=value&lt;br /&gt;&lt;br /&gt;	if len(args)&lt;1:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	for u in args:&lt;br /&gt;		print tiny(u, settings)&lt;br /&gt;&lt;br /&gt;if __name__=="__main__":&lt;br /&gt;	try:&lt;br /&gt;		main()&lt;br /&gt;	except KeyboardInterrupt, e:&lt;br /&gt;		pass&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Jun 2007 18:32:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4195</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
  </channel>
</rss>
