-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateGTasks.py
163 lines (143 loc) · 5.69 KB
/
updateGTasks.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
from setup import *
from helperFunctions import *
###############################################################
### PART 4: UPDATE GOOGLE TASKS THAT WERE CHANGED IN NOTION ###
###############################################################
print("Updating Google Tasks that were changed in Notion...\n")
# Get all of your Notion tasks that need to be updated in Google Tasks
query = {
'database_id': NOTION_DATABASE_ID,
'filter': {
"and": [
{
'property': NOTION_NEEDS_GTASKS_UPDATE,
'checkbox': {
'equals': True
}
},
{
'property': NOTION_SYNCED,
'checkbox': {
'equals': True
}
},
{
'property': NOTION_DELETED,
'checkbox': {
'equals': False
}
}
]
}
}
notionPages = notion.databases.query(
**query
)
nextCursor = notionPages['next_cursor']
while notionPages['has_more']:
query['start_cursor'] = nextCursor
moreNotionPages = notion.databases.query(
**query
)
nextCursor = moreNotionPages['next_cursor']
notionPages['results'] += moreNotionPages['results']
if nextCursor is None:
break
notionPagesResults = notionPages['results']
notionTaskNames = []
gTasksStatuses = []
notionTaskDates = []
notionDescriptions = []
gTasksIds = [makeOneLinePlainText(page['properties'][NOTION_GTASKS_TASK_ID]['rich_text']) for page in notionPagesResults]
notionLists = []
currentGTasksLists = [makeOneLinePlainText(page['properties'][NOTION_GTASKS_LIST_ID]['rich_text']) for page in notionPagesResults]
newGTasksLists = []
if len(notionPagesResults) > 0:
for i, page in enumerate(notionPagesResults):
try:
notionTaskNames.append(page['properties'][NOTION_TASK_NAME]['title'][0]['plain_text'])
except:
notionTaskNames.append('No Title')
try:
status = page['properties'][NOTION_STATUS]['status']['name']
if status in NOTION_TO_DO_STATUSES or status in NOTION_IN_PROGRESS_STATUSES:
gTasksStatuses.append(GOOGLE_TO_DO_STATUS)
elif status in NOTION_COMPLETE_STATUSES:
gTasksStatuses.append(GOOGLE_DONE_STATUS)
except:
gTasksStatuses.append(GOOGLE_TO_DO_STATUS)
try:
notionTaskDates.append(parseDateTimeString(page['properties'][NOTION_DATE]['date']['start'][:-6], '%Y-%m-%dT%H:%M:%S.000'))
except:
try:
notionTaskDates.append(parseDateString(page['properties'][NOTION_DATE]['date']['start'], '%Y-%m-%d'))
except:
notionTaskDates.append('')
try:
notionDescriptions.append(makeDescription(page['properties'][NOTION_DESCRIPTION]['rich_text']))
except:
notionDescriptions.append('')
try:
newGTasksLists.append(LIST_DICTIONARY[page['properties'][NOTION_LIST_NAME]['select']['name']])
except:
newGTasksLists.append(DEFAULT_LIST_ID)
try:
notionLists.append(page['properties'][NOTION_LIST_NAME]['select']['name'])
except:
notionLists.append('')
# Update the Google Calendar event
updatedGoogleTask = updateGoogleTask(notionTaskNames[i], notionDescriptions[i], notionTaskDates[i], currentGTasksLists[i], newGTasksLists[i], gTasksIds[i], gTasksStatuses[i])
# Update the necessary fields in Notion with the updated Google Calendar event info
notionTaskUpdate = notion.pages.update(
**{
'page_id': page['id'],
'properties': {
NOTION_LAST_SYNCED: {
'date': {
'start': addTimeZoneForNotion(nowToDateTimeString()),
'end': None
}
},
}
}
)
# List changed to empty, so set it to the list associated with the 'GTasks List ID' field
if notionLists[i] == '':
notionTaskUpdate = notion.pages.update(
**{
'page_id': page['id'],
'properties': {
NOTION_LIST_NAME: {
'select': {
"name": list(LIST_DICTIONARY.keys())[list(LIST_DICTIONARY.values()).index(currentGTasksLists[i])]
}
}
}
}
)
# List changed (but not to an empty value), so update the 'GTasks List ID' field
elif newGTasksLists[i] != currentGTasksLists[i]:
notionTaskUpdate = notion.pages.update(
**{
'page_id': page['id'],
'properties': {
NOTION_GTASKS_LIST_ID: {
'rich_text': [{
'text': {
'content': LIST_DICTIONARY[notionTaskUpdate['properties'][NOTION_LIST_NAME]['select']['name']]
}
}]
},
NOTION_GTASKS_TASK_ID: {
'rich_text': [{
'text': {
'content': updatedGoogleTask['id']
}
}]
}
}
}
)
print("Finished updating Google Tasks that were changed in Notion!\n")
else:
print("No Google Tasks to update\n")