#!/usr/local/bin/python

import time
import feedparser
from netgrowl import *

from socket import *

GROWL_HOST="10.10.10.253"

RSS_FILE="/home/xmltok/.rss"

a="GrowlRSS"
n="New RSS Event"
st=False

class ee:
   def __init__(self, et, ft):
      self.et = et
      self.ft = ft

   def __eq__(self, other):
      if self.et == other.et and self.ft == other.ft:
        return True

print "Registering with Growl"
addr = (GROWL_HOST, GROWL_UDP_PORT)
s = socket(AF_INET,SOCK_DGRAM)
p = GrowlRegistrationPacket(application=a)
p.addNotification(notification=n)
s.sendto(p.payload(), addr)
s.close();

print "Monitoring %s" % RSS_FILE

r = []

while 1:
   s = open(RSS_FILE).readlines()
   for url in s:
       x = feedparser.parse(url.strip())
       for e in x.entries:
           if not ee(e.title, x.feed.title) in r:
             s = socket(AF_INET,SOCK_DGRAM)
             p = GrowlNotificationPacket(application=a,notification=n,description=e.title,title=x.feed.title,sticky=st)
             s.sendto(p.payload(), addr)
             s.close();
           r.append(ee(e.title, x.feed.title))
   st=True
   time.sleep(300)
