-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_calendar.py
319 lines (264 loc) · 11.3 KB
/
notion_calendar.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from flask import Flask, Response, request
from flask_httpauth import HTTPTokenAuth
from ics import Calendar, Event
from retry import retry
from werkzeug.exceptions import Unauthorized
import arrow
import cachetools.func
import json
import logging
import os
import requests
import typing
from ics.grammar.parse import ContentLine
from ics.serializers.icalendar_serializer import CalendarSerializer
from ics.serializers.event_serializer import EventSerializer
from dateutil import tz
app = Flask(__name__)
auth = HTTPTokenAuth(scheme='Bearer')
app.config['NOTION_API_KEY'] = os.environ['NOTION_API_KEY']
app.config['NOTION_API_VERSION'] = '2022-06-28'
app.config['TOKENS'] = json.loads(os.environ['TOKENS'])
ljubljana = tz.gettz('Europe/Ljubljana')
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
waitress_logger = logging.getLogger('waitress')
waitress_logger.setLevel(logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class CustomCalendarSerializer(CalendarSerializer):
"""
Serialize a calendar with some additional properties
"""
def serialize_2url(calendar, container):
if hasattr(calendar, 'url') and calendar.url:
container.append(ContentLine("URL", value=""))
def serialize_3name(calendar, container):
if hasattr(calendar, 'name') and calendar.name:
container.append(ContentLine("NAME", value=calendar.name))
def serialize_3description(calendar, container):
if hasattr(calendar, 'description') and calendar.description:
container.append(ContentLine("DESCRIPTION", value=calendar.description))
def serialize_3timezone_id(calendar, container):
container.append(ContentLine("TIMEZONE-ID", value="Europe/Ljubljana"))
def serialize_3color(calendar, container):
container.append(ContentLine("COLOR", value="136:115:80"))
class CustomEventSerializer(EventSerializer):
"""
Serialize event with additional properties
"""
def serialize_color(event, container):
if hasattr(event, 'color') and event.color:
container.append(ContentLine("COLOR", value=event.color))
@auth.verify_token
def verify_token(token: str) -> str:
"""
Verify that the bearer token sent is in the list of our tokens (and return the related username
"""
if token in app.config['TOKENS']:
return app.config['TOKENS'][token]
def validate_token(token: typing.Optional[str]):
"""
Rudimentary implementation of bearer tokens over query string
"""
if token is None or token == "":
raise Unauthorized()
res = verify_token(token)
if res is None or res == "":
raise Unauthorized()
@retry(Exception, tries=5, delay=1)
def get_page(url: str, page_count: int, body: typing.Optional[typing.Dict] = None, headers: typing.Optional[typing.Dict] = None) -> typing.Optional[typing.Dict]:
logger.info(f'Fetching {url}, page {page_count}')
query_response = requests.post(url, json=body, headers=headers)
query_response.raise_for_status()
return query_response.json()
@retry(Exception, tries=5, delay=0)
@cachetools.func.ttl_cache(maxsize=128, ttl=600)
def get_events(notion_database_id: str) -> typing.Dict:
"""
Fetch all data and metadata from a Notion database. Paginate if multiple pages exist.
Cache the result for 10 minutes
:param notion_database_id: The ID of a Notion database. I.e. the part after your workspace. Specifically, Notion
URLs will look something like this: https://www.notion.so/<workspace>/<database_id>
:return: A dictionary containing data ("results") and metadata ("info")
"""
notion_api_key = app.config['NOTION_API_KEY']
notion_api_version = app.config['NOTION_API_VERSION']
database_info = f'https://api.notion.com/v1/databases/{notion_database_id}'
database_query = f'https://api.notion.com/v1/databases/{notion_database_id}/query'
headers = {
'Authorization': f'Bearer {notion_api_key}',
'Notion-Version': f'{notion_api_version}',
}
params = {}
pages_and_databases = []
logger.info(f'Fetching {database_info}')
info_response = requests.get(database_info, json=params, headers=headers)
info_response.raise_for_status()
page_count = 1
while True:
search_response_obj = get_page(database_query, page_count, params, headers)
pages_and_databases.extend(search_response_obj.get("results"))
if not search_response_obj.get("has_more"):
break
page_count += 1
params["start_cursor"] = search_response_obj.get("next_cursor")
return {
'info': info_response.json(),
'results': pages_and_databases
}
@cachetools.func.ttl_cache(maxsize=128, ttl=60)
def get_calendar(notion_database_id: str) -> str:
"""
Fetch a database from Notion and create a ICS-compatible calendar as string.
:param notion_database_id: The Notion database ID
:return: The ICS file
"""
events = get_events(notion_database_id)
logger.info(f'Parsing results for {notion_database_id}...')
info = events["info"]
results = events["results"]
c = Calendar()
c.Meta.serializer = CustomCalendarSerializer
c.name = info["title"][0]["plain_text"]
if info["description"] is not None and len(info["description"]) > 0:
calendar.description = info["description"][0]["plain_text"].strip()
for obj in results:
# Skip archived and trashed objects
# Fetch a few properties into variables
properties = obj["properties"]
title_prop = properties[""]
location_prop = properties["Location"]
type_prop = properties["Type"]
tags_prop = properties["Tags"]
date_prop = properties["Date"]
page_prop = properties["Page"]
try:
if title_prop is None:
continue
except IndexError:
continue
if len(title_prop["title"]) == 0:
continue
title_0 = title_prop["title"][0]
if obj["archived"] or obj["in_trash"]:
continue
if date_prop["date"] is None:
continue
if date_prop["date"]["start"] is None:
continue
try:
if date_prop is None:
continue
except IndexError:
continue
try:
if location_prop is None:
pass
except IndexError:
location_prop = None
e = Event()
e.Meta.serializer = CustomEventSerializer
if type_prop["select"] is not None and type_prop["select"]["name"] is not None:
e.name = "[" + type_prop["select"]["name"].strip() + "] "
e.categories = [
type_prop["select"]["name"].strip()
]
if type_prop["select"]["color"]:
e.color = type_prop["select"]["color"]
else:
e.name = ""
e.categories = []
if obj["icon"] is not None and "emoji" in obj["icon"] and obj["icon"]["emoji"] is not None:
e.name = e.name + obj["icon"]["emoji"] + " "
e.name = e.name + title_0["plain_text"].strip()
for tag in tags_prop["multi_select"]:
e.categories = e.categories + [tag["name"]]
e.uid = obj["id"]
e.url = obj["url"]
if date_prop["date"] is not None:
is_all_day = False
if date_prop["date"]["start"] is not None:
try:
if len(date_prop["date"]["start"]) == 10:
is_all_day = True
e.begin = arrow.get(date_prop["date"]["start"]).floor('day')
else:
e.begin = arrow.get(date_prop["date"]["start"], tzinfo=ljubljana)
except arrow.ParserError:
e.begin = arrow.get(date_prop["date"]["start"])
if is_all_day:
e.make_all_day()
if date_prop["date"]["end"] is not None:
# End days, by iCal definition are EXCLUSIVE. So if the event is a whole day event,
# we must define it as ending one day later.
if len(date_prop["date"]["end"]) == 10:
e.end = arrow.get(date_prop["date"]["end"]).floor('day').shift(days=+1)
else:
try:
e.end = arrow.get(date_prop["date"]["end"], tzinfo=ljubljana)
except arrow.ParserError:
e.end = arrow.get(date_prop["date"]["end"])
else:
if is_all_day:
e.end = e.begin.shift(days=+1)
else:
e.end = e.begin
try:
if page_prop is not None and page_prop["url"] is not None:
e.description = page_prop["url"] + '\r\n'
else:
e.description = ""
except TypeError:
e.description = ""
if properties["Status"] and properties["Status"]["status"] and properties["Status"]["status"]["name"]:
status = properties["Status"]["status"]["name"]
if status == "Not going":
e.status = "CANCELLED"
elif status == "Confirmed":
e.status = "CONFIRMED"
elif status == "Need more info":
e.status = "TENTATIVE"
# TENTATIVE, CONFIRMED, CANCELLED
# e.status =
if obj["created_time"] is not None:
e.created = arrow.get(obj["created_time"])
if obj["last_edited_time"] is not None:
e.last_modified = arrow.get(obj["last_edited_time"])
if location_prop is not None and len(location_prop["rich_text"]) > 0:
e.location = location_prop["rich_text"][0]["plain_text"].strip()
c.events.add(e)
logger.info(f'Done for {notion_database_id}...')
result = ''.join(c.serialize_iter())
return result
# notion_view_id = vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# https://www.notion.so/<workspace>/f0c46e3a76cc4536ae84b0e1ffa58a72?v=761088f2880549f79b458d19e5333d54
# notion_database_id = ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@app.route("/calendar/bearer/<notion_database_id>")
@auth.login_required
def calendar(notion_database_id: str):
"""
Get a calendar, authenticating via Bearer token
:param notion_database_id: The ID of a Notion database, as retrieved from Notion URL. Ensure that the integration
service has access to this database.
:return: The ICS calendar file
"""
return Response(get_calendar(notion_database_id), mimetype='text/calendar')
@app.route("/calendar/qs/<notion_database_id>")
def calendar_qs(notion_database_id: str):
"""
Get the calendar, authenticating via token in URL
:param notion_database_id: The ID of a Notion database, as retrieved from Notion URL. Ensure that the integration
service has access to this database.
:return: The ICS calendar file
"""
validate_token(request.args.get('token'))
return Response(get_calendar(notion_database_id), mimetype='text/calendar')
def create_app() -> Flask:
"""
Called from waitress server to start the app
:return: Flask app. Nothing special here.
"""
return app
# Starts flask in development mode if calling via "python <name>"
if __name__ == '__main__':
app.run(debug=True, port=8080, host="0.0.0.0")