feed2html.py feed-to-HTML converter in Python

About

feed2html.py is an open source/free source command line tool that takes a RSS/RDF/Atom news feed, and converts it into HTML documents (or other text documents using templates). It is developed in Python (2.3+ required), and it requires the universal feed parser to be installed separately. It can take input from an URL, a local file or from standard input, and can write the output to a local file or standard output. It has a built in template engine to allow customisation of the resulting HTML. feed2html.py can also be invoked from the web server as a CGI application.

feed2html.py should run whereever Python does. It is released as a free software using Python Software Foundation License.

Download feed2html.py

Usage

Command Line

Using feed2html.py from the command line:

% feed2html.py -h
Usage:
    feed2html.py [options]

Options:
    -D var=val  Define variable equals to value.
    -h          Display this help message.
    -i input    Feed input. It can be an URL, a local file name, or '-' for
                standard input. (Default standard input)
    -o output   HTML output. It can be a local file name, or '-' for standard
                output. (Default standard output)
    -t tplfile  Template file name. If omitted, default template will be used.

For example, downloading the latest news for nerds from Slashdot:

% feed2html.py -i http://slashdot.org/index.rss > slashdot.html

CGI

Using feed2html.py as CGI application:

  1. Place feed2html.py into your cgi-bin directory.
  2. Point your browser at it, i.e. http://<your site>/cgi-bin/feed2html.py?i=<input>&t=<tplfile>

Note that:

  • "input" parameter is always required, and it should always be an URL.
  • "tplfile" parameter is optional, and default template will be used if not present. Template file must be inside the same directory or in the sub-directories as the feed2html.py.

Template

Default Template

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=<tpl:charset filter="xmlq"/>" />
    <title><tpl:feed value="title" filter="xml" /></title>
  </head>
  <body>
    <div id="container">
      <h1><a href="<tpl:feed value="link" filter="xmlq"/>"><tpl:feed value="title" filter="xml"/></a></h1>
      <div id="content">
        <tpl:entries>
        <div class="entry">
          <h3><a href="<tpl:entry value="link" filter="xmlq"/>"><tpl:entry value="title" filter="xml" /></a></h3>
          <div class="entrybody"><tpl:entry value="summary" /></div>
          <p class="posted">Posted by <tpl:entry value="author" filter="xml" /> on <tpl:entry value="modified" /></p>
        </div>
        </tpl:entries>
      </div>
      <div id="footer">Generated on <tpl:now /> by <a href="<tpl:feed2html_url filter="xmlq"/>">Feed2HTML <tpl:feed2html_version /></a></div>
    </div>
  </body>
</html>

Template Tags

There are two types of tags - control tags and value tags. Control tags let you iterate through a list, or allows conditional execution of a template segment. Value tags injects value in the place where the tag is. These tags closely follow what feedparser provides.

<tpl:entries> ... </tpl:entries>
Iterate through all entries.
<tpl:contents> ... </tpl:contents>
Iterate through full content of the current entry.
<tpl:contributors> ... </tpl:contributors>
Iterate through the contributors of the current entry.
<tpl:enclosures> ... </tpl:enclosures>
Iterate through the enclosures of the current entry.
<tpl:feed value="..."/>
Print a value related to the feed.
<tpl:entry value="..."/>
Print a value related to the current feed.
<tpl:content value="..."/>
Print a value related to the currently selected current of the current entry.
<tpl:contributor value="..."/>
Print a value related to the currently selected contributor of the current entry.
<tpl:enclosure value="..."/>
Print a value related to the currently selected enclosure of the current entry.

XXX: template section to be written.

History

  • 1.0 - 2 May 2005