More about Alcides Fonseca
Go Back

Simple Python RSS aggregator

Written by Alcides Fonseca at 2007/07/01

Finally today I revamped ideias3 homepage. It now has a simple page featuring a simple aggregator of this, sergio and ideias3 blogs. It was written in Python with a experimental version of pungi and using universal feedparser to get the info from the RSS feeds, and chardet to solve encoding problems. This one also uses a sorter I found out here instead of traditional bubblesort with auxiliar lists I used to use.



In a near future I’m thinking of creating a full aggregator to use in a project of mine, I will talk about soon.



Source code follows.




#!/usr/bin/python

from pungi import *
import feedparser
import sorter


page=Page('ideias3.html')

# IDEIAS3

posts = []
authors=["Ideias3","Alcides","Sérgio"]
feeds=["http://blog.ideias3.com/main/rss","http://feeds.feedburner.com/alcides","http://feeds.feedburner.com/sergiosantos"]

for feed in feeds:
d = feedparser.parse(feed)
for entry in d['entries'][:min(8,len(d['entries']))]:
title=str(entry['title'].encode('ascii', 'xmlcharrefreplace'))
link=str(entry['links'][0]['href'])
if 'updated_parsed' in entry.keys():
date=entry['updated_parsed']
else:
date=(0,0,0,0,0,0,0,0)
author=authors[feeds.index(feed)]
posts.append({'title':title,'link':link,'date':date,"author":author})

sorter.sort(posts, 'date')
posts.reverse()

body="<ul>"
for entry in posts[:min(15,len(posts))]:
body+="<li><div class='author'>"+entry['author']+":</div> <a href='"+entry['link']+"' target='_blank'>"+entry['title']+"</a></li> "
body+="</ul>"



page.add_tag('body',body )

print start_output()

import chardet
final=""
for line in body.split(" "):
try:
it = chardet.detect(line)
final+= line.replace("<br />","<br />").decode(it['encoding']).encode('utf-8')
except:
final+= line.replace("<br />","<br />")

page.add_tag('body',final)
print page.output()