Never been to DZone Snippets before?

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

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

tiny.conf

// Configuration for tiny.py

config = "tiny.conf"
service = "urltea"
urltea url = "http://urltea.com/api/text/?url="
tinyurl url = "http://tinyurl.com/api-create.php?"
description delimeter = "?"
description = ""

tea.py

// 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"
__license__="GPL"
__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 <url1> <url2> <url3> ..." % (sys.argv[0])
	print "-d|--description <comment>"
	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

tiny.py

#!/usr/bin/env python

"""Converts long URLs to tiny URLs, with either tinyurl, urltea, or a custom url."""

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="22 Jun 2007 - 24 Jun 2007"
__copyright__="Copyright 2007 Andrew Pennebaker"
__license__="GPL"
__version__="0.0.1"
__URL__="http://snippets.dzone.com/posts/show/4195"
__credits__="http://lateral.netmanagers.com.ar/weblog/2007/04/08.html#BB548"

import sys, getopt, urllib

import configreader

def tiny(url, settings):
	try:
		encodedurl=settings["posting url"]+urllib.urlencode({"url":url})
		instream=urllib.urlopen(encodedurl)
		tinyurl=instream.read()
		instream.close()

		if len(tinyurl)==0:
			return url

		if settings["service"]=="urltea" and len(settings["description"])>0:
				tinyurl+=settings["description delimeter"]+settings["description"]

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

def usage():
	print "Usage: %s [options] <url1> <url2> <url3> ..." % (sys.argv[0])
	print "\nDefaults to urlTea unless specified in options or a config file."
	print "\n-s|--service [tinyurl|urltea]"
	print "-u|--custom-url <posting url>"
	print "-d|--description <comment> May only be used with urltea."
	print "-c|--config <configfile>"
	print "-h|--help (usage)"

	sys.exit()

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

	settings={
		"config":"tiny.conf",
		"service":"urltea",
		"urltea url":"http://urltea.com/api/text/?url=",
		"tinyurl url":"http://tinyurl.com/api-create.php?",
		"description delimeter":"?",
		"description":""
	}

	try:
		optlist, args=getopt.getopt(systemArgs, "s:u:d:c:h", ["service=", "custom-url=", "description=", "config=", "help"])
	except:
		usage()

	for option, value in optlist:
		if option=="-c" or option=="--config":
			settings["config"]=value

	try:
		configreader.load(open(settings["config"], "r"), settings)
	except IOError, e:
		pass

	for option, value in optlist:
		if option=="-h" or option=="--help":
			usage()
		elif option=="-s" or option=="--service":
			settings["service"]=value
		elif option=="-d" or option=="--description":
			settings["description"]=value

	if settings["service"]!="urltea" and len(settings["description"])>0:
		usage()

	try:
		settings["posting url"]=settings[settings["service"]+" url"]
	except:
		usage()

	for option, value in optlist:
		if option=="-u" or option=="--custom-url":
			settings["posting url"]=value

	if len(args)<1:
		usage()

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

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

tw.py

// Sets and views Twitter status

#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="17 Jun 2007 - 28 Jun 2007"
__copyright__="Copyright 2007 Andrew Pennebaker"
__license__="GPL"
__version__="0.0.1"
__credits__="Based on tweetyPy (http://muffinresearch.co.uk/archives/2007/03/24/tweetypy-python-based-cli-client-for-twitter/)"
__URL__="http://snippets.dzone.com/posts/show/4150"

import sys, getopt, getpass, urllib, urllib2

import configreader

STATUS_MODE="STATUS"
VIEW_MODE="VIEW"
COMMAND_MODE="COMMAND"

COMMANDS="""Command\t\tMeaning

d username\tDirect Text
@username\tReply
follow username\tReceive updates via phone or IM
leave username\tStop following username
leave all\tStop following all friends
remove username\tRemove username from friends list
delete username\tDelete username from friends list
get username\tGet the last update from username
get\tGet the most recent updates from all friends
nudge username\tTwitter aks what the person is currently up to
whois username\tGet username's bio
add phonenumber\tSend text invite. If already a member, invite will turn into a friend request.
accept username\tAccept username as a friend
deny username\tDeny username as friend"""

def usage():
	print "Usage: %s [options]" % (sys.argv[0])
	print "\nWithout any options, uses status mode. Leftover args are concatenated to form message."
	print "\n-u|--username <username> specified in tw.conf"
	print "-s|--status mode"
	print "-v|--view status"
	print "-l|--list-commands List Twitter commands"
	print "-c|--config <configfile>"
	print "-h|--help"

	sys.exit()

def set_status(settings, status):
	auth=urllib2.HTTPPasswordMgrWithDefaultRealm()
	auth.add_password(None, settings["rootauthurl"], settings["username"], settings["password"])
	authHandler=urllib2.HTTPBasicAuthHandler(auth)
	opener=urllib2.build_opener(authHandler)

	url="http://twitter.com/statuses/update.xml"
	post=urllib.urlencode({"status":status})

	request=urllib2.Request(url, post)
	request.add_header("User-Agent", settings["useragent"])

	try:
		response=opener.open(request)
	except IOError, e:
		raise "Could not connect."

def view_status(settings):
	url="http://twitter.com/"+settings["username"]
	message=""

	statusdelimeter1=settings["statusdelimeter1"]
	statusdelimeter2=settings["statusdelimeter2"]

	try:
		instream=urllib.urlopen(url)
		for line in instream:
			if statusdelimeter1 in line:
				message=line[
					line.index(statusdelimeter1)+len(statusdelimeter1):line.index(statusdelimeter2)
				]
				break
		instream.close()

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

def main():
	global STATUS_MODE
	global VIEW_MODE
	global COMMAND_MODE
	global COMMANDS

	sysArgs=sys.argv[1:]

	mode=STATUS_MODE

	settings={
		"config":"tw.conf",
		"username":"mcandre",
		"rootauthurl":"http://twitter.com/statuses/",
		"useragent":sys.argv[0]+" "+__version__,
		"statusdelimeter1":"<p class=\"entry-title entry-content\">",
		"statusdelimeter2":"</p>"
	}

	optlist, args=[], []
	try:
		optlist, args=getopt.getopt(sysArgs, "u:svlc:h", ["username=", "status", "view", "list-commands", "config=", "help"])
	except:
		usage()

	for option, value in optlist:
		if option=="-c" or option=="--config":
			settings["config"]=value

	try:
		configreader.load(open(settings["config"], "r"), settings)
	except IOError, e:
		pass

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

		elif option=="-u" or option=="--username":
			settings["username"]=value
		elif option=="-s" or option=="--status":
			mode=STATUS_MODE
		elif option=="-v" or option=="--view":
			mode=VIEW_MODE
		elif option=="-l" or option=="--list=commands":
			mode=COMMAND_MODE

	if mode==STATUS_MODE:
		if len(args)<1:
			usage()

		message=" ".join(args)

		settings["password"]=getpass.getpass()

		set_status(settings, message)
	elif mode==VIEW_MODE:
		print view_status(settings)
	elif mode==COMMAND_MODE:
		print COMMANDS

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

downloader.py

#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="3 Nov 2005 - 14 Feb 2007"
__copyright__="Copyright 2006 2007 Andrew Pennebaker"
__license__="GPL"
__version__="0.5"
__URL__="http://snippets.dzone.com/posts/show/2887"

from urllib import urlopen

import os

import sys
from getopt import getopt

def getURLName(url):
	directory=os.curdir

	name="%s%s%s" % (
		directory,
		os.sep,
		url.split("/")[-1]
	)

	return name

def createDownload(url, proxy=None):
	instream=urlopen(url, None, proxy)

	filename=instream.info().getheader("Content-Length")
	if filename==None:
		filename="temp"

	return (instream, filename)

def download(instream, outstream):
	outstream.write(instream.read())

	outstream.close()

def usage():
	print "Usage: %s [options] <url1 url2 url3 ...>" % (sys.argv[0])
	print "\n--httpproxy <proxy>"
	print "--ftpproxy <proxy>"
	print "--gopherproxy <proxy>"
	print "\n--help (usage)"

	sys.exit()

def main():
	systemArgs=sys.argv[1:] # ignore program name

	urls=[]
	proxies={}

	optlist=[]
	args=[]

	try:
		optlist, args=getopt(systemArgs, None, ["url=", "httpproxy=", "ftpproxy=", "gopherproxy=", "help"])
	except Exception, e:
		usage()

	if len(args)<1:
		usage()

	for option, value in optlist:
		if option=="--help":
			usage()

		elif option=="--httpproxy":
			proxies["http"]=value
		elif option=="--ftpproxy":
			proxies["ftp"]=value
		elif options=="--gopherproxy":
			proxies["gopher"]=value

	urls=args

	for url in urls:
		try:
			outfile=open(getURLName(url), "wb")
			fileName=outfile.name.split(os.sep)[-1]

			url, length=createDownload(url, proxies)
			if not length:
				length="?"

			print "Downloading %s (%s bytes) ..." % (url.url, length)
			if length!="?":
				length=float(length)
			bytesRead=0.0

			for line in url:
				bytesRead+=len(line)

				if length!="?":
					print "%s: %.02f/%.02f kb (%d%%)" % (
						fileName,
						bytesRead/1024.0,
						length/1024.0,
						100*bytesRead/length
					)

				outfile.write(line)

			url.close()
			outfile.close()
			print "Done"

		except Exception, e:
			print "Error downloading %s: %s" % (url, e)

if __name__=="__main__":
	main()
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS