-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
148 lines (124 loc) · 7.09 KB
/
Main.py
File metadata and controls
148 lines (124 loc) · 7.09 KB
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
from Notion import NotionAPI
from Gcal import GCalAPI
from datetime import datetime, timedelta, date
import numpy as np
import os
import json
def bring_new_events_to_notion(notion, gcal_entries, notion_entries):
# find events that have been newly created in gcal and not added to notion yet
new_gcal_events = np.setdiff1d(gcal_entries['gcal_ids'], notion_entries['gcal_ids'], assume_unique=True)
idx_new_gcal_events = [gcal_entries['gcal_ids'].index(item) for item in new_gcal_events]
# bring new events from gcal to notion
for idx in idx_new_gcal_events:
name = gcal_entries['names'][idx]
start_date = gcal_entries['start_dates'][idx]
end_date = gcal_entries['end_dates'][idx]
duration = gcal_entries['durations'][idx]
gcal_id = gcal_entries['gcal_ids'][idx]
calendar = gcal_entries['calendars'][idx]
notion.add_entry(name=name, start_date=start_date, end_date=end_date, duration=duration, gcal_id=gcal_id, category=calendar)
print("Added '{}' to Notion.".format(name))
def update_events_in_notion(notion, gcal_entries, notion_entries):
# update events in notion that have been updated in gcal
# events that have been modified in gcal since last update
common_ids = set(gcal_entries['gcal_ids']).intersection(notion_entries['gcal_ids'])
idx_common_ids_notion = [notion_entries['gcal_ids'].index(item) for item in common_ids]
idx_common_ids_gcal = [gcal_entries['gcal_ids'].index(item) for item in common_ids]
idx_modified_gcal_events = []
for notion_idx, gcal_idx in zip(idx_common_ids_notion, idx_common_ids_gcal):
if gcal_entries['last_updated'][gcal_idx] > notion_entries['last_updated'][notion_idx]:
idx_modified_gcal_events.append([gcal_idx, notion_idx])
for idx in idx_modified_gcal_events:
gcal_idx, notion_idx = idx
name = gcal_entries['names'][gcal_idx]
start_date = gcal_entries['start_dates'][gcal_idx]
end_date = gcal_entries['end_dates'][gcal_idx]
duration = gcal_entries['durations'][gcal_idx]
gcal_id = gcal_entries['gcal_ids'][gcal_idx]
calendar = gcal_entries['calendars'][gcal_idx]
page_id = notion_entries['notion_ids'][notion_idx]
notion.update_entry(page_id=page_id, name=name, start_date=start_date, end_date=end_date, duration=duration, gcal_id=gcal_id, category=calendar)
print("Updated '{}' in Notion.".format(name))
def bring_new_events_to_gcal(gcal_config, notion, gcal, notion_entries):
# events that have been newly created in notion and not added to gcal yet
idx_new_notion_events = np.where(np.array(notion_entries['gcal_ids']) == None)[0]
# bring new events from notion to gcal
calendar_names = gcal_config['Calendars'].keys()
for idx in idx_new_notion_events:
name = notion_entries['names'][idx]
calendar = notion_entries['categories'][idx]
start_date = notion_entries['start_dates'][idx]
end_date = notion_entries['end_dates'][idx]
duration = notion_entries['durations'][idx]
if calendar not in calendar_names:
calendar = gcal_config['Default Calendar']
if not end_date:
if (start_date.hour == 0) and (start_date.minute == 0): # no time specified
start_date += timedelta(hours=gcal_config['Default Event Start'])
print("Start time of '{}' set to {}".format(name, start_date))
if not duration:
duration = gcal_config['Default Event Length']
end_date = start_date + timedelta(hours=duration)
page_id = notion_entries['notion_ids'][idx]
gcal_id = gcal.add_entry(calendar=calendar, name=name, start_date=start_date, end_date=end_date, description=None, source=None)
duration = (end_date - start_date).seconds / 3600
notion.update_entry(page_id=page_id, gcal_id=gcal_id, category=calendar, duration=duration, start_date=start_date, end_date=end_date)
print("Added '{}' to GCal.".format(name))
def update_events_in_gcal(gcal_config, notion, gcal, notion_entries):
# events that have been modified in notion since last update
idx_modified_notion_events = np.where(np.array(notion_entries['needs_update']))[0]
# update events in gcal that have been updated in notion
calendar_names = gcal_config['Calendars'].keys()
for idx in idx_modified_notion_events:
name = notion_entries['names'][idx]
calendar = notion_entries['categories'][idx]
start_date = notion_entries['start_dates'][idx]
end_date = notion_entries['end_dates'][idx]
duration = notion_entries['durations'][idx]
gcal_id = notion_entries['gcal_ids'][idx]
page_id = notion_entries['notion_ids'][idx]
if calendar not in calendar_names:
calendar = gcal_config['Default Calendar']
if not end_date:
if (start_date.hour == 0) and (start_date.minute == 0): # no time specified
start_date += timedelta(hours=gcal_config['Default Event Start'])
print("Start time of '{}' set to {}".format(name, start_date))
if not duration:
duration = gcal_config['Default Event Length']
end_date = start_date + timedelta(hours=duration)
gcal.update_entry(calendar=calendar, gcal_id=gcal_id, name=name, start_date=start_date, end_date=end_date, description=None, source=None)
notion.update_entry(page_id=page_id)
print("Updated '{}' in GCal.".format(name))
def run_sync(notion, todoist, gcal):
# notion_config = json.loads(os.environ['notion_config'])
gcal_config = json.loads(os.environ['gcal_config'])
# get all entries from today to next month
today = date.today()
time_min = today - timedelta(seconds=1)
time_max = today + timedelta(days=30)
notion_entries = notion.query(time_min, time_max)
if os.environ['SYNC_TODOIST'] == "True":
todoist_entries = todoist.get_tasks()
# loop through updated todoist tasks:
# if id in notion db:
# update notion entry
# else:
# make new notion entry
# bring all new notion tasks to todoist
idx_new_notion_events = np.where(np.array(notion_entries['todoist_ids']) == None)[0] # and category == "tasks"
# update changed tasks in todoist
idx_modified_notion_events = np.where(np.array(notion_entries['needs_update']))[0] # and category == "tasks"
if os.environ['SYNC_GCAL'] == "True":
print('{} start notion/gcal sync'.format(datetime.now()))
# # set up APIs
# gcal = GCalAPI(timezone, gcal_config)
# notion = NotionAPI(timezone, notion_config)
gcal_entries = gcal.query(time_min, time_max)
# sync events
bring_new_events_to_notion(notion, gcal_entries, notion_entries)
update_events_in_notion(notion, gcal_entries, notion_entries)
bring_new_events_to_gcal(gcal_config, notion, gcal, notion_entries)
update_events_in_gcal(gcal_config, notion, gcal, notion_entries)
print('{} finished notion/gcal sync'.format(datetime.now()))
if __name__ == '__main__':
run_sync()