-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncDeletions.py
165 lines (143 loc) · 6.28 KB
/
syncDeletions.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
from setup import *
from helperFunctions import *
##################################
### PART 1: SYNC DELETED TASKS ###
##################################
print("Updating tasks that were deleted...\n")
# Delete Google Tasks where the 'Deleted' checkbox is checked in Notion
query = {
'database_id': NOTION_DATABASE_ID,
'filter': {
'and':[
{
'property': NOTION_GTASKS_TASK_ID,
'rich_text': {
'is_not_empty': True
}
},
{
'property': NOTION_SYNCED,
'checkbox': {
'equals': True
}
},
{
'property': NOTION_DELETED,
'checkbox': {
'equals': True
}
},
{
'property': NOTION_NEEDS_GTASKS_UPDATE,
'checkbox': {
'equals': True
}
}
]
}
}
maxDate = addTimeZoneForNotion(dateTimeToString(datetime.now() + timedelta(weeks=FUTURE_WEEKS_TO_SYNC)))
minDate = addTimeZoneForNotion(dateTimeToString(datetime.now() - timedelta(weeks=PAST_WEEKS_TO_SYNC)))
if PAST_WEEKS_TO_SYNC >=0 and FUTURE_WEEKS_TO_SYNC >= 0:
query['filter']['and'].append({'or': [{'property': NOTION_DATE, 'date': {'on_or_after': minDate}}, { 'property': NOTION_DATE, 'date': {'is_empty': True}}]})
query['filter']['and'].append({'or': [{'property': NOTION_DATE, 'date': {'on_or_before': maxDate}}, { 'property': NOTION_DATE, 'date': {'is_empty': True}}]})
elif PAST_WEEKS_TO_SYNC >=0:
query['filter']['and'].append({'or': [{'property': NOTION_DATE, 'date': {'on_or_after': minDate}}, { 'property': NOTION_DATE, 'date': {'is_empty': True}}]})
elif FUTURE_WEEKS_TO_SYNC >= 0:
query['filter']['and'].append({'or': [{'property': NOTION_DATE, 'date': {'on_or_before': maxDate}}, { 'property': NOTION_DATE, 'date': {'is_empty': True}}]})
deletedNotionPages = notion.databases.query(
**query
)
nextCursor = deletedNotionPages['next_cursor']
while deletedNotionPages['has_more']:
query['start_cursor'] = nextCursor
moreDeletedNotionPages = notion.databases.query(
**query
)
nextCursor = moreDeletedNotionPages['next_cursor']
deletedNotionPages['results'] += moreDeletedNotionPages['results']
if nextCursor is None:
break
for i, deletedPage in enumerate(deletedNotionPages['results']):
gTasksListId = makeOneLinePlainText(deletedPage['properties'][NOTION_GTASKS_LIST_ID]['rich_text'])
gTasksId = makeOneLinePlainText(deletedPage['properties'][NOTION_GTASKS_TASK_ID]['rich_text'])
deletedPageName = deletedPage['properties'][NOTION_TASK_NAME]['title'][0]['plain_text']
try:
print(f'Deleting this task from Google Tasks: {deletedPageName}\n')
service.tasks().delete(tasklist=gTasksListId, task=gTasksId).execute()
except:
print(f'Could not delete this task from Google Tasks: {deletedPageName}\n')
notionPageUpdate = notion.pages.update(
**{
'page_id': deletedPage['id'],
'properties': {
NOTION_LAST_SYNCED: {
'date': {
'start': addTimeZoneForNotion(nowToDateTimeString()),
'end': None
}
}
}
}
)
# Check off the 'Deleted' checkbox for tasks that were deleted in Google Tasks
deletedGTasks = []
for tasklist in LIST_DICTIONARY.keys(): # Only get Google Tasks info for lists of interest
maxDate = addTimeZoneForNotion(dateTimeToString(datetime.now() + timedelta(weeks=FUTURE_WEEKS_TO_SYNC)))
minDate = addTimeZoneForNotion(dateTimeToString(datetime.now() - timedelta(weeks=PAST_WEEKS_TO_SYNC)))
if PAST_WEEKS_TO_SYNC >= 0 and FUTURE_WEEKS_TO_SYNC >= 0:
gTasksResults = service.tasks().list(tasklist=LIST_DICTIONARY[tasklist], maxResults=500, showHidden=True, showDeleted=True, dueMax=maxDate, dueMin=minDate).execute()
elif PAST_WEEKS_TO_SYNC >= 0:
gTasksResults = service.tasks().list(tasklist=LIST_DICTIONARY[tasklist], maxResults=500, showHidden=True, showDeleted=True, dueMin=minDate).execute()
elif FUTURE_WEEKS_TO_SYNC >= 0:
gTasksResults = service.tasks().list(tasklist=LIST_DICTIONARY[tasklist], maxResults=500, showHidden=True, showDeleted=True, dueMax=maxDate).execute()
else:
gTasksResults = service.tasks().list(tasklist=LIST_DICTIONARY[tasklist], maxResults=500, showHidden=True, showDeleted=True).execute()
for item in gTasksResults['items']:
try: # Only deleted Google Tasks have the 'deleted' key, so the try & except block is necessary to catch key errors on tasks that aren't deleted
if item['deleted'] == True:
deletedGTasks.append(item)
except:
continue
for deletedGTask in deletedGTasks:
deletedGTaskId = deletedGTask['id']
notionPagesToDelete = notion.databases.query(
**{
'database_id': NOTION_DATABASE_ID,
'filter': {
'and': [
{
'property': NOTION_GTASKS_TASK_ID,
'rich_text': {
'equals': deletedGTaskId
}
},
{
'property': NOTION_DELETED,
'checkbox': {
'equals': False
}
}
]
}
}
)
for notionPageToDelete in notionPagesToDelete['results']:
print(f'Deleting this task in Notion: {deletedGTaskId}\n')
notionPageUpdate = notion.pages.update(
**{
'page_id': notionPageToDelete['id'],
'properties': {
NOTION_DELETED: {
'checkbox': True
},
NOTION_LAST_SYNCED: {
'date': {
'start': addTimeZoneForNotion(nowToDateTimeString()),
'end': None
}
}
}
}
)
print("Finished updating tasks that were deleted!\n")