#!/usr/bin/env python
# encoding: utf-8
"""
twitter-juick.py
Created by Alexey Karguine on 2009-02-25.
Copyright (c) 2009 alexeykarguine@me.com. All rights reserved.
"""
import sys
import os
import xml.dom.minidom
import urllib
import xmpp
import cPickle
import logging
import daemonize
import time
import re
RSS = 'http://twitter.com/statuses/user_timeline/username.xml'
FROM_GMAIL_ID = "user@jabber.srv/twitter2juick"
GMAIL_PASS = "passwd"
GTALK_SERVER = "jabber.srv"
TO_GMAIL_ID = 'juick@juick.com'
TimeOut = 60
# Logging
curr_dir = os.getcwd()
logfile = os.path.join(curr_dir, 'twitter2juick.log')
errfile = os.path.join(curr_dir, 'twitter2juick_crash.log')
logging.basicConfig(filename=logfile, level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s")
# Pickle
curr_dir = os.getcwd()
lastseenfn = os.path.join(curr_dir, 'lastid.pickle')
# regular for tags
pattern = re.compile('^#')
def xmppConnect():
jid=xmpp.protocol.JID(FROM_GMAIL_ID)
cl=xmpp.Client(jid.getDomain(),debug=[])
if not cl.connect((GTALK_SERVER,5222)):
raise IOError('Can not connect to server.')
if not cl.auth(jid.getNode(),GMAIL_PASS,resource=jid.getResource()):
raise IOError('Can not auth with server.')
return cl
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def getValue(node, name):
"""Returns text value of node content"""
return getText(node.getElementsByTagName(name)[0].childNodes)
def getLastseen():
logging.debug('File: %s' % lastseenfn)
try:
pfile = open(lastseenfn, 'rb')
try:
lastseen = cPickle.load(pfile)
except EOFError:
logging.warn( "Pickle empty, passing.")
lastseen = 0
except IOError:
logging.warn("Pickle file not present.")
lastseen = 0
return lastseen
def saveLastseen(lastseen):
pfile = open(lastseenfn, 'wb')
cPickle.dump(lastseen, pfile, 2)
pfile.close()
def loop():
logging.debug('In loop')
lastid = getLastseen()
logging.debug('get last: %d' % lastid)
if lastid:
URL = RSS+'?since_id='+str(lastid)
else:
URL = RSS
logging.debug('URL: %s' % URL)
try:
data = urllib.urlopen(URL)
except:
logging.exception("Can't get RSS")
return
try:
dom = xml.dom.minidom.parse(data)
except:
logging.exception("Can't parse XML")
return
statuses = dom.getElementsByTagName('status')
statuses.reverse()
logging.debug('Got rss, len: %s' % len(statuses))
try:
cl = xmppConnect()
except:
logging.exception("Can't connect to GTalk")
return
logging.debug('Connected to jabber')
for status in statuses:
logging.debug('In loop cycle')
text = pattern.sub('*', getValue(status, 'text'))
id_ = int(getValue(status, 'id'))
is_reply = getValue(status, 'in_reply_to_screen_name')
if len(is_reply) == 0:
logging.info( "id: %s text: %s" % (id_, text) )
cl.send(xmpp.Message(TO_GMAIL_ID, text))
lastid = id_
logging.debug('ID: %d' % id_)
saveLastseen(lastid)
cl.disconnect()
logging.debug('EOF loop')
def main():
daemonize.daemonize(stderr=errfile)
while True:
try:
logging.debug('Starting new cycle')
loop()
except:
logging.exception('main')
finally:
time.sleep(TimeOut)
if __name__ == '__main__':
main()