-
Notifications
You must be signed in to change notification settings - Fork 0
/
importToGTasks.py
166 lines (143 loc) · 5.85 KB
/
importToGTasks.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
from setup import *
from helperFunctions import *
#######################################################
### PART 2: IMPORT NEW NOTION TASKS TO GOOGLE TASKS ###
#######################################################
print("Adding new Notion tasks to Google Tasks...\n")
# Get all of your Notion tasks that have not been synced with Google Tasks
query = {
'database_id': NOTION_DATABASE_ID,
'filter': {
'and': [
{
'property': NOTION_SYNCED,
'checkbox': {
'equals': False
}
},
{
'property': NOTION_DELETED,
'checkbox': {
'equals': False
}
}
]
}
}
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}}]})
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 = []
#notionTaskStatuses = []
gTasksStatuses = []
notionTaskDates = []
notionDescriptions = []
gTasksLists = []
notionLists = []
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:
# notionTaskStatuses.append(page['properties'][NOTION_STATUS]['status']['name'])
#except:
# notionTaskStatuses.append('')
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:
gTasksLists.append(LIST_DICTIONARY[page['properties'][NOTION_LIST_NAME]['select']['name']])
except:
gTasksLists.append(DEFAULT_LIST_ID)
try:
notionLists.append(page['properties'][NOTION_LIST_NAME]['select']['name'])
except:
notionLists.append('')
# Create the Google Task
newGoogleTask = makeGoogleTask(notionTaskNames[i], notionDescriptions[i], notionTaskDates[i], gTasksLists[i], gTasksStatuses[i])
# Update the necessary fields in Notion with the new Google Task info
notionTaskUpdate = notion.pages.update(
**{
'page_id': page['id'],
'properties': {
NOTION_GTASKS_TASK_ID: {
'rich_text': [{
'text': {
'content': newGoogleTask['id']
}
}]
},
NOTION_GTASKS_LIST_ID: {
'rich_text': [{
'text': {
'content': gTasksLists[i]
}
}]
},
NOTION_SYNCED: {
'checkbox': True
},
NOTION_LAST_SYNCED: {
'date': {
'start': addTimeZoneForNotion(nowToDateTimeString()),
'end': None
}
}
}
}
)
# Update the Notion 'List' field with the default list if it wasn't assigned
if notionLists[i] == '':
notionTaskUpdate = notion.pages.update(
**{
'page_id': page['id'],
'properties': {
NOTION_LIST_NAME: {
'select': {
"name": DEFAULT_LIST_NAME
}
}
}
}
)
print("Finished adding new Notion tasks to Google Tasks!\n")
else:
print("No new Notion tasks to add to Google Tasks\n")