rss feed generation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blake Garner

    rss feed generation

    I'm looking for suggestions on how to approach generating rss feed
    ..xml files using python. What modules to people recommend I start
    with?

    Thanks!
    Blake
  • alex23

    #2
    Re: rss feed generation

    On Sep 10, 7:30 am, "Blake Garner" <trodemas...@gm ail.comwrote:
    I'm looking for suggestions on how to approach generating rss feed
    .xml files using python. What modules to people recommend I start
    with?
    A Python library for generating RSS 2.0 feeds.

    Comment

    • Walter Cruz

      #3
      Re: rss feed generation

      On Tue, Sep 9, 2008 at 9:35 PM, alex23 <wuwei23@gmail. comwrote:
      On Sep 10, 7:30 am, "Blake Garner" <trodemas...@gm ail.comwrote:
      >I'm looking for suggestions on how to approach generating rss feed
      >.xml files using python. What modules to people recommend I start
      >with?
      pylons webhelpers makes a good work too.

      []'s
      - Walter

      Comment

      • Tim Golden

        #4
        Re: rss feed generation

        Walter Cruz wrote:
        On Tue, Sep 9, 2008 at 9:35 PM, alex23 <wuwei23@gmail. comwrote:
        >On Sep 10, 7:30 am, "Blake Garner" <trodemas...@gm ail.comwrote:
        >>I'm looking for suggestions on how to approach generating rss feed
        >>.xml files using python. What modules to people recommend I start
        >>with?
        I just rolled something with ElementTree. No idea how it
        stacks up against validity checks, but it seems to be
        accepted by a couple of standard feed readers.

        (sorry; longish code segment)

        <code>
        import os, sys
        from datetime import datetime
        try:
        from xml.etree import cElementTree as ET
        except ImportError:
        from elementtree import ElementTree as ET

        class Feed (object):

        ENCODING = "utf-8"

        def __init__ (self, title, link, description=Non e, author=None, updated_on=None ):
        self.title = title
        self.link = link
        self.descriptio n = description
        self.author = author
        self.updated_on = updated_on or datetime.now ()
        self.entries = []

        def add_entry (self, title, link, content, summary=None, id=None, updated_on=None ):
        self.entries.ap pend (
        dict (
        title=title,
        link=link,
        content=content ,
        summary=summary ,
        id=id or link,
        updated_on=upda ted_on or datetime.now ()
        )
        )

        def write_rss (self, ofile=sys.stdou t, encoding=ENCODI NG):
        rss = ET.Element ("rss", version="2.0")
        channel = ET.SubElement (rss, "channel")
        ET.SubElement (channel, "title").te xt = self.title
        ET.SubElement (channel, "link").tex t = self.link
        ET.SubElement (channel, "description"). text = self.descriptio n

        for entry in self.entries:
        item = ET.SubElement (channel, "item")
        ET.SubElement (item, "title").te xt = entry['title']
        ET.SubElement (item, "link").tex t = entry['link']
        ET.SubElement (item, "guid").tex t = entry['id']
        ET.SubElement (item, "description"). text = entry['content']

        ET.ElementTree (rss).write (ofile, encoding=encodi ng)

        def write_atom (self, ofile=sys.stdou t, encoding=ENCODI NG):
        feed = ET.Element ("feed")
        ET.SubElement (feed, "title", type="text").te xt = self.title
        ET.SubElement (feed, "link", rel="self", href=self.link)
        if self.descriptio n:
        ET.SubElement (feed, "subtitle", type="text").te xt = self.descriptio n
        if self.author:
        name, email = self.author
        author = ET.SubElement (feed, "author")
        ET.SubElement (author, "name").tex t = name
        ET.SubElement (author, "email").te xt = email
        ET.SubElement (feed, "updated"). text = self.updated_on .isoformat ()

        for entry in self.entries:
        e = ET.SubElement (feed, "entry")
        ET.SubElement (e, "title").te xt = entry['title']
        ET.SubElement (e, "link", href=entry['link'])
        ET.SubElement (e, "id").text = entry['id']
        ET.SubElement (e, "updated"). text = entry['updated_on'].isoformat ()
        if entry['summary']:
        ET.SubElement (e, "summary"). text = entry['summary']
        ET.SubElement (e, "content", type="html").te xt = "<![CDATA[%s]]>" % entry['content']

        ET.ElementTree (feed).write (ofile, encoding=encodi ng)

        if __name__ == '__main__':
        feed = Feed ("Feed for example.org", "http://example.org", author=("Tim Golden", "tim@example.or g"))
        for i in range (5):
        feed.add_entry ("Entry %d" % i, "http://example.org/entry/%d" % i, "Contents for entry %d" % i, id="entry#%d" % i)
        feed.write_atom (open ("feed.atom.xml ", "w"), "iso-8859-1")
        feed.write_rss (open ("feed.rss.xml" , "w"), "iso-8859-1")

        </code>

        TJG

        Comment

        • Tim Golden

          #5
          Re: rss feed generation

          Tim Golden wrote:
          Walter Cruz wrote:
          >On Tue, Sep 9, 2008 at 9:35 PM, alex23 <wuwei23@gmail. comwrote:
          >>On Sep 10, 7:30 am, "Blake Garner" <trodemas...@gm ail.comwrote:
          >>>I'm looking for suggestions on how to approach generating rss feed
          >>>.xml files using python. What modules to people recommend I start
          >>>with?
          >
          I just rolled something with ElementTree. No idea how it
          stacks up against validity checks, but it seems to be
          accepted by a couple of standard feed readers.

          [... snip messy code ...]

          Well that didn't come out very well. If anyone's interested,
          I've dropped the module here:



          TJG

          Comment

          Working...