#!/usr/bin/env python # toot.py - Butler reads you your Twitter # Copyright (C) 2008 Lenny Domnitser # # To run this, you need to install python-simplejson and flite # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import getpass, os, time, urllib2 import simplejson WAIT = 60 * 2 LAST_READ_FILE = os.path.expanduser('~/.toot') DATA_URL = 'https://twitter.com/statuses/friends_timeline.json' user = raw_input('Username: ') passwd = getpass.getpass() passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(None, DATA_URL, user, passwd) handler = urllib2.HTTPBasicAuthHandler(passmgr) opener = urllib2.build_opener(handler) try: last_read = int(open(LAST_READ_FILE).read()) except: last_read = 0 while True: try: flite = os.popen('flite', 'w') rawdata = opener.open(DATA_URL).read() data = simplejson.loads(rawdata) local_last = 0 for message in data: if message['id'] > last_read: print >> flite, message['user']['name'].encode('utf8'), 'says:', print >> flite, message['text'].encode('utf8') + '.' if message['id'] > local_last: local_last = message['id'] last_read = local_last try: lrf = open(LAST_READ_FILE, 'w') print >> lrf, last_read lrf.close() except: pass flite.close() time.sleep(WAIT) except KeyboardInterrupt: break except Exception, e: print 'Unhandled exception (continuing): ', e