-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.py
213 lines (207 loc) · 8.41 KB
/
cron.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
import twitter
import config
import utils
import logging
import traceback
from mylocale import gettext
from time import time
from StringIO import StringIO
from google.appengine.api import xmpp
from google.appengine.api.capabilities import CapabilitySet
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.runtime.apiproxy_errors import DeadlineExceededError, CapabilityDisabledError
from db import Db, GoogleUser, TwitterUser, IdList, Session, MODE_HOME, MODE_LIST, MODE_MENTION, MODE_DM
class cron_handler(webapp.RequestHandler):
def get(self, cron_id):
cron_id = int(cron_id)
data = Session.get_all(shard=cron_id)
for u in data:
jid = u.key().name()
try:
self.process(u)
except CapabilityDisabledError:
try:
xmpp.send_presence(jid, presence_show=xmpp.PRESENCE_SHOW_AWAY)
except xmpp.Error:
pass
else:
try:
xmpp.send_presence(jid)
except xmpp.Error:
pass
def process(self, u):
jid = u.key().name()
try:
flag = xmpp.get_presence(jid)
except (xmpp.Error, DeadlineExceededError):
flag = True
if not flag:
u.delete()
return
google_user = GoogleUser.get_by_jid(jid)
if google_user is None:
u.delete()
return
time_delta = int(time()) - google_user.last_update
if time_delta < google_user.interval * 60 - 30:
return
_ = lambda x: gettext(x, locale=google_user.locale)
twitter_user = TwitterUser.get_by_twitter_name(google_user.enabled_user, google_user.jid)
if twitter_user is None:
google_user.enabled_user = ''
Db.set_datastore(google_user)
return
api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
consumer_secret=config.OAUTH_CONSUMER_SECRET,
access_token_key=twitter_user.access_token_key,
access_token_secret=twitter_user.access_token_secret)
try:
self._user = api.verify_credentials()
if not self._user or 'screen_name' not in self._user:
raise twitter.TwitterError
except twitter.TwitterError:
google_user.retry += 1
if google_user.retry >= config.MAX_RETRY:
GoogleUser.disable(jid=google_user.jid)
xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION'))
else:
Db.set_cache(google_user)
return
finally:
if google_user.retry > 0:
google_user.retry = 0
Db.set_cache(google_user)
if twitter_user.twitter_name != self._user['screen_name']:
twitter_user.twitter_name = self._user['screen_name']
Db.set_cache(twitter_user)
google_user.enabled_user = self._user['screen_name']
Db.set_cache(google_user)
utils.set_jid(google_user.jid)
home_statuses = []
home_mention_statuses = []
all_statuses = []
at_username = '@' + google_user.enabled_user
if google_user.display_timeline & MODE_HOME or google_user.display_timeline & MODE_MENTION:
home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id, async=True)
else:
home_rpc = None
if google_user.display_timeline & MODE_LIST:
list_rpc = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id,
since_id=google_user.last_list_id, async=True)
else:
list_rpc = None
if google_user.display_timeline & MODE_MENTION:
mention_rpc = api.get_mentions(since_id=google_user.last_mention_id, async=True)
else:
mention_rpc = None
if google_user.display_timeline & MODE_DM:
dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id, async=True)
else:
dm_rpc = None
if google_user.display_timeline & MODE_HOME:
try:
home_statuses = api._process_result(home_rpc)
if home_statuses:
all_statuses.extend(home_statuses)
if home_statuses[0]['id'] > google_user.last_msg_id:
google_user.last_msg_id = home_statuses[0]['id']
except twitter.TwitterInternalServerError:
pass
except BaseException:
err = StringIO('')
traceback.print_exc(file=err)
logging.error(google_user.jid + ' Home:\n' + err.getvalue())
if google_user.display_timeline & MODE_MENTION:
try:
statuses = api._process_result(mention_rpc)
if statuses:
all_statuses.extend(statuses)
if statuses[0]['id'] > google_user.last_mention_id:
google_user.last_mention_id = statuses[0]['id']
if not google_user.display_timeline & MODE_HOME:
try:
home_statuses = api._process_result(home_rpc)
except twitter.TwitterInternalServerError:
pass
except BaseException:
err = StringIO('')
traceback.print_exc(file=err)
logging.error(google_user.jid + ' Home:\n' + err.getvalue())
else:
if home_statuses:
if home_statuses[0]['id'] > google_user.last_msg_id:
google_user.last_msg_id = home_statuses[0]['id']
home_mention_statuses = [x for x in home_statuses if
at_username in x['text'] and x['id'] > google_user.last_mention_id]
if home_mention_statuses:
all_statuses.extend(home_mention_statuses)
except twitter.TwitterInternalServerError:
pass
except BaseException:
err = StringIO('')
traceback.print_exc(file=err)
logging.error(google_user.jid + ' Mention:\n' + err.getvalue())
if google_user.display_timeline & MODE_LIST:
try:
statuses = api._process_result(list_rpc)
if statuses:
if statuses[0]['id'] > google_user.last_list_id:
google_user.last_list_id = statuses[0]['id']
for i in range(len(statuses) - 1, -1, -1):
if at_username in statuses[i]['text'] and statuses[i]['id'] <= google_user.last_mention_id:
del statuses[i]
all_statuses.extend(statuses)
except twitter.TwitterInternalServerError:
pass
except BaseException, e:
if 'Not found' not in e.message:
err = StringIO('')
traceback.print_exc(file=err)
logging.error(google_user.jid + ' List:\n' + err.getvalue())
if all_statuses:
all_statuses.sort(cmp=lambda x, y: cmp(x['id'], y['id']))
last = all_statuses[-1]['id']
for i in range(len(all_statuses) - 2, -1, -1):
if last == all_statuses[i]['id']:
del all_statuses[i]
else:
last = all_statuses[i]['id']
content = utils.parse_statuses(all_statuses, filter_self=True, reverse=False)
if content.strip():
IdList.flush(google_user.jid)
while CapabilitySet('xmpp').is_enabled():
try:
xmpp.send_message(google_user.jid, content)
except xmpp.Error:
pass
else:
break
if google_user.display_timeline & MODE_DM:
try:
statuses = api._process_result(dm_rpc)
content = utils.parse_statuses(statuses)
if content.strip():
while CapabilitySet('xmpp').is_enabled():
try:
xmpp.send_message(google_user.jid, _('DIRECT_MESSAGES') + '\n\n' + content)
except xmpp.Error:
pass
else:
break
if statuses[-1]['id'] > google_user.last_dm_id:
google_user.last_dm_id = statuses[-1]['id']
except twitter.TwitterInternalServerError:
pass
except BaseException:
err = StringIO('')
traceback.print_exc(file=err)
logging.error(google_user.jid + ' DM:\n' + err.getvalue())
google_user.last_update = int(time())
Db.set_datastore(google_user)
def main():
application = webapp.WSGIApplication([('/cron(\d+)', cron_handler)], debug=True)
run_wsgi_app(application)
if __name__ == "__main__":
main()