-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (54 loc) · 1.99 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
from dotenv import load_dotenv
from googleapiclient.discovery import build
from src.calendar_credentials_obtainer import CalendarCredentialsObtainer
from src.calendar_item_retriever import CalendarItemRetriever
from src.notion_item_retriever import NotionItemRetriever
import os
import json
class Encoder(json.JSONEncoder):
def default(self, o):
if type(o) is bytes:
return bytes.decode(o)
return o.__dict__
def main():
credentials = CalendarCredentialsObtainer().get_credentials()
calendar_id = os.getenv('CALENDAR_ID')
token = os.getenv('NOTION_TOKEN')
database_id = os.getenv('NOTION_DATABASE_ID')
calendar_items = CalendarItemRetriever(credentials, calendar_id).retrieve_items()
notion_items = NotionItemRetriever(token, database_id).get_notion_items()
service = build('calendar', 'v3', credentials=credentials)
def callback_s(id, res, exc):
print(
json.dumps({
'id': id,
'res': res,
'exc': exc
}, indent = 2, cls=Encoder)
)
br = service.new_batch_http_request(callback=callback_s)
for item in notion_items:
found = False
for event in calendar_items:
if event.id == item.get_id():
found = True
matches = event == item
if not matches:
sr = service.events().update(
calendarId=calendar_id,
eventId=event.id,
body=item.to_google_calendar_item()
)
br.add(sr, request_id=event.id)
if not found:
sr = service.events().insert(
calendarId=calendar_id,
body=item.to_google_calendar_item()
)
br.add(sr, request_id=item.id)
br.execute()
def obj_to_dict(item):
return item.__dict__
if __name__ == '__main__':
load_dotenv()
main()