#!/usr/bin/python2 #Checks facebook for updates, using beautifulsoup and urlgrabber. #RSS URL for FB notifications URL = "URL for Facebook RSS feed" #Path to last message file. I think it needs to be fully qualified. #i.e. "/home/ben/.config/fblast" CONFIG = "/path/to/a/writable/file" #Path to an icon for the notifications. Use "" to disable the icon. ICON = "" #Time that the notification is visible for, in seconds. DELAY = 5 ### print "Loading Facebook Notifier" from urllib import FancyURLopener from bs4 import BeautifulSoup import sys import pynotify #Overrides the URLReader with a sane browser ID. useragent. Thingey. class URLReader(FancyURLopener): version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11' def main(): #Grab the RSS file print "Grabbing the RSS file:", URL soup = BeautifulSoup(grab(URL)) #Pull out events events = soup.find_all('item') for num, data in enumerate(events): events[num] = data.description.get_text() print "Found", #RSS lists them in reverse chronological order events.reverse() #Filter to new events, since last run events = new(events) #Notify for each. pynotify.init('Facebook Notifications') for event in events: print event notify(event) def grab(url): """Returns the text of the argument url.""" browser = URLReader() try: return browser.open(url).read() except: print "Could not load "+URL exit(1) def new(events): """Trims the RSS list of events to include only events that haven't been on the notify list yet.""" #Open the last shown message. try: last = open(CONFIG, 'r').read() except: last = "" #Find the index of the last notified message try: index = events.index(last) except: index = -1 print "Removing oldest", index+1, "events" #Save the new most recent message to the config file try: recent = open(CONFIG, 'w') recent.write(events[-1]) except: pass #Trim off events that've already been alerted. return events[index+1:] def notify(event): """Creates a notify-osd bubble for the event, with an appropriate icon.""" icon = ICON #Uncomment to use custom icons for friend requests, wall posts, pokes, etc. #if event.find('post'): icon = '' #if event.find('your friend request'): icon = '' note = pynotify.Notification(event, '', icon) note.set_timeout(1000*DELAY) note.show() #Calling "fb.py 10" will check for updates every 10 minutes. if len(sys.argv)>1: from time import sleep while True: main() print "Sleeping for", sys.argv[1], " minutes." sleep(60*float(sys.argv[1])) #Calling "fb.py" will check once, then exit. else: main()