-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.py
75 lines (72 loc) · 3.45 KB
/
manager.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
if __name__ == "__main__":
raise Exception("This not the main")
from notion import notion_database
from notion.query import RetrieveList, RetrieveDatabaseList, RetrieveLatestCursor
class manager:
def __init__(self,notion,exchange_rate_getter) -> None:
self.notion = notion
self.exchange_rate_getter = exchange_rate_getter
self.cursor = None
self.databases = dict[str,notion_database]()
def FullUpdate(self):
# Reset databases
self.databases = dict[str,notion_database]()
# Collect all database
database_results = RetrieveDatabaseList(self.notion)
if database_results is None:
return
for dbindex,result in enumerate(database_results): # Loop through each database to update values
print("database:",dbindex+1,"/",len(database_results))
database = notion_database(self.notion,result.get("id"),self.exchange_rate_getter)
if database.success:
self.databases[result.get("id")] = database
database.UpdateAllPages()
self.cursor = RetrieveLatestCursor(self.notion)
def Update(self) -> bool:
# Collect updated databases
results = RetrieveList(self.notion, descending=False ,cursor=self.cursor,page_size=5)
if results is None:
return False
# Update cursor
self.cursor = results[-1].get("id")
page_list = list() # for pages, process after db updated
updated_db = list() # skip updated db to prevent recursive running update on same item
for index,result in enumerate(results):
pageid = result.get("id")
if result.get("object") == "database" :
if self.databases.get(pageid) is None:
print("Items:",index+1,"/",len(results)) # print only if it is processed
database = notion_database(self.notion,result.get("id"),self.exchange_rate_getter)
if database.success:
self.databases[pageid] = database
database.UpdateAllPages()
updated_db.append(pageid)
self.cursor = pageid
else:
if self.databases[pageid].PullPropertyStruct() and self.databases[pageid].PropertyUpdate():
print("Items:",index+1,"/",len(results)) # print only if it is processed
self.databases[pageid].UpdateAllPages()
updated_db.append(pageid)
self.cursor = pageid
else:
dbid = result.get("parent").get("database_id")
if dbid is not None:
page_list.append((dbid,result.get("properties"),pageid))
updated = False
for index,(dbid,prop,pageid) in enumerate(page_list):
if dbid in updated_db:
continue
db = self.databases.get(dbid)
if db is not None:
result = db.UpdatePageWithResult(prop,pageid)
if result is None:
continue
if not result:
db.PullPropertyStruct()
db.PropertyUpdate()
db.UpdateAllPages()
else:
self.cursor = pageid
updated = True
print("Pages:",index+1,"/",len(page_list)) # print only if it is processed
return updated