#!/usr/bin/python try: import os, sys, pynotify, pycurl, urllib, time, ConfigParser import cStringIO from cStringIO import StringIO from xml.etree import cElementTree as ET except: print "One or more of the required modules cannot be found.\n" config = ConfigParser.ConfigParser() confPath = os.environ['HOME'] + '/.twittipy' if(os.path.isfile(confPath)): config.readfp(open(confPath)) try: username = config.get('general', 'username') password = config.get('general', 'password') except: sys.stderr.write("Couldn't find access credentials in config file.\n") sys.exit(1) try: lastUpdate = config.get('general', 'last_update') except: lastUpdate = time.strftime('%Y-%m-%dT%H:%I:%S+00:00', time.gmtime()) try: updateInterval = config.getfloat('general', 'update_interval') except: updateInterval = 240.0 else: sys.stderr.write("Couldn't find config file.\n") sys.exit(1) def saveConfig(confPath): fp = open(confPath, 'w') fp.write("[general]\n") fp.write("username = " + username + "\n") fp.write("password = " + password + "\n") fp.write("update_interval = " + str(updateInterval) + "\n") fp.write("last_update = " + lastUpdate + "\n") fp.close() if pynotify.init("twittipy"): while 1: params = {'since': lastUpdate} try: ob = StringIO() twitterConn = pycurl.Curl() twitterConn.setopt(twitterConn.URL, 'http://twitter.com/statuses/friends_timeline.xml') twitterConn.setopt(twitterConn.USERPWD, username + ':' + password) twitterConn.setopt(twitterConn.POSTFIELDS, urllib.urlencode(params)) twitterConn.setopt(twitterConn.WRITEFUNCTION, ob.write) twitterConn.perform() twitterConn.close() if(ob.getvalue()): updates = ET.fromstring(ob.getvalue()) for status in updates.findall('status'): twitUser = status.find('user').find('name').text twitText = status.find('text').text n = pynotify.Notification(twitUser, twitText) if(len(twitText) >= 90): n.set_timeout(10000) else: n.set_timeout(5000) n.show() ob.close() lastUpdate = time.strftime('%Y-%m-%dT%H:%I:%S+00:00', time.gmtime()) saveConfig(confPath) except: sys.stderr.write("Couldn't fetch updates.\n") time.sleep(updateInterval) else: sys.stderr.write("Couldn't initialize pynotify.\n") sys.exit(1)