-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.py
354 lines (312 loc) · 12.7 KB
/
services.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import json
import urllib
import urllib2
from oauth2client.client import Credentials
from crontab import CronTab
import config
from googleapis import *
from helper import *
from query import AppQuery
__author__ = 'spance'
log = getLogger(__name__)
class CredentialSetup:
def __init__(self, mail):
if not mail:
raise Exception('mail is required.')
self.mail = mail
self.state = 'authorization'
self.authorization_code = None
def __acquireAuthorizationCode(self):
self.authorization_code = tryInput('Please enter authorization_code: ')
if self.authorization_code:
return True
def __acquireCredentials(self):
try:
id_and_credential = get_credentials(self.authorization_code, self.state)
self.userId = id_and_credential[0]
self.credential = id_and_credential[1]
return True
except BaseException as e:
if isinstance(e, GetCredentialsException):
raise AvailedException('Please check on the authorization_code and rerun this program.')
else:
log.exception('__acquireCredentials')
raise e
def __acquireTelephone(self):
reTel = re.compile('^\+?\d+$')
self.tel = tryInput('Please enter a telephone(eg. 123000 or +123000) :', re=reTel)
if reTel.match(self.tel):
return True
def __saveCredential(self):
credential_json = self.credential.to_json()
CredentialManager.persistAccount(self.mail, self.tel, self.userId, credential_json)
def interact(self):
try:
print 'Please copy the url into browser and give approval, then you can got the authorization code.'
print '-' * 60
print get_authorization_url(self.mail, self.state)
print '-' * 60
if not self.__acquireAuthorizationCode():
return False
if not self.__acquireCredentials():
return False
if not self.__acquireTelephone():
return False
self.__saveCredential()
CronTask().create()
except BaseException as e:
if isinstance(e, AvailedException):
print(e.message)
else:
log.exception('setup')
class CredentialManager:
@staticmethod
def listAccounts():
""" query all accounts from database. within close connection.
:return: resultSet tuple
"""
sql = 'SELECT mail,create_time FROM authorization'
query = AppQuery()
results = []
try:
for row in query.query(sql):
results.append('%-36s %s' % (row[0], row[1]))
finally:
query.close()
return results
@staticmethod
def removeAccount(mail):
""" remove account/credential from db. within close connection.
:param mail:
:return: affected count
"""
sql = 'DELETE FROM authorization WHERE mail=?'
query = AppQuery()
try:
affected = query.update_auto(sql, mail)
sql = 'SELECT count(*) FROM authorization'
count = query.query(sql)[0][0]
if count == 0:
CronTask().remove()
return affected
finally:
query.close()
@staticmethod
def listCredentials():
""" query all credentials from database. within close connection.
:return: resultSet array
"""
sql = 'SELECT mail,tel,credential FROM authorization'
query = AppQuery()
try:
return query.query(sql)
finally:
query.close()
@staticmethod
def persistAccount(mail, tel, id, credential):
""" save an account/credential to database. within close connection.
:param mail:
:param tel:
:param id:
:param credential: credential should be a string.
"""
sql = 'INSERT INTO authorization (mail, user_id, tel, credential, create_time)' \
' VALUES (?, ?, ?, ?, ?)'
query = AppQuery()
try:
query.update_auto(sql, mail, id, tel, credential, datetime.datetime.now())
finally:
query.close()
@staticmethod
def updateCredential(mail, credential):
""" update credential of special account. within close connection.
:param mail:
:param credential: credential should be a string.
"""
sql = "UPDATE authorization SET credential=?, refresh_time=datetime('now') WHERE mail=?"
query = AppQuery()
try:
query.update_auto(sql, credential, mail)
finally:
query.close()
@staticmethod
def analyseAndPersistMessages(mail, messages):
""" save messages from api to database.last_msg. within close connection.
And take unread messages that not noticed over N-times during T-hours
1. delete messages if old records after T-hours(expired).
2. delete messages if old records not in this messages list(already read).
3. insert this messages with current time(local timezone). So all records is current unread messages.
4. total count of the records from sub query if records count <= N in a group by msg_id.
:param mail:
:param messages:
:return:
"""
args = []
_N, _T = 2, 6
for msg in messages:
args.append((mail, '%s+%s' % (msg['id'], msg['threadId'])))
query = AppQuery()
try:
sql = "DELETE FROM last_msg WHERE mail=? AND julianday(log_time)<julianday('now')-?"
cleared = query.update_auto(sql, mail, _T / 24.0)
log.debug('clear repeated expiry logs count=%d' % cleared)
if args:
sql = "delete from last_msg where mail=? and msg_id not in ('%s')" % "','".join(i[1] for i in args)
cleared = query.update_auto(sql, mail)
log.debug('clear read count=%d' % cleared)
sql = "INSERT INTO last_msg (mail, msg_id, log_time) VALUES (?,?, datetime('now'))"
inserted = query.update_batch(sql, args)
log.debug('insert unread count=%d' % inserted)
sql = 'SELECT count(*) FROM (' \
' SELECT msg_id FROM last_msg ' \
' WHERE mail=? ' \
' GROUP BY msg_id ' \
' HAVING count(*) <= ?' \
' )'
lastUnread = query.query(sql, mail, _N)[0][0]
log.debug('lastUnread count=%d' % lastUnread)
return lastUnread
else:
sql = "DELETE FROM last_msg WHERE mail=?"
cleared = query.update_auto(sql, mail)
log.debug('clear read COUNT=%d' % cleared)
return 0
finally:
query.close()
@staticmethod
def prepareCredential(mail, credential_json):
""" build credential object from json, and check expiry or refresh it.
may be throw exceptions while dealing with google-apis.
:param mail:
:param credential_json:
:return: credential obj
"""
credential = Credentials.new_from_json(credential_json)
if credential.access_token_expired:
credential.refresh(httplib2.Http())
CredentialManager.updateCredential(mail, credential.to_json())
log.info('Credential refreshed and saved.')
return credential
class CronTask:
def __init__(self, interval=5):
self.cron = '*/%d * * * *' % interval
self.cmd_file = 'gnoti.py'
self.cmd_args = '--scheduled-task'
self.cmd = 'python %s/%s %s &' % (appPath(), self.cmd_file, self.cmd_args)
def create(self):
cron = CronTab(user=True)
jobs = cron.find_command(self.cmd_args)
changed, jobExisted = 0, False
if jobs:
for job in jobs:
if not job.is_enabled() or not job.is_valid():
log.info('remove the invalid cron task[%s]' % job)
changed += cron.remove(job)
else:
jobExisted = True
if not jobExisted:
job = cron.new(self.cmd)
job.setall(self.cron)
changed += 1
log.info('create a new cron task[%s]' % job)
if changed > 0:
try:
cron.write()
except:
log.exception('An Error when writing cron configuration')
else:
log.info('the cron task is already exists. No changes.')
def remove(self):
cron = CronTab(user=True)
jobs = cron.find_command(self.cmd_args)
changed = 0
for job in jobs:
changed += cron.remove(job)
log.info('remove task[%s]' % job)
if changed > 0:
try:
cron.write()
except:
log.exception('An Error when writing cron configuration')
else:
log.info('the cron task is already exists. No changes.')
def maskedAccount(account):
""" to ensure account privacy, make account to a masked text.
:param account:
:return:
"""
suffix = None
if '@' in account:
name, _, suffix = account.partition('@')
else:
name = account
_len = len(name)
if _len <= 3:
return account
plen = 2 if _len > 3 else 1
name = '%s%s%s' % (name[:plen], '*' * (_len - 2 * plen), name[_len - plen:])
return '%s@%s' % (name, suffix) if suffix else name
class CheckMailTask(object):
def __sendNotifications(self, mail, tel, count):
url, api_code, api_result = None, None, None
conf = config.getConfiguration()
assert conf is not None
for method in conf.methods:
try:
template = {
'msg': 'Your account %s have %d unread messages.' % (maskedAccount(mail), count),
'to': tel
}
url, data = method.api, {}
# apply variables( {msg} {to} ) to http parameters.
if method.data:
for (k, v) in method.data.items():
k = k.format(**template) if '{' in k else k
v = v.format(**template) if '{' in v else v
data[k] = v
data = urllib.urlencode(data)
if method.method == 'GET': # else POST : retain data.
# todo parse url, append to query_part
url = '%s?%s' % (url, data) # GET : append parameters to url and remove data.
data = None
# apply basic auth
if method.basic_auth:
urllib2.install_opener(urllib2.build_opener(method.basic_auth))
# headers=None is not allowed.
headers = method.headers if method.headers else {}
req = urllib2.Request(url, data=data, headers=headers)
log.info('Call %s_api HTTP/%s ==> %s' % (method.name, method.method, url))
if data:
log.debug('Post-data = %s', json.dumps(data))
if headers:
log.debug('Header = %s', json.dumps(headers))
# if 1 == 1: return
httpCall = urllib2.urlopen(req, timeout=5)
api_code = httpCall.getcode()
api_result = httpCall.read()
log.info('Responded %s_api ==> [Code=%d] %s' % (method.name, api_code, api_result))
except:
log.exception('Error call %s_api [Code=%d] %s' % (method.name, api_code, api_result))
def __check(self, info):
mail, tel, credential = info # tuple
log.info('Working for account[%s]' % mail)
if not tel:
log.error('Unable send notifications to the account [%s] without tel.' % mail)
return
credential = CredentialManager.prepareCredential(mail, credential)
unreadMails = findUnreadMails(mail, credential)
log.info('Found unread&inbox COUNT=%d' % len(unreadMails))
lastUnread = CredentialManager.analyseAndPersistMessages(mail, unreadMails)
log.info('Checked account[%s] last-unread&inbox COUNT=%d' % (mail, lastUnread))
if lastUnread > 0:
# todo should be consider sending on an available time frame
self.__sendNotifications(mail, tel, lastUnread)
def run(self):
try:
allList = CredentialManager.listCredentials()
log.info('Checking Task is running. ACCOUNTS-TOTAL = %d', len(allList))
for info in allList:
self.__check(info)
log.info('Task is complete.')
except:
log.exception('Error when running the task!')