Simple Python RSS aggregator

July 01, 2007

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>\n"
body+="</ul>"



page.add_tag('body',body )

print start_output()

import chardet
final=""
for line in body.split("\n"):
	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()
Tagged with: en, python, snippet, planets
This post has 0 comments. Feel free to read them and leave your own.

Comment:

Author:
Email:
Website:
Comment:

About

I used to write in this blog, but I've found a better format to express myself. From now on, you may read my writings on ideas, programming and politics on my new wiki.

hCard

Name: Alcides Fonseca
Email:
MSN:
Gtalk:
Nov 24, 1988 40.197958, -8.408312

Tagcloud

Archives

Other links