-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_notion_cache.py
99 lines (90 loc) · 3.45 KB
/
update_notion_cache.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
import os
import json
import time
import requests
from dotenv import load_dotenv
from pathlib import Path
addon_dir = os.path.dirname(os.path.realpath(__file__))
env_path = os.path.join(addon_dir, '.env')
load_dotenv(env_path)
SUBJECT_DATABASE_ID = os.getenv('DATABASE_ID')
PHARMACOLOGY_DATABASE_ID = os.getenv('PHARMACOLOGY_DATABASE_ID')
ETG_DATABASE_ID = os.getenv('ETG_DATABASE_ID')
ROTATION_DATABASE_ID = os.getenv('ROTATION_DATABASE_ID')
class NotionCache:
def __init__(self, addon_dir: str):
self.cache_dir = Path(addon_dir) / "cache"
self.cache_dir.mkdir(exist_ok=True)
def update_cache(self, database_id: str):
"""Fetch and save all pages from a Notion database to the cache"""
self.clear_cache(database_id)
headers = {
"Authorization": f"Bearer {os.getenv('NOTION_TOKEN')}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
pages = []
has_more = True
start_cursor = None
while has_more:
payload = {
"filter": {
"property": "For Search",
"checkbox": {
"equals": True
}
},
"page_size": 100
}
if start_cursor:
payload["start_cursor"] = start_cursor
try:
response = requests.post(
f"https://api.notion.com/v1/databases/{database_id}/query",
headers=headers,
json=payload
)
response.raise_for_status()
data = response.json()
pages.extend(data['results'])
has_more = data.get('has_more', False)
start_cursor = data.get('next_cursor')
except Exception as e:
print(f"Error fetching from Notion: {e}")
break
self.save_to_cache(database_id, pages)
print(f"Saved {len(pages)} pages to the cache for database {database_id}")
def clear_cache(self, database_id: str):
"""Clear the cache for a specific database"""
cache_path = self.get_cache_path(database_id)
if cache_path.exists():
os.remove(cache_path)
print(f"Cleared cache for database {database_id}")
def save_to_cache(self, database_id: str, pages: list):
"""Save pages to cache file"""
cache_path = self.get_cache_path(database_id)
try:
with cache_path.open('w', encoding='utf-8') as f:
json.dump({
'version': 1,
'timestamp': time.time(),
'pages': pages
}, f)
except Exception as e:
print(f"Error saving cache: {e}")
def get_cache_path(self, database_id: str) -> Path:
"""Get the path for a specific database's cache file"""
return self.cache_dir / f"{database_id}.json"
def update_notion_cache():
"""Update the Notion database cache"""
notion_cache = NotionCache(addon_dir)
if SUBJECT_DATABASE_ID:
notion_cache.update_cache(SUBJECT_DATABASE_ID)
if PHARMACOLOGY_DATABASE_ID:
notion_cache.update_cache(PHARMACOLOGY_DATABASE_ID)
if ETG_DATABASE_ID:
notion_cache.update_cache(ETG_DATABASE_ID)
if ROTATION_DATABASE_ID:
notion_cache.update_cache(ROTATION_DATABASE_ID)
if __name__ == "__main__":
update_notion_cache()