usr bin env python encoding utf-8 twitter-juick py Created by Alexey K

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/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()