-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
343 lines (273 loc) · 10.1 KB
/
tasks.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
import gflags
import urllib
import httplib2
import time
import datetime
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from rfc3339 import rfc3339
import pywintypes
import win32com.client
USING_PROXY = False
if win32com.client.gencache.is_readonly == True:
#allow gencache to create the cached wrapper objects
win32com.client.gencache.is_readonly = False
# under p2exe the call in gencache to __init__() does not happen
# so we use Rebuild() to force the creation of the gen_py folder
win32com.client.gencache.Rebuild()
# NB You must ensure that the python...\win32com.client.gen_py dir does not exist
# to allow creation of the cache in %temp%
PROXY_TYPE_HTTP = 3
PROXY_HOST = 'www-proxy.ericsson.se'
PROXY_PORT = 8080
GOOGLE_LIST_NAME = "Ericsson"
FLAGS = gflags.FLAGS
toOutlook = {'title' : 'Subject', 'notes' : 'Body', 'status' : 'Complete', 'id' : "EntryID"}
# 'due' : 'DueDate', 'updated' : 'LastModificationTime', 'completed' : 'DateCompleted'
toGoogle = dict ((v,k) for k,v in toOutlook.items())
# Important
# importantKeys = [ "Subject", "Complete", "Body", "EntryID", "LastModificationTime"]
# Commented so body isn't read - this removes need for confirmation in Outlook
importantKeys = [ "Subject", "Complete", "EntryID", "LastModificationTime"]
# "ReminderTime", "CreationTime", "StartDate", "DueDate", "DateCompleted", "LastModificationTime",
def toDateTime(value):
if value.year == 4501:
# Combination of pywin being old and Outlook COM being stupid
# returns year 4501 if there is no due date
# (ie latest possible date acc'd to outlook)
# Fix this to the max date rfc3339 will take?
value = rfc3339(datetime.datetime(2011,9,8,17,37,0))
# value.year = 3000
return [key,value]
value = rfc3339(datetime.datetime(
year=value.year,
month=value.month,
day=value.day,
hour=value.hour,
minute=value.minute,
second=value.second
))
return value
def toOutlookKey(item):
key,value = item
if key == "status":
if value == "completed":
value = True
else:
value = False
if key not in toGoogle:
key = toOutlook[key]
return [key,value]
def toGoogleKey(item):
key,value = item
if key not in toOutlook:
key = toGoogle[key]
if key == "status":
if not value or value == "needsAction":
value = 'needsAction'
else:
value = 'completed'
timeFields = {'updated','completed','due'}
if key in timeFields:
value = toDateTime(value)
return [key,value]
class task(dict):
def __init__(self, dic=None, obj=None, google=False, outlook=False):
dict.__init__(self)
self.google = google
self.outlook = outlook
if dic is not None:
for key,value in dic:
self[key] = value
elif obj is not None:
for key in obj._prop_map_get_:
if key in importantKeys:
self[key] = getattr(obj,key)
def __getitem__(self,key):
try:
return dict.__getitem__(self,key)
except KeyError:
try:
return dict.__getitem__(self,toGoogle[key])
except KeyError:
return dict.__getitem__(self,toOutlook[key])
def __contains__(self,key):
if dict.__contains__(self,key):
return True
else:
try:
if self[key] != None:
return True
else:
return False
except KeyError:
return False
def convertToGoogle(self):
# print self
res = task (google=True)
for item in self.items():
try:
key,value = toGoogleKey(item)
res[key] = value
except KeyError:
next
return res
def convertToOutlook(self):
res = task (outlook=True)
for item in self.items():
try:
key,value = toOutlookKey(item)
res[key] = value
except KeyError:
next
return res
def convert(self):
if self.outlook:
return self.convertToGoogle()
elif self.google:
return self.convertToOutlook()
raise TypeError
def updatedUTC(self):
offset = datetime.datetime.now() - datetime.datetime.utcnow()
try:
return datetime.datetime.strptime(str(self['LastModificationTime']),"%m/%d/%y %H:%M:%S") - offset
except KeyError:
return datetime.datetime.strptime(self['updated'],"%Y-%m-%dT%H:%M:%S.%fZ")
def completed(self):
if self['status'] == "needsAction":
return False
elif self['status'] == "completed":
return True
else:
return self['status']
class outlook():
def __init__(self):
print "Connecting to outlook.",
self.tasks = []
self.outlook = win32com.client.gencache.EnsureDispatch("Outlook.Application")
print ".",
# outlook = win32com.client.Dispatch("Outlook.Application")
self.ns = self.outlook.GetNamespace("MAPI")
print ".",
ofTasks = self.ns.GetDefaultFolder(win32com.client.constants.olFolderTasks)
print ".",
for taskno in range(len(ofTasks.Items)):
# print "Processing task ",taskno
otask = ofTasks.Items.Item(taskno+1)
if otask.Class == win32com.client.constants.olTask:
newtask = task(obj=otask,outlook=True)
self.tasks.append(newtask)
def modify(self, task, taskid):
updatetask = self.ns.GetItemFromID(taskid)
for key in updatetask._prop_map_get_:
if not key == "EntryID":
if key in task:
setattr(updatetask,key,task[key])
updatetask.Save()
def add(self, gtask):
newtask = self.outlook.CreateItem(win32com.client.constants.olTaskItem)
for key,value in gtask.items():
# Set values for this new task, ensure EntryID isn't set
if not key == "EntryID":
setattr(newtask,key,value)
newtask.Save()
# Now convert this task into a dict format used elsewhere.
otask = task(obj=newtask,outlook=True)
return otask
def getTasks(self):
return self.tasks
class google():
def __init__(self, list_name):
print "Connecting to Google",
# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(
client_id='45198696978.apps.googleusercontent.com',
client_secret='PXAHwAr3i9vh13ckf2M89Zve',
scope='https://www.googleapis.com/auth/tasks',
user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')
print ".",
# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False
# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('tasks.dat')
credentials = storage.get()
print ".",
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
print ".",
# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
proxies = urllib.getproxies()
print ".",
# if len(proxies) > 0:
if USING_PROXY:
# proxy_type, proxy_url = proxies.items()[0]
# proxy_protocol, proxy_url = proxy_url.split('://')
# proxy_url, proxy_port = proxy_url.split(':')
# proxy_port = int(proxy_port)
#temp until urllib works...
proxy_type = PROXY_TYPE_HTTP
proxy_url = PROXY_HOST
proxy_port = PROXY_PORT
http = httplib2.Http(proxy_info = httplib2.ProxyInfo(proxy_type, proxy_url, proxy_port),disable_ssl_certificate_validation=True)
# http = httplib2.Http(proxy_info = httplib2.ProxyInfo(proxy_type, proxy_url, proxy_port))
else:
http = httplib2.Http(disable_ssl_certificate_validation=True)
http = credentials.authorize(http)
print ".",
# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
self.service = build(serviceName='tasks', version='v1', http=http, developerKey='45198696978.apps.googleusercontent.com')
print ".",
self.update()
print ".",
# Find the outlook task list on google
for tasklist in self.tasklists['items']:
print ".",
if list_name == tasklist['title'] :
self.listid = tasklist['id']
break
# If the outlook task list doesn't exist on google then create it
else:
tasklist = { 'title': list_name }
result = self.service.tasklists().insert(body=tasklist).execute()
self.listid = result['id']
print ".",
self.update()
print " done"
def modify(self,gtask,taskid):
gtask['id'] = taskid
result = self.service.tasks().update(tasklist = self.listid, body=gtask, task=taskid).execute()
modtask = task(dic=result.items(),google=True)
return modtask
def update(self):
self.tasklists = self.service.tasklists().list().execute()
def add(self,gtask):
# Delete the id before adding
del gtask['id']
result = self.service.tasks().insert(tasklist = self.listid, body=gtask).execute()
# Construct task container for result
newtask = task(dic=result.items(),google=True)
return newtask
def getTasks(self):
print "Getting tasks.",
results = self.service.tasks().list(tasklist = self.listid ).execute()
print ".",
gtasks = []
if 'items' in results:
for result in results['items']:
gtask = task(dic=result.items(),google=True)
gtasks.append(gtask)
print "done."
return gtasks