2009-10-11 12:06:44 -03:00
|
|
|
"""
|
|
|
|
A demo that reads in an RSS XML document and emits an HTML file containing
|
|
|
|
a list of the individual items in the feed.
|
|
|
|
"""
|
|
|
|
|
2000-10-16 12:27:05 -03:00
|
|
|
import sys
|
2009-10-11 12:06:44 -03:00
|
|
|
import codecs
|
2000-10-16 12:27:05 -03:00
|
|
|
|
|
|
|
from xml.sax import make_parser, handler
|
|
|
|
|
|
|
|
# --- Templates
|
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
top = """\
|
2000-10-16 12:27:05 -03:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
2009-10-11 12:06:44 -03:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>%s</title>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<h1>%s</h1>
|
2000-10-16 12:27:05 -03:00
|
|
|
"""
|
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
bottom = """
|
2000-10-16 12:27:05 -03:00
|
|
|
</ul>
|
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
<hr>
|
|
|
|
<address>
|
|
|
|
Converted to HTML by rss2html.py.
|
|
|
|
</address>
|
2000-10-16 12:27:05 -03:00
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
</body>
|
|
|
|
</html>
|
2000-10-16 12:27:05 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
# --- The ContentHandler
|
|
|
|
|
|
|
|
class RSSHandler(handler.ContentHandler):
|
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
def __init__(self, out=sys.stdout):
|
2000-10-16 12:27:05 -03:00
|
|
|
handler.ContentHandler.__init__(self)
|
2009-10-11 12:06:44 -03:00
|
|
|
self._out = codecs.getwriter('utf-8')(out)
|
2000-10-16 12:27:05 -03:00
|
|
|
|
|
|
|
self._text = ""
|
|
|
|
self._parent = None
|
2009-10-11 12:06:44 -03:00
|
|
|
self._list_started = False
|
2000-10-16 12:27:05 -03:00
|
|
|
self._title = None
|
|
|
|
self._link = None
|
|
|
|
self._descr = ""
|
|
|
|
|
|
|
|
# ContentHandler methods
|
|
|
|
|
|
|
|
def startElement(self, name, attrs):
|
|
|
|
if name == "channel" or name == "image" or name == "item":
|
|
|
|
self._parent = name
|
|
|
|
|
|
|
|
self._text = ""
|
|
|
|
|
|
|
|
def endElement(self, name):
|
|
|
|
if self._parent == "channel":
|
|
|
|
if name == "title":
|
|
|
|
self._out.write(top % (self._text, self._text))
|
|
|
|
elif name == "description":
|
|
|
|
self._out.write("<p>%s</p>\n" % self._text)
|
|
|
|
|
|
|
|
elif self._parent == "item":
|
|
|
|
if name == "title":
|
|
|
|
self._title = self._text
|
|
|
|
elif name == "link":
|
|
|
|
self._link = self._text
|
|
|
|
elif name == "description":
|
|
|
|
self._descr = self._text
|
|
|
|
elif name == "item":
|
|
|
|
if not self._list_started:
|
|
|
|
self._out.write("<ul>\n")
|
2009-10-11 12:06:44 -03:00
|
|
|
self._list_started = True
|
2000-10-16 12:27:05 -03:00
|
|
|
|
|
|
|
self._out.write(' <li><a href="%s">%s</a> %s\n' %
|
|
|
|
(self._link, self._title, self._descr))
|
|
|
|
|
|
|
|
self._title = None
|
|
|
|
self._link = None
|
|
|
|
self._descr = ""
|
|
|
|
|
|
|
|
if name == "rss":
|
|
|
|
self._out.write(bottom)
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2000-10-16 12:27:05 -03:00
|
|
|
def characters(self, content):
|
|
|
|
self._text = self._text + content
|
|
|
|
|
|
|
|
# --- Main program
|
|
|
|
|
2009-10-11 12:06:44 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = make_parser()
|
|
|
|
parser.setContentHandler(RSSHandler())
|
|
|
|
parser.parse(sys.argv[1])
|