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 > url
13300 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
1 2 #!/usr/bin/env python 3 4 __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" 5 __date__="17 Jun 2007 - 28 Jun 2007" 6 __copyright__="Copyright 2007 Andrew Pennebaker" 7 __license__="GPL" 8 __version__="0.0.1" 9 __credits__="Based on tweetyPy (http://muffinresearch.co.uk/archives/2007/03/24/tweetypy-python-based-cli-client-for-twitter/)" 10 __URL__="http://snippets.dzone.com/posts/show/4150" 11 12 import sys, getopt, getpass, urllib, urllib2 13 14 import configreader 15 16 STATUS_MODE="STATUS" 17 VIEW_MODE="VIEW" 18 COMMAND_MODE="COMMAND" 19 20 COMMANDS="""Command\t\tMeaning 21 22 d username\tDirect Text 23 @username\tReply 24 follow username\tReceive updates via phone or IM 25 leave username\tStop following username 26 leave all\tStop following all friends 27 remove username\tRemove username from friends list 28 delete username\tDelete username from friends list 29 get username\tGet the last update from username 30 get\tGet the most recent updates from all friends 31 nudge username\tTwitter aks what the person is currently up to 32 whois username\tGet username's bio 33 add phonenumber\tSend text invite. If already a member, invite will turn into a friend request. 34 accept username\tAccept username as a friend 35 deny username\tDeny username as friend""" 36 37 def usage(): 38 print "Usage: %s [options]" % (sys.argv[0]) 39 print "\nWithout any options, uses status mode. Leftover args are concatenated to form message." 40 print "\n-u|--username <username> specified in tw.conf" 41 print "-s|--status mode" 42 print "-v|--view status" 43 print "-l|--list-commands List Twitter commands" 44 print "-c|--config <configfile>" 45 print "-h|--help" 46 47 sys.exit() 48 49 def set_status(settings, status): 50 auth=urllib2.HTTPPasswordMgrWithDefaultRealm() 51 auth.add_password(None, settings["rootauthurl"], settings["username"], settings["password"]) 52 authHandler=urllib2.HTTPBasicAuthHandler(auth) 53 opener=urllib2.build_opener(authHandler) 54 55 url="http://twitter.com/statuses/update.xml" 56 post=urllib.urlencode({"status":status}) 57 58 request=urllib2.Request(url, post) 59 request.add_header("User-Agent", settings["useragent"]) 60 61 try: 62 response=opener.open(request) 63 except IOError, e: 64 raise "Could not connect." 65 66 def view_status(settings): 67 url="http://twitter.com/"+settings["username"] 68 message="" 69 70 statusdelimeter1=settings["statusdelimeter1"] 71 statusdelimeter2=settings["statusdelimeter2"] 72 73 try: 74 instream=urllib.urlopen(url) 75 for line in instream: 76 if statusdelimeter1 in line: 77 message=line[ 78 line.index(statusdelimeter1)+len(statusdelimeter1):line.index(statusdelimeter2) 79 ] 80 break 81 instream.close() 82 83 return message 84 85 except IOError, e: 86 raise "Could not connect." 87 88 def main(): 89 global STATUS_MODE 90 global VIEW_MODE 91 global COMMAND_MODE 92 global COMMANDS 93 94 sysArgs=sys.argv[1:] 95 96 mode=STATUS_MODE 97 98 settings={ 99 "config":"tw.conf", 100 "username":"mcandre", 101 "rootauthurl":"http://twitter.com/statuses/", 102 "useragent":sys.argv[0]+" "+__version__, 103 "statusdelimeter1":"<p class=\"entry-title entry-content\">", 104 "statusdelimeter2":"</p>" 105 } 106 107 optlist, args=[], [] 108 try: 109 optlist, args=getopt.getopt(sysArgs, "u:svlc:h", ["username=", "status", "view", "list-commands", "config=", "help"]) 110 except: 111 usage() 112 113 for option, value in optlist: 114 if option=="-c" or option=="--config": 115 settings["config"]=value 116 117 try: 118 configreader.load(open(settings["config"], "r"), settings) 119 except IOError, e: 120 pass 121 122 for option, value in optlist: 123 if option=="-h" or option=="--help": 124 usage() 125 126 elif option=="-u" or option=="--username": 127 settings["username"]=value 128 elif option=="-s" or option=="--status": 129 mode=STATUS_MODE 130 elif option=="-v" or option=="--view": 131 mode=VIEW_MODE 132 elif option=="-l" or option=="--list=commands": 133 mode=COMMAND_MODE 134 135 if mode==STATUS_MODE: 136 if len(args)<1: 137 usage() 138 139 message=" ".join(args) 140 141 settings["password"]=getpass.getpass() 142 143 set_status(settings, message) 144 elif mode==VIEW_MODE: 145 print view_status(settings) 146 elif mode==COMMAND_MODE: 147 print COMMANDS 148 149 if __name__=="__main__": 150 try: 151 main() 152 except KeyboardInterrupt, e: 153 pass
1 2 #!/usr/bin/env python 3 4 __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" 5 __date__="3 Nov 2005 - 14 Feb 2007" 6 __copyright__="Copyright 2006 2007 Andrew Pennebaker" 7 __license__="GPL" 8 __version__="0.5" 9 __URL__="http://snippets.dzone.com/posts/show/2887" 10 11 from urllib import urlopen 12 13 import os 14 15 import sys 16 from getopt import getopt 17 18 def getURLName(url): 19 directory=os.curdir 20 21 name="%s%s%s" % ( 22 directory, 23 os.sep, 24 url.split("/")[-1] 25 ) 26 27 return name 28 29 def createDownload(url, proxy=None): 30 instream=urlopen(url, None, proxy) 31 32 filename=instream.info().getheader("Content-Length") 33 if filename==None: 34 filename="temp" 35 36 return (instream, filename) 37 38 def download(instream, outstream): 39 outstream.write(instream.read()) 40 41 outstream.close() 42 43 def usage(): 44 print "Usage: %s [options] <url1 url2 url3 ...>" % (sys.argv[0]) 45 print "\n--httpproxy <proxy>" 46 print "--ftpproxy <proxy>" 47 print "--gopherproxy <proxy>" 48 print "\n--help (usage)" 49 50 sys.exit() 51 52 def main(): 53 systemArgs=sys.argv[1:] # ignore program name 54 55 urls=[] 56 proxies={} 57 58 optlist=[] 59 args=[] 60 61 try: 62 optlist, args=getopt(systemArgs, None, ["url=", "httpproxy=", "ftpproxy=", "gopherproxy=", "help"]) 63 except Exception, e: 64 usage() 65 66 if len(args)<1: 67 usage() 68 69 for option, value in optlist: 70 if option=="--help": 71 usage() 72 73 elif option=="--httpproxy": 74 proxies["http"]=value 75 elif option=="--ftpproxy": 76 proxies["ftp"]=value 77 elif options=="--gopherproxy": 78 proxies["gopher"]=value 79 80 urls=args 81 82 for url in urls: 83 try: 84 outfile=open(getURLName(url), "wb") 85 fileName=outfile.name.split(os.sep)[-1] 86 87 url, length=createDownload(url, proxies) 88 if not length: 89 length="?" 90 91 print "Downloading %s (%s bytes) ..." % (url.url, length) 92 if length!="?": 93 length=float(length) 94 bytesRead=0.0 95 96 for line in url: 97 bytesRead+=len(line) 98 99 if length!="?": 100 print "%s: %.02f/%.02f kb (%d%%)" % ( 101 fileName, 102 bytesRead/1024.0, 103 length/1024.0, 104 100*bytesRead/length 105 ) 106 107 outfile.write(line) 108 109 url.close() 110 outfile.close() 111 print "Done" 112 113 except Exception, e: 114 print "Error downloading %s: %s" % (url, e) 115 116 if __name__=="__main__": 117 main()