This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
notion_ics.py
90 lines (69 loc) · 2.78 KB
/
notion_ics.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
import json
from datetime import datetime
from icalendar import Calendar, Event
from notion.client import NotionClient
from notion.collection import CalendarView
from notion.block import BasicBlock
from notion.user import User
# Hack some representation stuff into notion-py
BasicBlock.__repr__ = BasicBlock.__str__ = lambda self: self.title
User.__repr__ = User.__str__ = lambda self: self.given_name or self.family_name
def get_ical(client, calendar_url, title_format):
calendar = client.get_block(calendar_url)
for view in calendar.views:
if isinstance(view, CalendarView):
calendar_view = view
break
else:
raise Exception(f"Couldn't find a calendar view in the following list: {calendar.views}")
calendar_query = calendar_view.build_query()
calendar_entries = calendar_query.execute()
collection = calendar.collection
schema = collection.get_schema_properties()
properties_by_name = {}
properties_by_slug = {}
properties_by_id = {}
title_prop = None
for prop in schema:
name = prop['name']
if name in properties_by_name:
print("WARNING: duplicate property with name {}".format(name))
properties_by_name[name] = prop
properties_by_slug[prop['slug']] = prop
properties_by_id[prop['id']] = prop
if prop['type'] == 'title':
title_prop = prop
assert title_prop is not None, "Couldn't find a title property"
dateprop = properties_by_id[calendar_query.calendar_by]
#assert dateprop['type'] == 'date', "Property '{}' is not a Date property".format(settings['property'])
cal = Calendar()
cal.add("summary", "Imported from Notion, via notion-export-ics.")
cal.add('version', '2.0')
for e in calendar_entries:
date = e.get_property(dateprop['id'])
if date is None:
continue
name = e.get_property(title_prop['id'])
clean_props = {'NAME': name}
# Put in ICS file
event = Event()
desc = ''
event.add('dtstart', date.start)
if date.end is not None:
event.add('dtend', date.end)
desc += e.get_browseable_url() + '\n\n'
desc += 'Properties:\n'
for k, v in e.get_all_properties().items():
if k != dateprop['slug']:
name = properties_by_slug[k]['name']
desc += " - {}: {}\n".format(name, v)
clean_props[name] = v
title = title_format.format_map(clean_props)
event.add('summary', title)
event.add('description', desc)
cal.add_component(event)
# Print
#print("{}: {} -> {}".format(title, date.start, date.end))
#print(desc)
#print('--------------')
return cal