-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraph.py
675 lines (542 loc) · 18.3 KB
/
graph.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
from __future__ import annotations
import asyncio
import dataclasses
import json
import logging
import math
import os
import tempfile
from collections import defaultdict
from collections.abc import AsyncGenerator
from collections.abc import Collection
from collections.abc import Generator
from contextlib import asynccontextmanager
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import datetime
from datetime import timezone
from logging import getLogger
from pathlib import Path
from threading import Thread
from typing import Any
from typing import Literal
from typing import NoReturn
from uuid import UUID
from uuid import uuid4
from aiohttp import ClientSession
from aiolimiter import AsyncLimiter
from dateutil.parser import parse as parse_date
from dotenv import load_dotenv
from flask import Flask
from flask import jsonify
from flask import render_template
from werkzeug import Response
logger = getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
NOTION_URL = 'https://www.notion.so'
NOTION_API_URL = 'https://api.notion.com/v1'
DEFAULT_DATA_PATH = Path(__file__).parent / 'data'
DEFAULT_REFRESH_INTERVAL = 60 * 5
DEFAULT_N_WORKERS = 4
# api config
RATE_LIMIT_BURST = 1
RATE_LIMIT = 3 * RATE_LIMIT_BURST
RATE_LIMITER = AsyncLimiter(RATE_LIMIT, RATE_LIMIT_BURST)
TIMEOUT = 30
MAX_RETRY = 1
SKIP_PROPAGATION_BLOCK_TYPES = (
'child_page',
'child_database',
)
@dataclass(frozen=True)
class Config:
notion_key: str
data_path: Path
refresh_interval: int
n_workers: int
def load_config() -> Config:
load_dotenv()
try:
notion_key = os.environ['NOTION_KEY']
except KeyError:
raise ValueError('Missing NOTION_KEY environment variable')
data_path = Path(os.environ.get('GRAPH_DATA_PATH', DEFAULT_DATA_PATH))
refresh_interval = int(
os.environ.get('GRAPH_REFRESH_INTERVAL', DEFAULT_REFRESH_INTERVAL)
)
n_workers = int(os.environ.get('GRAPH_N_WORKERS', DEFAULT_N_WORKERS))
return Config(notion_key, data_path, refresh_interval, n_workers)
@dataclass
class Page:
id: UUID
url: str
title: str
last_parsed: datetime
def __hash__(self) -> int:
return hash(self.id)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Page):
return NotImplemented
return self.id == other.id
def serialize_page(page: Page) -> dict[str, str]:
return {
'id': str(page.id),
'url': str(page.url),
'title': str(page.title),
'last_parsed': page.last_parsed.isoformat(),
}
def deserialize_page(data: dict[str, str]) -> Page:
return Page(
id=UUID(data['id']),
url=data['url'],
title=data['title'],
last_parsed=datetime.fromisoformat(data['last_parsed']),
)
@dataclass
class Link:
id: UUID
source: UUID
target: UUID
link_type: Literal['page', 'database', 'mention', 'href']
def __hash__(self) -> int:
return hash(self.id)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Link):
return NotImplemented
return self.id == other.id
def serialize_link(link: Link) -> dict[str, str]:
return {
'id': str(link.id),
'source': str(link.source),
'target': str(link.target),
'link_type': link.link_type,
}
def deserialize_link(data: dict[str, str]) -> Link:
return Link(
id=UUID(data['id']),
source=UUID(data['source']),
target=UUID(data['target']),
link_type=data['link_type'], # type: ignore
)
class Graph:
_pages: dict[UUID, Page]
_links: dict[UUID, Link]
def __init__(
self,
pages: Collection[Page] | None = None,
links: Collection[Link] | None = None,
) -> None:
self._pages = {}
self._links = {}
if pages is None:
pages = []
if links is None:
links = []
for page in pages:
self.add(page)
for link in links:
self.add(link)
def __contains__(self, item: Page | UUID) -> bool:
if isinstance(item, Page):
return item.id in self._pages
elif isinstance(item, UUID):
return item in self._pages
else:
raise TypeError(f'Cannot check for item of type {type(item)}')
@classmethod
def deserialize(cls, data: str) -> Graph:
dct = json.loads(data)
try:
pages = [deserialize_page(page) for page in dct['pages']]
links = [deserialize_link(link) for link in dct['links']]
except Exception as e:
raise ValueError(f'Invalid data {e}')
return cls(pages, links)
def serialize(self) -> str:
pages = [serialize_page(page) for page in self._pages.values()]
links = [serialize_link(link) for link in self._links.values()]
return json.dumps({'pages': pages, 'links': links})
def save(self, path: str | os.PathLike) -> None:
with open(path, 'w') as f:
f.write(self.serialize())
@classmethod
def load(cls, path: str | os.PathLike) -> Graph:
with open(path) as f:
return cls.deserialize(f.read())
@property
def pages(self) -> list[Page]:
return list(self._pages.values())
@property
def links(self) -> list[Link]:
return list(self._links.values())
def add(self, item: Page | Link) -> None:
if isinstance(item, Page):
if item.id in self._pages:
raise ValueError(f'Page {item.id} already exists')
self._pages[item.id] = item
elif isinstance(item, Link):
if item.id in self._links:
raise ValueError(f'Link {item.id} already exists')
self._links[item.id] = item
else:
raise TypeError(f'Cannot add item of type {type(item)}')
def prune(self) -> Graph:
links = set()
for link in self._links.values():
if link.source in self and link.target in self:
links.add(link)
return self.__class__(list(self._pages.values()), links)
def update(self, pages: Collection[Page], links: Collection[Link]) -> None:
new_pages = {page.id: page for page in pages}
delete_ids = []
for link in self._links.values():
if link.source in new_pages:
delete_ids.append(link.id)
for id_ in delete_ids:
del self._links[id_]
self._pages.update(new_pages)
self._links.update({link.id: link for link in links})
@dataclass
class DisplayLink:
source: str
target: str
rotation: float
curvature: float
@dataclass
class DisplayNode:
id: str
title: str
url: str
@dataclass
class DisplayGraph:
nodes: list[DisplayNode]
links: list[DisplayLink]
def to_display_graph(graph: Graph) -> DisplayGraph:
node_ids: set[str] = set()
nodes = []
for page in graph.pages:
node_ids.add(str(page.id))
node = DisplayNode(id=str(page.id), title=page.title, url=page.url)
nodes.append(node)
links_dict: dict[tuple[UUID, UUID], list[Link]] = defaultdict(list)
for link in graph.links:
if str(link.source) not in node_ids or str(link.target) not in node_ids:
continue
links_dict[(link.source, link.target)].append(link)
links_list = []
for ids_tp, links in links_dict.items():
if ids_tp[0] == ids_tp[1]:
base_curvature = 0.5
else:
base_curvature = 0
n = len(links)
for i, link in enumerate(links):
rotation = 2 * math.pi * i / n
new_link = DisplayLink(
source=str(link.source),
target=str(link.target),
rotation=rotation,
curvature=base_curvature + min((n - 1) / 10, 0.5),
)
links_list.append(new_link)
return DisplayGraph(nodes=nodes, links=links_list)
@contextmanager
def persisted_graph(
path: str | Path, flush: bool = False, persist: bool = True
) -> Generator[Graph, None, None]:
if not flush:
try:
graph = Graph.load(path)
except Exception:
graph = Graph()
logger.warning(f'Could not load graph from {path}, creating new graph.')
else:
graph = Graph()
try:
yield graph
except Exception:
raise
else:
if persist:
fd, tmp_path = tempfile.mkstemp()
with open(fd, 'w') as f:
f.write(graph.serialize())
Path(path).parent.mkdir(parents=True, exist_ok=True)
os.replace(tmp_path, path)
@asynccontextmanager
async def RateLimitedSession(
config: Config,
auth: str | None = None,
) -> AsyncGenerator[ClientSession, None]:
auth = auth or config.notion_key
headers = {
'Authorization': f'Bearer {auth}',
'Notion-Version': '2022-02-22',
}
async with RATE_LIMITER:
async with ClientSession(headers=headers) as session:
yield session
async def paginated(
method: Literal['GET', 'POST'],
url: str,
config: Config,
initial_params: dict[str, Any] | None = None,
) -> list[dict]:
results = []
cursor: str | None = None
has_more = True
while has_more:
params = initial_params or {}
if cursor is not None:
params = params | {'start_cursor': cursor}
data = {}
async with RateLimitedSession(config=config) as session:
if method == 'GET':
async with session.get(url, params=params) as resp:
data = await resp.json()
elif method == 'POST':
async with session.post(url, json=params) as resp:
data = await resp.json()
results.extend(data['results'])
cursor = data.get('next_cursor')
has_more = data.get('has_more', False) and cursor is not None
return results
def _uuid_to_url(uuid: UUID) -> str:
return f"{NOTION_URL}/{str(uuid).replace('-', '')}"
def _strip_uuid(href: str) -> UUID:
if not href.startswith('/'):
raise ValueError
no_slash = href[1:]
try:
return UUID(no_slash.split('#')[0])
except ValueError:
pass
try:
return UUID(no_slash.split('?')[0])
except ValueError:
raise
def _parse_page(
page_data: dict[str, Any], last_parsed: dict[UUID, datetime]
) -> Page | None:
# skip archived pages
if page_data['archived']:
return None
# only parse page if it has been updated since last parse
page_id = UUID(page_data['id'])
if page_id in last_parsed:
last_edited = parse_date(page_data['last_edited_time'])
time = last_parsed[page_id]
if time.tzinfo is None:
time = time.replace(tzinfo=timezone.utc)
if last_edited < last_parsed[page_id]:
return None
properties = page_data.get('properties', {})
for value in properties.values():
if isinstance(value, dict) and value.get('type') == 'title':
title_rich_text = value.get('title', [])
break
else:
title_rich_text = []
title_rich_text = [rt for rt in title_rich_text if rt['type'] == 'text']
title = '-'.join([rt['text']['content'] for rt in title_rich_text])
return Page(
id=page_id,
url=_uuid_to_url(page_id),
title=title,
last_parsed=datetime.now(timezone.utc),
)
async def parse_pages(last_parsed: dict[UUID, datetime], config: Config) -> set[Page]:
param = {'filter': {'value': 'page', 'property': 'object'}}
logger.debug('getting page ids...')
resp = await paginated(
'POST', url=f'{NOTION_API_URL}/search', config=config, initial_params=param
)
ret = []
for data in resp:
page = _parse_page(page_data=data, last_parsed=last_parsed)
if page is None:
continue
ret.append(page)
return set(ret)
def _parse_rich_text(page: UUID, rich_text: dict[str, Any]) -> Link | None:
if rich_text['type'] == 'mention':
mention = rich_text['mention']
if mention['type'] == 'page':
return Link(
id=uuid4(),
source=page,
target=UUID(mention['page']['id']),
link_type='mention',
)
elif rich_text['type'] == 'text':
if rich_text.get('href') is not None:
try:
uuid = _strip_uuid(rich_text['href'])
except ValueError:
logger.debug(f"failed to parse href format: {rich_text['href']}")
return None
return Link(id=uuid4(), source=page, target=uuid, link_type='href')
return None
def parse_links(page: UUID, data: dict[str, Any]) -> list[Link]:
block_type = data['type']
if block_type in ('child_page', 'child_database'):
return []
if block_type not in data:
return []
ret: list[Link] = []
block = data.get(block_type, {})
for rich_text in block.get('rich_text', []):
try:
link = _parse_rich_text(page=page, rich_text=rich_text)
except KeyError:
pass
else:
if link is not None:
ret.append(link)
return ret
async def parse_children(
page: UUID, block: UUID, config: Config
) -> tuple[list[UUID], list[Link]]:
# logger.info(f"parsing children of {block} in {page}...")
resp = await paginated(
'GET', url=f'{NOTION_API_URL}/blocks/{block}/children', config=config
)
links: list[Link] = []
children: list[UUID] = []
for data in resp:
# handle child_pages separately
tp = data['type']
target = data['id']
if tp == 'child_page':
links.append(Link(id=uuid4(), source=page, target=target, link_type='page'))
if tp == 'child_database':
links.append(
Link(id=uuid4(), source=page, target=target, link_type='database')
)
# handle any other links such as mentions and hrefs
links.extend(parse_links(page=page, data=data))
# handle propagation to children
if tp in SKIP_PROPAGATION_BLOCK_TYPES:
continue
if data.get('has_children'):
children.append(UUID(target))
return children, links
@dataclass
class Task:
page: UUID
block: UUID
retry: int = 0
def __hash__(self) -> int:
return hash(self.block)
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Task):
return NotImplemented
return self.block == other.block
async def worker(
queue: asyncio.Queue[Task],
links: set[Link],
enqueued: set[Task],
done: set[Task],
failed: set[Task],
config: Config,
):
while True:
task = await queue.get()
try:
children, new_links = await asyncio.wait_for(
parse_children(page=task.page, block=task.block, config=config), TIMEOUT
)
except Exception:
if task.retry >= MAX_RETRY:
logger.debug(f'task failed: {task!r}')
failed.add(task)
else:
logger.debug(f'retrying task: {task!r}')
task.retry += 1
queue.put_nowait(task)
else:
async with asyncio.Lock():
for child in children:
# don't parse blocks twice
if child not in enqueued:
new_task = Task(page=task.page, block=child)
enqueued.add(new_task)
queue.put_nowait(new_task)
for link in new_links:
links.add(link)
done.add(task)
finally:
queue.task_done()
async def parse(
last_parsed: dict[UUID, datetime],
config: Config,
) -> tuple[set[Page], set[Link]]:
pages = await parse_pages(config=config, last_parsed=last_parsed)
links: set[Link] = set()
# monitor the queue
queue: asyncio.Queue[Task] = asyncio.Queue()
enqueued: set[Task] = set()
done: set[Task] = set()
failed: set[Task] = set()
workers = []
for _ in range(config.n_workers):
task = asyncio.create_task(
worker(queue, links, enqueued, done, failed, config=config)
)
workers.append(task)
for page in pages:
new_task = Task(page=page.id, block=page.id)
queue.put_nowait(new_task)
enqueued.add(new_task)
# wait for all tasks to be done
async def monitor() -> None:
while True:
await asyncio.sleep(1)
logger.debug(
f'ENQUEUED: {len(enqueued)}, DONE: {len(done)}, FAILED: {len(failed)}'
)
logger_task = asyncio.create_task(monitor())
await queue.join()
logger_task.cancel()
logger.debug('work done, cancelling workers...')
for w in workers:
w.cancel()
logger.info(f'done: {len(done)}, failed: {len(failed)}')
return pages, links
async def partial_parse(config: Config, flush: bool = False) -> None:
with persisted_graph(config.data_path / 'graph.json', flush=flush) as graph:
last_parsed = {page.id: page.last_parsed for page in graph.pages}
pages, links = await parse(last_parsed=last_parsed, config=config)
graph.update(pages, links)
async def run_daemon(config: Config) -> NoReturn:
while True:
try:
logger.info('refreshing graph...')
await partial_parse(config=config, flush=False)
await asyncio.sleep(config.refresh_interval)
except Exception:
logger.exception('error while parsing, retrying in 5s...')
await asyncio.sleep(5)
def flask_app(config: Config) -> Flask:
app = Flask(__name__)
def index() -> Any:
return render_template('index.html')
def data() -> Response:
with persisted_graph(config.data_path / 'graph.json') as graph:
display_graph = to_display_graph(graph)
return jsonify(dataclasses.asdict(display_graph))
app.add_url_rule('/', view_func=index)
app.add_url_rule('/data', view_func=data)
return app
def main() -> int:
config = load_config()
app = flask_app(config)
daemon = Thread(target=lambda: asyncio.run(run_daemon(config)))
flask = Thread(target=lambda: app.run(host='0.0.0.0', port=8080))
daemon.start()
flask.start()
daemon.join()
flask.join()
return 0
if __name__ == '__main__':
raise SystemExit(main())