-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
62 lines (53 loc) · 2.36 KB
/
user.py
File metadata and controls
62 lines (53 loc) · 2.36 KB
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
from helpers import build as helpers_build
from helpers import flatmap
from remote import users
from oauth2client.client import OAuth2Credentials
import json, time, apiclient
class User():
def __init__(self, email):
self.user = users.find_one({'email': email})
def get(self, key, default=''):
return self.user.get(key, default)
def set(self, key, value):
self.update({key: value})
def update(self, d):
users.update({'_id': self.user['_id']}, {'$set': d})
self.user = users.find_one({'_id': self.get('_id')})
def get_credentials(self):
return OAuth2Credentials.from_json(json.dumps(self.get('credentials')))
def get_timezone(self):
timezone = self.get('timezone')
if not timezone:
calendar_service = self.build('calendar', v='v3')
default_calendar = calendar_service.calendars().get(calendarId='primary').execute()
self.set('timezone', default_calendar['timeZone'])
return timezone
def get_history_token(self):
history_token = self.get('history_token')
if not history_token:
gmail_service = self.build('gmail', v='v1')
threads = gmail_service.users().threads().list(userId='me', maxResults=1).execute()['threads']
history_token = threads[0]['historyId']
self.set('history_token', history_token)
return history_token
def get_new_messages(self):
gmail_service = self.build('gmail', v='v1')
history_token = self.get_history_token()
history = gmail_service.users().history().list(userId='me', startHistoryId=history_token).execute()
changes = history['history'] if 'history' in history else []
while 'nextPageToken' in history:
page_token = history['nextPageToken']
history = gmail_service.users().history().list(userId='me', startHistoryId=history_token, pageToken=page_token).execute()
changes.extend(history['history'])
messages = flatmap(lambda x: x['messages'], changes)
thread_ids = set([x['threadId'] for x in messages])
def get_thread(x):
try:
return gmail_service.users().threads().get(userId='me', id=x).execute()
except:
return None
threads = filter(lambda x: bool(x), map(get_thread , thread_ids))
full_messages = map(lambda x: x['messages'][0], threads)
return full_messages, history['historyId']
def build(self, service, **kwargs):
return helpers_build(service, self.get_credentials(), **kwargs)