tiny.conf
1 2 config = "tiny.conf" 3 service = "urltea" 4 urltea url = "http://urltea.com/api/text/?url=" 5 tinyurl url = "http://tinyurl.com/api-create.php?" 6 description delimeter = "?" 7 description = ""
DZone Snippets > mcandre > tiny
13456 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Andrew Pennebaker http://mcandre.devjavu.com/wiki
1 2 config = "tiny.conf" 3 service = "urltea" 4 urltea url = "http://urltea.com/api/text/?url=" 5 tinyurl url = "http://tinyurl.com/api-create.php?" 6 description delimeter = "?" 7 description = ""
1 2 #!/usr/bin/env python 3 4 __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" 5 __date__="22 Jun 2007" 6 __copyright__="Copyright 2007 Andrew Pennebaker" 7 __license__="GPL" 8 __version__="0.0.1" 9 __URL__="http://urltea.com/to8" 10 __credits__="http://tinyurl.com/yswqg3" 11 12 import sys, urllib, getopt 13 14 CREATE_URL="http://urltea.com/api/text/?url=" 15 COMMENT_DELIMETER="?" 16 17 def tiny(url, description=""): 18 global CREATE_URL 19 global COMMENT_DELIMETER 20 21 try: 22 encodedurl=CREATE_URL+urllib.urlencode({"url":url}) 23 instream=urllib.urlopen(encodedurl) 24 tinyurl=instream.read() 25 instream.close() 26 27 if len(tinyurl)==0: 28 return url 29 30 if len(description)>0: 31 tinyurl+=COMMENT_DELIMETER+description 32 33 return tinyurl 34 except IOError, e: 35 raise "Could not connect." 36 37 def usage(): 38 print "Usage: %s <url1> <url2> <url3> ..." % (sys.argv[0]) 39 print "-d|--description <comment>" 40 print "-h|--help (usage)" 41 42 sys.exit() 43 44 def main(): 45 systemArgs=sys.argv[1:] 46 oplist, args=[], [] 47 48 comment="" 49 50 try: 51 optlist, args=getopt.getopt(systemArgs, "d:h", ["description=", "help"]) 52 except: 53 usage() 54 55 for option, value in optlist: 56 if option=="-h" or option=="--help": 57 usage() 58 elif option=="-d" or option=="--description": 59 comment=value 60 61 if len(args)<1: 62 usage() 63 64 for u in args: 65 print tiny(u, comment) 66 67 if __name__=="__main__": 68 try: 69 main() 70 except KeyboardInterrupt, e: 71 pass
1 2 #!/usr/bin/env python 3 4 """Converts long URLs to tiny URLs, with either tinyurl, urltea, or a custom url.""" 5 6 __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" 7 __date__="22 Jun 2007 - 24 Jun 2007" 8 __copyright__="Copyright 2007 Andrew Pennebaker" 9 __license__="GPL" 10 __version__="0.0.1" 11 __URL__="http://snippets.dzone.com/posts/show/4195" 12 __credits__="http://lateral.netmanagers.com.ar/weblog/2007/04/08.html#BB548" 13 14 import sys, getopt, urllib 15 16 import configreader 17 18 def tiny(url, settings): 19 try: 20 encodedurl=settings["posting url"]+urllib.urlencode({"url":url}) 21 instream=urllib.urlopen(encodedurl) 22 tinyurl=instream.read() 23 instream.close() 24 25 if len(tinyurl)==0: 26 return url 27 28 if settings["service"]=="urltea" and len(settings["description"])>0: 29 tinyurl+=settings["description delimeter"]+settings["description"] 30 31 return tinyurl 32 except IOError, e: 33 raise "Could not connect." 34 35 def usage(): 36 print "Usage: %s [options] <url1> <url2> <url3> ..." % (sys.argv[0]) 37 print "\nDefaults to urlTea unless specified in options or a config file." 38 print "\n-s|--service [tinyurl|urltea]" 39 print "-u|--custom-url <posting url>" 40 print "-d|--description <comment> May only be used with urltea." 41 print "-c|--config <configfile>" 42 print "-h|--help (usage)" 43 44 sys.exit() 45 46 def main(): 47 systemArgs=sys.argv[1:] 48 oplist, args=[], [] 49 50 settings={ 51 "config":"tiny.conf", 52 "service":"urltea", 53 "urltea url":"http://urltea.com/api/text/?url=", 54 "tinyurl url":"http://tinyurl.com/api-create.php?", 55 "description delimeter":"?", 56 "description":"" 57 } 58 59 try: 60 optlist, args=getopt.getopt(systemArgs, "s:u:d:c:h", ["service=", "custom-url=", "description=", "config=", "help"]) 61 except: 62 usage() 63 64 for option, value in optlist: 65 if option=="-c" or option=="--config": 66 settings["config"]=value 67 68 try: 69 configreader.load(open(settings["config"], "r"), settings) 70 except IOError, e: 71 pass 72 73 for option, value in optlist: 74 if option=="-h" or option=="--help": 75 usage() 76 elif option=="-s" or option=="--service": 77 settings["service"]=value 78 elif option=="-d" or option=="--description": 79 settings["description"]=value 80 81 if settings["service"]!="urltea" and len(settings["description"])>0: 82 usage() 83 84 try: 85 settings["posting url"]=settings[settings["service"]+" url"] 86 except: 87 usage() 88 89 for option, value in optlist: 90 if option=="-u" or option=="--custom-url": 91 settings["posting url"]=value 92 93 if len(args)<1: 94 usage() 95 96 for u in args: 97 print tiny(u, settings) 98 99 if __name__=="__main__": 100 try: 101 main() 102 except KeyboardInterrupt, e: 103 pass