#!/usr/bin/python

#
# Feed Speaker
#   Janitha Karunaratne <janitha@janitha.com>
#

#
# This script will monitor any number of feeds and speak
# out loud (using Festival Text-to-Speech) as they are updated
#

#
# Notice
#  You are free to do whatever you want with this script, 
#  modifying and spreading it is highly encouraged.
#  Just keep this top header and preserve the credits please. 
#

#
# Requirements
#  * Python (duh..)
#  * Festival
#

#
# Usage
#    Add any RSS feed to the 'feeds' array below
#    Run the script and let it run, any new updates will be spoken
#
#    Twitter: Replace the username/password below
#    Facebook: Visit http://facebook.com/friends/?view=status and copy the FeedURL from there
#    Any RSS feed really
#


import os
import sys
import time
import urllib2
#import pynotify
import xml.dom.minidom
import unicodedata



################################### 
# Options 

feeds = [
#   'http://www.facebook.com/feeds/friends_status.php?id=XXXXXXXX&key=XXXXXXXX&format=rss20',
#   'http://username:password@www.twitter.com/statuses/friends_timeline.rss',  
    'http://twitter.com/statuses/public_timeline.rss'
    ]

interval = 30

###################################



msgs = []
#pynotify.init("Facebook Status Feed")

def fetchfeed(feed, notify):

    tts = os.popen("festival --tts", "w")
    req = urllib2.Request(feed, None, {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'})
    fbfeed = urllib2.urlopen(req).read()
    dom = xml.dom.minidom.parseString(fbfeed)
    
    for item in dom.getElementsByTagName("item"):
        title = item.getElementsByTagName("title")[0].childNodes[0].data
        guid = item.getElementsByTagName("guid")[0].childNodes[0].data
        titleascii = unicodedata.normalize('NFKD', title).encode('ascii','ignore')

        if guid not in msgs:
            msgs.append(guid)

            if notify:            
#                n = pynotify.Notification(titleascii)
#                n.set_urgency(pynotify.URGENCY_NORMAL)
#                n.show()
                tts.write(titleascii+". .")
                print titleascii

    tts.close()

def main():

    notify = 0
    while 1:
        for feed in feeds:
            fetchfeed(feed, notify)
        time.sleep(interval)
        notify=1

if __name__ == "__main__":
    sys.exit(main())

