import urllib2, simplejson, base64, xmpp, time, ConfigParser, sys config = ConfigParser.ConfigParser() try: config.read("details") except: print "Couldn't read details file" sys.exit() try: jabber_login = config.get("jabber", "username") jabber_password = config.get("jabber", "password") twitter_username = config.get("twitter", "username") twitter_password = config.get("twitter", "password") except: print "Couldn't find your details in the details file" sys.exit() def get_config(section, option, default): try: x = config.get(section, options) except: x = default return x last_id_file = get_config("files", "last_id", "last_id") # default connection details to Google Talk jabber_domain = get_config("jabber", "domain", "gmail.com") jabber_server = get_config("jabber", "server", "talk.google.com") jabber_port = int(get_config("jabber", "port", "5223")) cnx = xmpp.Client(jabber_domain, debug=[]) cnx.connect( server=(jabber_server,jabber_port) ) cnx.auth(jabber_login,jabber_password, 'identi.ca twitter reflector') url = 'http://twitter.com/statuses/user_timeline.json' req = urllib2.Request(url) req.add_header("Authorization", "Basic %s" % base64.encodestring('%s:%s' % (twitter_username, twitter_password))[:-1]) try: fp = urllib2.urlopen(req) except: print "Error from Twitter" sys.exit() timeline = simplejson.loads(fp.read()) fp.close() try: fp = open(last_id_file) last_id = int(fp.read().strip()) fp.close() except: last_id = 0 posts = [(x["text"],int(x["id"])) for x in timeline if (int(x["id"]) > last_id) and not x["text"].startswith("@")] posts.sort(lambda a,b:cmp(a[1],b[1])) for text, tid in posts: cnx.send( xmpp.Message( "update@identi.ca" ,text ) ) #print "Posting", text time.sleep(1) last_id = tid if posts: fp = open(last_id_file,"w") fp.write(str(last_id)) fp.close()