The universal feedparser

Sometimes things don't have to be difficult. For example, incorporating a RSS feed summary in another website. With the universal feedparser this takes only a few lines of code.

The universal feedparser is a old but venerable piece of software that makes it a walk in the park to retrieve information from an RSS or Atom feed in Python.

The following code is what I actually use to incorporate the title and first paragraph of the postings on this blog on my homepage.

  1. import feedparser  
  2. import re  
  3.   
  4. firstp=re.compile(r'^.*?<p>(.*?)</p>')  
  5.   
  6. url="http://michelanders.blogspot.com/feeds/posts/default"  
  7. feed=feedparser.parse(url)  
  8.   
  9. print '<h2><a href="%s">%s</a></h2>'%(feed.feed.link,feed.feed.title)  
  10. print '<div class="feeditemlist">'  
  11. for e in feed.entries:  
  12.         mo=firstp.search(e.description)  
  13.         short=mo.group(1if not mo is None else 'no summary available'  
  14.         date="-".join(map(str,e.updated_parsed[:3]))  
  15.         summary='<p>%s</p><a href="%s">read more</a>'%(short,e.link)  
  16.         print ''''' 
  17.         <h3><a href="#"> 
  18.         <span class="feeditemdate">%s</span> 
  19.         <span class="feeditemtitle">%s</span> 
  20.         </a></h3> 
  21.         <div class="feeditemsummary">%s</div>'''%(date,e.title,summary)  
  22. print '</div>'  

The html this script produces is easily readable by itself but can simply be converted to a jQueryUI Accordion widget to save space.

The only drawback is that the universal feed parser only works with Python 2.x