-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (102 loc) · 4.26 KB
/
main.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
import optparse
import json
from json2notion.console import print_error,print_status
from json2notion.children import Children
from json2notion.properties import Properties
from json2notion.page import Page
from json2notion.database import Database
from json2notion.parse import Parser
from json2notion.notion_api import GetToken
CHILDREN = Children()
PROPERTY = Properties()
PARSER = Parser()
GETTOKEN = GetToken()
DATABASE = Database(integrations_token=GETTOKEN.token)
PAGE = Page(integrations_token=GETTOKEN.token)
def main():
parser = optparse.OptionParser()
PARSER.get_option(parser=parser)
datas = json_reader()
page_ids = retrive_page_id(datas[0][1][1])
for index,(fields_key,data_extract)in enumerate(datas):
column_type = read_database(fields_key)
if page_ids:
update_database(column_type,fields_key,data_extract,page_ids[index])
elif not page_ids:
write_database(column_type,fields_key,data_extract)
def json_reader():
datas = []
try:
json_file = open(PARSER.options.json_input)
json_dict = json.load(json_file)
except FileNotFoundError:
print_error(f"The '{PARSER.options.json_input}' file cannot be found ")
exit(1)
try:
for i in range(len(json_dict["entries"])):
fields_key=list(json_dict["entries"][i]["fields"])
fields_data=list(map(lambda l:json_dict["entries"][i]["fields"][l],json_dict["entries"][0]["fields"]))
body_data=[json_dict["entries"][i]["body"]["type"],json_dict["entries"][i]["body"]["content"]]
data_extract=[json_dict["entries"][i]["_id"],json_dict["entries"][i]["type"], fields_data, body_data]
datas.append([fields_key,data_extract])
json_file.close()
except KeyError:
print_error("The json file is badly formatted")
exit(1)
return datas
def read_database(fields_key):
DATABASE.retrive_column_type(database_id=PARSER.options.notion_db_id,fields_key=fields_key)
return DATABASE.type_list
def retrive_page_id(content_type):
page_ids = []
DATABASE.query_database_filter(database_id=PARSER.options.notion_db_id, column_name="Type", content=content_type)
for result in DATABASE.result["results"]:
page_ids.append(result["id"])
return page_ids
def write_database(column_type,fields_key,data_extract):
set_property('title','id',data_extract[0])
set_property('select','Type',data_extract[1])
for type, key, data in zip(column_type,fields_key,data_extract[2]):
set_property(type,key,data)
set_children(data_extract[3][0],data_extract[3][1])
PAGE.create_page(database_id=PARSER.options.notion_db_id, properties=PROPERTY,children=CHILDREN)
print_status("Success","Data added to the database ") #Not yet reliable
PROPERTY.clear()
CHILDREN.clear()
def update_database(column_type,fields_key,data_extract,page_id):
set_property('title','id',data_extract[0])
set_property('select','Type',data_extract[1])
for type, key, data in zip(column_type,fields_key,data_extract[2]):
set_property(type,key,data)
set_children(data_extract[3][0],data_extract[3][1])
PAGE.update_page(page_id=page_id,properties=PROPERTY,children=CHILDREN)
print_status("Success","Updated database") #Not yet reliable
PROPERTY.clear()
CHILDREN.clear()
def set_property(type,key,data):
match type:
case 'title':
PROPERTY.set_title(key, data)
case 'rich_text':
PROPERTY.set_rich_text(key, data)
case 'number':
PROPERTY.set_number(key, data)
case 'select':
PROPERTY.set_select(key, data)
case 'multi_select':
PROPERTY.set_multi_select(key, data) #Not sure if it works, this option requires an input table
case 'checkbox':
PROPERTY.set_checkbox(key, data)
case 'url':
PROPERTY.set_url(key, data)
case 'email':
PROPERTY.set_email(key, data)
case 'phone_number':
PROPERTY.set_phone_number(key, data)
def set_children(type,data):
match type:
case 'embed':
CHILDREN.set_embed(data)
#To be completed according to needs
if __name__ == '__main__':
main()