All pastes #1567637 Raw Edit

richard

public text v1 · immutable
#1567637 ·published 2009-09-15 23:09 UTC
rendered paste body
#!/usr/bin/python2# ripfeed.py# (C) Richard Whitty 2007# Licence: as-is# Requires feedparser, from http://www.feedparser.org# This script hopes to take an RSS/Atom feed and dump it into a human readable format.# Either HTML or plain text. (only html atm)import feedparserimport sys,getopt,codecs,localefrom getopt import GetoptError# from: http://www.feedparser.org#from optparse import OptionParser# will replace this with optionparser after I'm done...#url = "http://lookingprettyill.blogspot.com/feeds/posts/default?max-results=5"url=''do_it_backwards=Falsedef usage() :	print "%s: rip an rss/atom feed and render to html." % sys.argv[0]	print 'Usage: %s [-r|--reverse] [-u|--url=] /path/or/url/to/feed'	print '-r/--reverse: prints the feed in reverse order, for example to do blogs in chronological order'	print '-u/--url:     the url of the feed to operate on. This qualifier can be ommitted.'	return# oarse the arguments using gnu getoptdef parse_args(argv) :	global url	global do_it_backwards	if len(argv) == 0 : 		usage()		sys.exit(1)	else:		try:			opts,args = getopt.getopt(argv,"rhu:",["reverse","help","url="])		except getopt.GetoptError,ex:			print "Error: %s" % ex.msg			usage()			sys.exit(1)		for opt,arg in opts :			if opt in ("-h","--help") :				usage()				sys.exit(1)			elif opt in ("-r","--reverse") :				do_it_backwards = True			elif opt in ("-u","--url"):				url=arg		if url == '' :			if len(args)>=1:				url=args[0];			else:				usage()				sys.exit(1)	return# Prints the html code associated with a single entry in the feed.def print_entry_html(entry):	if(entry.has_key('link')) :		print '<h4><a href="%s">' % entry.link		print '%s</a></h4>' % entry.title	else:		print '<h4>%s</h4>' % entry.title	print '<h4>Date: %s</h4>' % entry.date	print '<div>%s</div>' % entry.description.encode('latin1')	returndef main(argv):	parse_args(argv)	d = feedparser.parse(url)	# for some reason it won't throw an exception, so here's the error check	if d.feed == {} :		print '''<html><head><title>Error</title></head><body><h1>Error &quot;%s&quot; was not a valid feed</h1></body></html> ''' % url		sys.exit(1)	# start actually rendering the page.	print '''<html><head><title>%s</title></head><body>'<h1><a href="%s">%s</a></h1>	'''  % (d.feed.get('title','Untitled feed'), d.feed.get('link',''), d.feed.get('title','Untitled'))	if(d.feed.has_key('subtitle')) :		print '<h2>%s</h2>' % d.feed.subtitle	if(d.feed.has_key('summary')) :		print '<h2>%s</h2>' % d.feed.summary	if(d.feed.has_key('date')) :		print '<h3>Retreived on date: %s</h3>' % d.feed.date	if(d.feed.has_key('info')) :		print '<h3>%s</h3>' % d.feed.info	if(do_it_backwards) :		d.entries.reverse()	# do the contents	print '<h4>Post index</h4>'	print '<ol>'	# As we're just printing to stdout it needs to be iterated over twice	# could do some bufferring, but not going to atm.	for index,entry in enumerate(d.entries) :		print '<li><a href="#%s">%s</a></li>' %  (index,entry.get('title','untitled'))	print '</ol><hr/>'	# now loop through and print the actual entries.	for index,entry in enumerate(d.entries) :		print '<hr/><a name="%s" />' % index		print_entry_html(entry)		print '<br/><a href="#top">back to top</a>'	print '</body></html>'	returnif __name__ == '__main__' :	main(sys.argv[1:])