Skip to content

Commit 0d6f775

Browse files
committed
v1 Basic Elementary function
1 parent c48783b commit 0d6f775

File tree

6 files changed

+315
-5
lines changed

6 files changed

+315
-5
lines changed

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NOTION_VERSION = "2022-02-22"

database.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from request_notion import Request
2+
3+
class Database:
4+
def __init__(self, integrations_token):
5+
"""
6+
init
7+
:param integrations_token: Notion Internal Integration Token
8+
"""
9+
self.properties_list = []
10+
self.url = 'https://api.notion.com/v1/databases'
11+
self.result = {}
12+
self.request = Request(self.url, integrations_token=integrations_token)
13+
14+
15+
def retrieve_database(self, database_id, fields_key):
16+
"""
17+
Retrieve a database
18+
:param database_id: Identifier for a Notion database
19+
:param get_properties: Get properties_list trigger
20+
:return:
21+
"""
22+
23+
self.result = self.request.call_api_get(self.url + "/" + database_id)
24+
25+
for field_key in fields_key:
26+
self.properties_list.append(self.result["properties"][field_key]["type"])
27+
28+
29+

main.py

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,93 @@
1+
from dataclasses import field
2+
import imp
3+
import requests
4+
import json
15
import os
26

3-
token = os.environ["NOTION_TOKEN_PUT"]
4-
BEARER_TOKEN = os.environ["NOTION_BEARER_TOKEN"]
7+
from properties import Properties
8+
from page import Page
9+
from database import Database
510

6-
main_page_id = 'fa2142e7178a4af980ef0bc2bcde14ec'
11+
12+
token = os.environ["NOTION_TOKEN_PUT_EXPERIMENT"]
13+
14+
database_id = 'd08f239718194d1f990c7ed7c67a6653'
715

816
headers = {
917
"Accept": "application/json",
10-
"Notion-Version": "2021-08-16",
18+
"Notion-Version": "2022-02-22",
1119
"Content-Type": "application/json",
1220
"Authorization": "Bearer " + token
1321
}
1422

23+
PROPERTY = Properties()
24+
1525
def main():
16-
print("main")
26+
27+
fields_key, data_extract = json_reader()
28+
29+
column_type = read_database(fields_key)
30+
31+
write_database(column_type,fields_key,data_extract)
32+
33+
def json_reader():
34+
35+
data_extract =[]
36+
37+
json_file = open('data.json')
38+
json_dict = json.load(json_file)
39+
40+
data_extract.append(json_dict["entries"][0]["_id"])
41+
42+
fields_key=list(json_dict["entries"][0]["fields"])
43+
fields_data=list(map(lambda l:json_dict["entries"][0]["fields"][l],json_dict["entries"][0]["fields"]))
44+
45+
data_extract.append(fields_data)
46+
data_extract.append(json_dict["entries"][0]["body"])
47+
48+
json_file.close()
49+
50+
return fields_key,data_extract
51+
52+
def read_database(fields_key):
53+
54+
D = Database(integrations_token=token)
55+
D.retrieve_database(database_id=database_id,fields_key=fields_key)
56+
57+
return D.properties_list
58+
59+
def write_database(column_type,fields_key,data_extract):
60+
61+
set_property('title','id',data_extract[0])
62+
63+
for type, key, data in zip(column_type,fields_key,data_extract[1]):
64+
set_property(type,key,data)
65+
66+
P = Page(integrations_token=token)
67+
P.create_page(database_id=database_id, properties=PROPERTY)
68+
69+
def set_property(type,key,data):
70+
71+
match type:
72+
case 'title':
73+
PROPERTY.set_title(key, data)
74+
case 'rich_text':
75+
PROPERTY.set_rich_text(key, data)
76+
case 'number':
77+
PROPERTY.set_number(key, data)
78+
case 'select':
79+
PROPERTY.set_select(key, data)
80+
case 'select':
81+
PROPERTY.set_multi_select(key, data)
82+
case 'checkbox':
83+
PROPERTY.set_checkbox(key, data)
84+
case 'url':
85+
PROPERTY.set_checkbox(key, data)
86+
case 'email':
87+
PROPERTY.set_email(key, data)
88+
case 'phone_number':
89+
PROPERTY.set_phone_number(key, data)
90+
print(PROPERTY.result)
1791

1892
if __name__ == '__main__':
1993
main()

page.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
from properties import Properties
3+
from request_notion import Request
4+
5+
6+
class Page:
7+
def __init__(self, integrations_token):
8+
"""
9+
init
10+
:param integrations_token: Notion Internal Integration Token
11+
"""
12+
self.url = 'https://api.notion.com/v1/pages'
13+
self.result = {}
14+
self.request = Request(self.url, integrations_token=integrations_token)
15+
16+
def create_page(self, database_id, properties=None):
17+
"""
18+
Create a page
19+
:param database_id: Identifier for a Notion database
20+
:param properties: Property values of this page
21+
:param children: Page content for the new page
22+
:return:
23+
"""
24+
if properties is None:
25+
properties = Properties()
26+
properties = properties
27+
28+
body = {
29+
"parent": {
30+
"database_id": database_id
31+
},
32+
"properties": properties.result,
33+
}
34+
self.result = self.request.call_api_post(self.url, body)

properties.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
class Properties:
2+
def __init__(self):
3+
"""
4+
init
5+
"""
6+
self.result = {}
7+
8+
def set_title(self, col, text=None):
9+
"""
10+
title configuration
11+
:param col: column name
12+
:param text: page text. If no text is given, for database only.
13+
:return:
14+
"""
15+
if text:
16+
text = [{"text": {"content": text}}]
17+
else:
18+
text = {}
19+
self.result.update({col: {"title": text}})
20+
21+
def set_rich_text(self, col, text=None):
22+
"""
23+
rich_text configuration
24+
:param col: column name
25+
:param text: page text. If no text is given, for database only.
26+
:return:
27+
"""
28+
if text:
29+
text = [{"text": {"content": text}}]
30+
else:
31+
text = {}
32+
self.result.update({col: {"rich_text": text}})
33+
34+
def set_number(self, col, text=None):
35+
"""
36+
number configuration
37+
:param col: column name
38+
:param text: page text. If no text is given, for database only.
39+
:return:
40+
"""
41+
if text:
42+
text = int(text)
43+
else:
44+
text = {}
45+
self.result.update({col: {"number": text}})
46+
47+
def set_select(self, col, text=None):
48+
"""
49+
select configuration
50+
:param col: column name
51+
:param text: page text. If no text is given, for database only.
52+
:return:
53+
"""
54+
if text:
55+
text = {"name": text}
56+
else:
57+
text = {}
58+
self.result.update({col: {"select": text}})
59+
60+
def set_multi_select(self, col, text_list=None):
61+
"""
62+
multi select configuration
63+
:param col: column name
64+
:param text_list: page text list. If no text is given, for database only.
65+
:return:
66+
"""
67+
if text_list:
68+
data = []
69+
for i in text_list:
70+
data.append({"name": i})
71+
else:
72+
data = {}
73+
74+
self.result.update({col: {"multi_select": data}})
75+
76+
def set_checkbox(self, col, text=None):
77+
"""
78+
checkbox configuration
79+
:param col: column name
80+
:param text: page text. If no text is given, for database only.
81+
:return:
82+
"""
83+
if not text:
84+
text = {}
85+
self.result.update({col: {"checkbox": text}})
86+
87+
def set_url(self, col, text=None):
88+
"""
89+
url configuration
90+
:param col: column name
91+
:param text: page text. If no text is given, for database only.
92+
:return:
93+
"""
94+
if not text:
95+
text = {}
96+
self.result.update({col: {"url": text}})
97+
98+
def set_email(self, col, text=None):
99+
"""
100+
email configuration
101+
:param col: column name
102+
:param text: page text. If no text is given, for database only.
103+
:return:
104+
"""
105+
if not text:
106+
text = {}
107+
self.result.update({col: {"email": text}})
108+
109+
def set_phone_number(self, col, text=None):
110+
"""
111+
phone_number configuration
112+
:param col: column name
113+
:param text: page text. If no text is given, for database only.
114+
:return:
115+
"""
116+
if not text:
117+
text = {}
118+
self.result.update({col: {"phone_number": text}})
119+
120+
def clear(self):
121+
"""
122+
Clear result
123+
:return:
124+
"""
125+
self.result.clear()

request_notion.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
3+
import requests
4+
5+
from __init__ import NOTION_VERSION
6+
7+
class Request:
8+
def __init__(self, url, integrations_token):
9+
"""
10+
init
11+
:param url: Notion API URL
12+
:param integrations_token: Notion Internal Integration Token
13+
"""
14+
self.NOTION_KEY = integrations_token
15+
self.HEADER = {"Authorization": f"Bearer {self.NOTION_KEY}",
16+
"Content-Type": "application/json",
17+
"Notion-Version": NOTION_VERSION}
18+
self.url = url
19+
20+
def call_api_post(self, url, body):
21+
"""
22+
request post
23+
:param url:
24+
:param body:
25+
:return:
26+
"""
27+
r = requests.post(url, data=json.dumps(body), headers=self.HEADER).json()
28+
return r
29+
30+
def call_api_get(self, url):
31+
"""
32+
request get
33+
:param url:
34+
:return:
35+
"""
36+
r = requests.get(url, headers=self.HEADER).json()
37+
return r
38+
39+
def call_api_patch(self, url, body):
40+
"""
41+
request patch
42+
:param url:
43+
:param body:
44+
:return:
45+
"""
46+
r = requests.patch(url, data=json.dumps(body), headers=self.HEADER).json()
47+
return r

0 commit comments

Comments
 (0)