-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
325 lines (278 loc) · 12.8 KB
/
main.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
import datetime
from googleapiclient.discovery import build
import gkeepapi
import gKeep
import gCal
import config
import traceback
from copy import deepcopy
import googleapiclient.discovery
import firebase_admin
from firebase_admin import credentials, firestore
from google.cloud.firestore_v1beta1 import ArrayRemove, ArrayUnion
from concurrent.futures import wait, ALL_COMPLETED
import logging
from db import addDaysToString, days_in_month, getMYD, fill, sortEntries, entries_to_array
import re;
re._pattern_type = re.Pattern
cred = credentials.Certificate("key.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
class ClientService:
def __init__(self, calendarCredentials, keepCredentials, keepi):
try:
# First setup calendar
cal = googleapiclient.discovery.build('calendar', 'v3', credentials=calendarCredentials)
# TODO: what if a thread accesses a variable at the same time? == atomic
# Fetch clients timezone
self.TIMEZONE = cal.settings().get(setting='timezone').execute()['value']
# Loop through calendars to see if one is already called "Spaced"
# Otherwise create it
calID = None # the ID of the calendar we will use
page_token = None
while True:
calendar_list = cal.calendarList().list(pageToken=page_token).execute()
for calendar_list_entry in calendar_list['items']:
print(calendar_list_entry['summary'])
if calendar_list_entry['summary'] == config.calendar['summary']:
calID = calendar_list_entry['id']
break
page_token = calendar_list.get('nextPageToken')
if not page_token:
break
if not calID:
created_calendar = cal.calendars().insert(body=config.calendar).execute()
calID = created_calendar['id']
# Then setup keep
doc_ref = db.collection('users').document(keepCredentials['username'])
doc_ref.set({
'name': keepCredentials['username'],
}, merge=True)
print("Keep created")
self.calID = calID
self.cal = cal
self.keep = keepi
self.userName = keepCredentials['username']
except Exception as e:
print(e)
def createEvent(self, title, desc, dateCreated):
# logging.basicConfig(filename='myapp.log', level=logging.INFO)
try:
print("Create event?", title, desc, dateCreated)
doc_ref = db.collection('users').document(self.userName)
doc = doc_ref.get().to_dict()
currentMonth = datetime.datetime.now().month
print("todict")
# print(doc['tasks'])
if not 'tasks' in doc:
print("in if")
doc_ref.set({
'name': self.userName,
'currMonth': currentMonth,
'tasks': True,
'0': [],
'1': [],
'2': [],
'3': [],
'4': [],
'5': [],
'6': [],
'7': [],
'8': [],
'9': [],
'10': [],
'11': [],
'12': []
}, merge=True)
print("created firebase")
# monthOffset = 0
print("beforebatch")
fireBatch = db.batch()
calBatch = self.cal.new_batch_http_request()
print("afterbach")
for r in gCal.spacings:
time = dateCreated + datetime.timedelta(days=r)
time = time.strftime('%Y-%m-%d')
print("working????")
month = int(time.split("-")[1])
print("after split")
# monthOffset = int(time.split("-")[1]) - datetime.datetime.now().month
# print(monthOffset)
timers = time
time += "T00:00:00-00:00"
event = gCal.eventSchema(title, desc, time, self.TIMEZONE)
print("a")
calBatch.add(self.cal.events().insert(calendarId=self.calID, body=event))
print(event.get('htmlLink'))
print("b")
month = str(month)
fireBatch.update(doc_ref, {
month: ArrayUnion([{
'title': title,
'desc': desc,
'date': timers,
}])
})
print("c")
print("a1")
fireBatch.commit()
calBatch.execute()
print("a2")
except Exception as e:
print(e)
def space_out(self):
try:
currDate = datetime.datetime.today()
InitPadding = 2 # do not space out the first n - 2 ( -2, because needs to look at neighbours) days.
doc_ref = db.collection("users").document(self.userName)
data = doc_ref.get().to_dict()
currMonth = int(data['currMonth'])
fireBatch = db.batch()
for month in range(currMonth, 13):
print(month)
thisMonthsData = data[str(month)]
print(thisMonthsData)
if thisMonthsData:
sortEntries(thisMonthsData)
oldMonth = deepcopy(thisMonthsData)
doNothing = 0
# while doNothing < len(thisMonthsData)-1:
entryCount = 0
for entry in thisMonthsData:
day = getMYD(entry["date"], "day")
pInArray = day - 1
print("looking at day:", day)
if currDate + datetime.timedelta(days=InitPadding) < datetime.datetime.strptime(entry["date"],
"%Y-%m-%d") \
and "changed" not in entry and day >= 2 and day <= days_in_month(month) - 2:
arr = entries_to_array(thisMonthsData)
print(" ----------------"
"BEGIN with: ", arr, "CURRENTLY ON DAY", day, "----------------")
if arr[pInArray] != 1:
neighbours = [arr[pInArray - 2], arr[pInArray - 1], arr[pInArray + 1], arr[pInArray + 2]]
index = 0
best = neighbours[0]
count = 1
for j in range(1, len(neighbours)):
if neighbours[j] + j / 4 < best:
index = j
best = neighbours[j] + j / 4
count += 1
# print (index)
# print( neighbours)
print("Days neighbours", neighbours, arr[pInArray], index)
if neighbours[index] + 1 >= arr[pInArray]:
# only move if it will actually make an overall difference
doNothing += 1
else:
if index <= 1:
index -= 2
else:
index -= 1
print("Changing date by ", index, " days")
newDate = addDaysToString(entry["date"], index)
if getMYD(newDate, "month") == month:
print("Before change: " + entry['date'])
entry["date"] = newDate
print("After change: ", entry['date'])
# the problem is that sees a 0 and subtracts one day from date leading to 31st
# thisMonthsData[entryCount]["date"] = entry["date"] # not necce
# thisMonthsData[entryCount]["changed"] = True
else:
print("OH AWSDHFJZSDJAFAJDFSJASDFJASDJFJASDFJA")
else:
doNothing+=1
else:
if "changed" in entry:
del entry["changed"]
doNothing += 1
entryCount += 1
sortEntries(thisMonthsData)
fireBatch.update(doc_ref, {
str(month): thisMonthsData
})
data[str(month)] = thisMonthsData
neww = entries_to_array(thisMonthsData)
print("OLDDDDDDD", entries_to_array(oldMonth), "EMND DOLDDLDLdd", )
print("NEWWWWWWW", neww, len(neww))
fireBatch.commit()
print("calling refresh (Ring ring)")
# print(data)
self.refreshCalendar(data)
print("dunnas")
except Exception as e:
print(e)
def refreshCalendar(self, data):
try:
print("refreshing calendar")
# doc_ref = db.collection("users").document(self.userName)
self.cal.calendars().delete(calendarId=self.calID).execute()
# data = doc_ref.get().to_dict()
calBatch = self.cal.new_batch_http_request()
created_calendar = self.cal.calendars().insert(body=config.calendar).execute()
self.calID = created_calendar['id']
count = 0
for i in range(1, 13):
print("motnh:", i)
# print(data)
for entry in data[str(i)]:
try:
count += 1
time = entry["date"] + "T00:00:00-00:00"
event = gCal.eventSchema(entry["title"], entry["desc"], time, self.TIMEZONE)
calBatch.add(self.cal.events().insert(calendarId=self.calID, body=event))
if count >= 499:
calBatch.execute()
count = 0
except Exception as e:
print(e)
if count > 0:
calBatch.execute()
except Exception as e:
print(e)
# def removeOld(self):
# if()
#
# def addFromDB(self):
def threadWork(obj):
try:
obj.keep.sync()
gnote = obj.keep.find(labels=[obj.keep.findLabel('spaced')], archived=False, trashed=False)
# print(gnote)
if gnote:
# config.threadPool.submit(work, obj, gnote)
# print("Runnin work")
# note technically this will not work if two equal google keep accounts are used
empty = True
for item in gnote:
if empty is True:
empty = False
config.threadPool.submit(gKeep.addToList, obj.keep, item)
config.threadPool.submit(obj.createEvent, item.title, item.text, item.timestamps.created)
config.threadPool.submit(item.delete)
print("deleted?")
if not empty:
obj.keep.sync()
# print("gunnin")
# print("done")
except Exception as e:
x = 0
# print(e, traceback.format_exc())
def startLoop():
while 1:
try:
arrayOfFutures = []
# print ("Ya", config.clientServices)
for obj in config.clientServices:
if config.clientServices[obj].keep:
# print("Ya")
arrayOfFutures.append(config.threadPool.submit(threadWork, config.clientServices[obj]))
wait(arrayOfFutures, timeout=None, return_when=ALL_COMPLETED)
except Exception as e:
# x=0
print(e)
# TODO for tomorrow: keep doesnt seem to be added to the right dates? desc and title from website is alwauys null.
# fix 1 was to sort before using data. use textfield instead of auto complete
# TODO: we got a lot done yesterday: fixed spaceout (although may need some more tweaking: error was called that means sometimes it goes into the next month, and at the last moment it suddenly added like a 100 things)
# todo: We implemented enternew - and it seems like it should work without too much hassle
# todo: after these fixes, only things left are design/content and bugs, oh and limit following tasks to 5