-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
364 lines (316 loc) · 11.1 KB
/
user.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
from db import Db
from sender import Sender
from datetime import datetime
from time import time, sleep
from config import *
import json, re
from requests_oauthlib import OAuth2Session
from threading import Thread, active_count
from rq import Queue
from worker import conn
class User():
"""
Data type for users of this system.
Public methods:
* create
* get_by_email
* email
* token
* name
* created_at
* filters_made
* make_filter
* json
* delete_filter
* user_info
"""
def __init__(self, data=None):
if data == None:
self._email = None
self._token = None
self._filters_made = None
self._name = None
self._created_at = None
else:
data = json.loads(data)
self._email = data['email']
self._token = data['token']
self._filters_made = data['filters_made']
self._name = data['name']
self._created_at = datetime.fromtimestamp(data['created_at'])
def __repr__(self):
return f"{self.email()} - {self.name()}"
def create(self, email, name=None, token=None):
created_at = datetime.now()
sql = "INSERT INTO participant (email, name, token, created_at) VALUES (%s, %s, %s, %s);"
data = [email, name, token, created_at]
db = Db()
try:
db.query(sql, data)
self._email = email
self._name = name
self._created_at = created_at
self._token = token
return True
except Exception as e:
print(e)
return False
def email(self):
return self._email
def token(self):
return self._token
def name(self):
return self._name
def created_at(self):
return self._created_at
def filters_made(self):
return self._filters_made
def get_by_email(self, email):
db = Db()
sql = "SELECT * FROM participant WHERE email = %s;"
data = [email]
participant = db.query(sql, data)
if participant is None:
return None
self._email = participant['email']
if participant['token'] is not None:
self._token = json.loads(participant['token'])
else:
self._token = None
self._filters_made = participant['filters_made']
self._name = participant['name']
self._created_at = participant['created_at']
return self
def json(self):
_dict = {'email': self.email(), 'name': self.name(), 'token': self.token(), 'filters_made': self.filters_made(), 'created_at': self.created_at().timestamp()}
return json.dumps(_dict)
def make_filter(self, domain, wait_time=1):
if self._email is None:
raise Exception('No user specified: use .get_by_email() or .create() first')
if self._token is None:
raise Exception("User's Oauth2 token is None")
google = OAuth2Session(client_id, token=self.token())
if self.token()['expires_at'] < time()+10:
google = self.refresh_token()
if google == 'refresh_error':
return 'refresh_error'
headers = {"Content-Type": "application/json"}
params = {
"criteria": {
"from": domain
},
"action": {
"removeLabelIds": ["SPAM"],
"addLabelIds": ["CATEGORY_PERSONAL"]
}
}
#print(domain)
r = google.post("https://www.googleapis.com/gmail/v1/users/me/settings/filters", data=json.dumps(params), headers=headers)
if r.status_code == 200:
filter_id = r.json()['id']
#print(filter_id)
db = Db()
sql = "INSERT INTO filter (filter_id, sender, participant, filter_made, created_at) VALUES (%s, %s, %s, %s, %s);"
data = [filter_id, domain, self.email(), True, datetime.now()]
db.query(sql, data, True)
return True
elif r.status_code == 429:
if wait_time <= 8:
sleep(wait_time)
return self.make_filter(wait_time*2)
else:
print(r.status_code, r.text)
return False
else:
if wait_time <= 1:
wait_time = 2
return self.make_filter(domain, wait_time)
else:
sleep(1)
print(r.text, r.status_code)
return False
def make_filters(self):
db = Db()
sql = "SELECT * FROM sender;"
result = db.query(sql)
for i, row in enumerate(result):
self.make_filter(row['domain'])
self._filters_made = True
return self.set_filters_made(True)
def set_filters_made(self, state):
db = Db()
sql = "UPDATE participant SET filters_made = %s WHERE email = %s"
data = [state, self.email()]
db.query(sql, data, True)
return True
def refresh_token(self):
google = OAuth2Session(client_id, token=self.token())
extra = {
'client_id': client_id,
'client_secret': client_secret,
}
if 'refresh_token' in self.token().keys():
try:
self.set_token(google.refresh_token(refresh_url, **extra))
print('token updated!')
return google
except Exception as e:
print('Error: ', e)
return 'refresh_error'
else:
return 'refresh_error'
def user_info(self, wait_time=1):
google = OAuth2Session(client_id, token=self.token())
if self.token()['expires_at'] < time() + 10:
google = self.refresh_token()
if google == 'refresh_error':
return 'refresh_error'
r = google.get('https://www.googleapis.com/oauth2/v1/userinfo')
if r.status_code == 200:
data = r.json()
if self._name != data['name']:
db = Db()
sql = 'UPDATE participant SET name = %s WHERE email = %s;'
params = [data['name'], self._email]
db.query(sql, params)
self._name = data['name']
if self._email != data['email']:
db = Db()
sql = 'UPDATE participant SET name = %s WHERE email = %s;'
params = [data['email'], self._email]
db.query(sql, params)
self._email = data['email']
return r.json()
elif r.status_code == 429:
if wait_time <= 8:
sleep(wait_time)
return self.user_info(wait_time*2)
else:
print(r.status_code, r.text)
return False
else:
print(r.status_code, r.text)
return False
def set_token(self, token):
if self._email is None:
raise Exception('Anonymous user, set email first')
db = Db()
sql = 'UPDATE participant set token = %s where email = %s;'
data = [json.dumps(token), self.email()]
db.query(sql, data)
self._token = token
return self
def _get_filter(self, filter_id, wait_time=1):
google = OAuth2Session(client_id, token=self.token())
if self.token()['expires_at'] < time()+10:
google = self.refresh_token()
if google == 'refresh_error':
return 'refresh_error'
url = "https://www.googleapis.com/gmail/v1/users/me/settings/filters/{}".format(filter_id)
r = google.get(url)
if str(r.status_code)[0] == '2':
return True
elif r.status_code == 429:
if wait_time <= 8:
sleep(wait_time)
return self._get_filter(wait_time*2)
else:
print(r.status_code, r.text)
return False
else:
print('Filter not found in user account')
self._reset_filter()
print(r.status_code, r.text)
return False
def list_filters(self):
db = Db()
sql = "select * from filter where participant = %s;"
data = [self.email()]
result = db.query(sql, data)
tmp_list = []
for row in result:
tmp_row = {}
for k, v in row.items():
tmp_row[k] = v
tmp_list.append(tmp_row)
return tmp_list
def delete_filter(self, filter_id, wait_time=1):
google = OAuth2Session(client_id, token=self.token())
if self.token()['expires_at'] < time()+10:
google = self.refresh_token()
if google == 'refresh_error':
return 'refresh_error'
url = "https://www.googleapis.com/gmail/v1/users/me/settings/filters/{}".format(filter_id)
r = google.delete(url)
if str(r.status_code)[0] == '2':
db = Db()
sql = 'DELETE FROM filter WHERE filter_id = %s;'
data = [filter_id]
db.query(sql, data, True)
return True
elif r.status_code == 429:
if wait_time <= 8:
sleep(wait_time)
return self.delete_filter(filter_id, wait_time*2)
else:
print(r.status_code, r.text)
return False
else:
if wait_time <= 1:
sleep(1)
wait_time = 2
return self.delete_filter(filter_id, wait_time)
else:
print(r.status_code, r.text)
return False
def delete_filters(self):
#if self.filters_made != True:
# raise Exception("Filters have not been made yet")
filters = self.list_filters()
for f in filters:
self.delete_filter(f['filter_id'])
self._filters_made = False
return self.set_filters_made(False)
if __name__=='__main__':
pass
u = User()
# get by email
#u.get_by_email('graydenshand@gmail.com')
#print(u.token())
# create
#u.create('graydenshand+test@gmail.com', 'Grayden Shand')
#print(u)
# to json --> from json
#u.get_by_email('graydenshand@gmail.com')
#string = u.json()
#p = User(string)
#print(p)
# user_info
#u.get_by_email('graydenshand@gmail.com')
#print(u.user_info())
# set_token
#u.get_by_email('graydenshand@gmail.com')
#u.set_token(token)
#u.get_by_email('graydenshand@gmail.com')
#print(u.token())
# make_filter
#u.get_by_email('graydenshand@gmail.com')
#u.make_filter()
#print(u.json())
u.get_by_email('graydenshand@gmail.com')
q = Queue(connection=conn)
#print(u.json())
#print(u.list_filters())
from queue_functions import *
#q.enqueue(make_filters, u.json())
q.enqueue(delete_filters, u.json())
# delete all filters
"""
db = Db()
sql = "select email from participant;"
result = db.query(sql)
for i, row in enumerate(result):
u = User().get_by_email(row['email'])
print(u.json())
u.delete_filter()
"""