Friday, December 07, 2007

RSS feed reader 3rd Update

Added toaster-pop-up like notification for updates . Clicking the Pop-up will open the corresponding blog entry in the browser.






from sgmllib import SGMLParser
import feedparser
from threading import Thread
from time import mktime, sleep
from re import sub
import pygtk
pygtk.require("2.0")
import gtk

class ParseDoc(SGMLParser):
def get_vals(self):
return self.datalist
def parse(self, data):
self.feed(data)
self.close()
def __init__(self, verbose=0):
SGMLParser.__init__(self, verbose=0)
self.datalist = []
def start_outline(self, attributes):
attrdict = {}
for name, value in attributes:
attrdict[name]=value
self.datalist.append(attrdict)


class FetchFeed(Thread):
def __init__(self, xmlurl):
Thread.__init__(self)
self.xmlurl = xmlurl
self.feedresult = None

def run(self):
d = feedparser.parse(self.xmlurl)
if d.feed.has_key('title'):
self.feedresult = d
def strip_html(text):
def fixup(m):
text = m.group(0)
if text[:1] == "<":
return ""
if text[:2] == "&#":
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
elif text[:1] == "&":
import htmlentitydefs
entity = htmlentitydefs.entitydefs.get(text[1:-1])
if entity:
if entity[:2] == "&#":
try:
return unichr(int(entity[2:-1]))
except ValueError:
pass
else:
return unicode(entity, "iso-8859-1")
return text
return sub("(?s)<[^>]*>|&#?\w+;", fixup, text)


class ParseBlogs(ParseDoc):

def __init__(self, opmldoc = None):
ParseDoc.__init__(self, verbose=0)
self.lastenty = None
if opmldoc:
fileobj = open(opmldoc);
filedata = fileobj.read()
fileobj.close()
ParseDoc.parse(self, filedata)
self.blogsdata = ParseDoc.get_vals(self)
del self.blogsdata[0]
self.opmldocflag = 1
self.resultlimit = 3

else:
self.blogsdata = ['http://mvblogs.org/mvblogs.xml']
self.opmldocflag = 0
self.resultlimit = None
self.feedresults = []

self.threadlist = []
self.lastparsetime = None

def getfeeds(self, min=0, max=1):

for i in self.blogsdata[min:max]:

if self.opmldocflag:
t = FetchFeed(i['xmlurl'])
else:
t = FetchFeed(self.blogsdata[0])
t.start()
self.threadlist.append(t)

for tl in self.threadlist:
tl.join()
self.getfeed(tl)
self.threadlist = []

def getfeed(self, tl):
results = []
if tl.feedresult:
tlfeed = tl.feedresult.feed
tlresult = tl.feedresult.entries

results.append(tlfeed)
results.append(tlresult)
self.feedresults.append(results)


def printfeeds(self, onlyupdates = 'false'):
print "printing"
for i in self.feedresults:#for each feed

#if limit not set fetch all entries
if not self.resultlimit:self.resultlimit = len(i[1])

for j in range(self.resultlimit):#for each entry

#if onlyupdates then ignore old entries
if onlyupdates == "true" and self.lastparsetime:
if mktime(i[1][j].updated_parsed) <= mktime(self.lastparsetime):
continue

if self.opmldocflag:
print i[0]['author']
print i[0]['title']

print i[1][j].date
print i[1][j].title
print i[1][j].link
print strip_html(i[1][j].description)
print "#"*100
#for updates display popup
if onlyupdates =="true" and self.lastparsetime:
toasterpop = Popup(i[1][j].title, i[1][j].link)

self.lastparsetime = self.feedresults[0][1][0].updated_parsed
self.feedresults = []

class Popup:
def clicky(self, widget, link=None):
os.system("firefox "+link)
gtk.main_quit()

def delete_event(self, widget, event, data=None):
return False

def destroy(self, widget, data=None):
gtk.main_quit()

def __init__(self,message = "Hello World", link="http://vishah.blogspot.com"):
self.toaster = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.toaster.set_title("New!!")
self.toaster.connect("destroy", self.destroy)
self.toaster.connect("delete_event", self.delete_event)
self.button = gtk.Button(message)
self.button.connect("clicked",self.clicky, link)
self.toaster.add(self.button)
self.toaster.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)
self.toaster.resize(150,150)
width, height = self.toaster.get_size()
self.toaster.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)
self.toaster.show_all()
self.main()


def main(self):
gtk.main()

#parseobj = ParseBlogs('mvblogs-export.xml')
#parseobj.getfeeds(0,3)
parseobj = ParseBlogs()
while(1):
parseobj.getfeeds()
parseobj.printfeeds('true')
sleep(20)



No comments: