Skip to content

Commit bb7a17d

Browse files
committed
v1.1
1 parent 0d6f775 commit bb7a17d

File tree

5 files changed

+114
-35
lines changed

5 files changed

+114
-35
lines changed

CHANGELOG.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
# Change log
22

3+
## v1.1
4+
5+
### New Features
6+
7+
8+
- Recovery of the token with error message in case of failure
9+
- Reading options and arguments
10+
11+
### Added:
12+
13+
- File ’parser.py’
14+
- File ’notion_api.py’
15+
316
## v1.0
417

5-
Initial release.
18+
### New Features
19+
20+
- Notion database connexion
21+
- Notion read and write on the database
22+
- Reading json file
23+
- Property formating
624

725
### Added:
826

927
- File ’__init__.py’
10-
- File ’main.py’
28+
- File ’main.py’
29+
- File ’page.py’
30+
- File ’database.py’
31+
- File ’properties.py’
32+
- File ’request_notion.py’

main.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,77 @@
1-
from dataclasses import field
2-
import imp
3-
import requests
1+
import optparse
42
import json
53
import os
64

75
from properties import Properties
86
from page import Page
97
from database import Database
8+
from parse import Parser
9+
from notion_api import GetToken
1010

11+
#token = os.environ["NOTION_TOKEN_PUT_EXPERIMENT"]
1112

12-
token = os.environ["NOTION_TOKEN_PUT_EXPERIMENT"]
13-
14-
database_id = 'd08f239718194d1f990c7ed7c67a6653'
15-
16-
headers = {
17-
"Accept": "application/json",
18-
"Notion-Version": "2022-02-22",
19-
"Content-Type": "application/json",
20-
"Authorization": "Bearer " + token
21-
}
13+
#database_id = 'd08f239718194d1f990c7ed7c67a6653'
2214

2315
PROPERTY = Properties()
16+
PARSER = Parser()
17+
GETTOKEN = GetToken()
2418

2519
def main():
2620

27-
fields_key, data_extract = json_reader()
28-
29-
column_type = read_database(fields_key)
21+
parser = optparse.OptionParser()
22+
PARSER.get_option(parser=parser)
23+
24+
datas = json_reader()
25+
26+
for data in datas:
27+
28+
fields_key = data[0]
29+
data_extract = data[1]
30+
31+
column_type = read_database(fields_key)
32+
33+
write_database(column_type,fields_key,data_extract)
3034

31-
write_database(column_type,fields_key,data_extract)
3235

3336
def json_reader():
34-
35-
data_extract =[]
37+
datas = []
38+
data_extract = []
3639

37-
json_file = open('data.json')
40+
json_file = open(PARSER.options.json_input)
3841
json_dict = json.load(json_file)
39-
40-
data_extract.append(json_dict["entries"][0]["_id"])
4142

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"]))
43+
for i in range(len(json_dict["entries"])):
44+
45+
fields_key=list(json_dict["entries"][i]["fields"])
46+
fields_data=list(map(lambda l:json_dict["entries"][i]["fields"][l],json_dict["entries"][0]["fields"]))
47+
48+
data_extract=[json_dict["entries"][i]["_id"], fields_data, json_dict["entries"][i]["body"]]
49+
50+
datas.append([fields_key,data_extract])
4451

45-
data_extract.append(fields_data)
46-
data_extract.append(json_dict["entries"][0]["body"])
47-
4852
json_file.close()
4953

50-
return fields_key,data_extract
54+
return datas
55+
5156

5257
def read_database(fields_key):
5358

54-
D = Database(integrations_token=token)
55-
D.retrieve_database(database_id=database_id,fields_key=fields_key)
59+
D = Database(integrations_token=GETTOKEN.token)
60+
D.retrieve_database(database_id=PARSER.options.notion_db_id,fields_key=fields_key)
5661

5762
return D.properties_list
5863

64+
5965
def write_database(column_type,fields_key,data_extract):
6066

6167
set_property('title','id',data_extract[0])
6268

6369
for type, key, data in zip(column_type,fields_key,data_extract[1]):
6470
set_property(type,key,data)
6571

66-
P = Page(integrations_token=token)
67-
P.create_page(database_id=database_id, properties=PROPERTY)
72+
P = Page(integrations_token=GETTOKEN.token)
73+
P.create_page(database_id=PARSER.options.notion_db_id, properties=PROPERTY)
74+
6875

6976
def set_property(type,key,data):
7077

@@ -89,5 +96,6 @@ def set_property(type,key,data):
8996
PROPERTY.set_phone_number(key, data)
9097
print(PROPERTY.result)
9198

99+
92100
if __name__ == '__main__':
93101
main()

notion_api.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
3+
class GetToken:
4+
def __init__(self):
5+
self.token = self._get_env_variable()
6+
7+
def _get_env_variable(self):
8+
try:
9+
return os.environ["NOTION_TOKEN_PUT_EXPERIMENT"]
10+
except:
11+
print("Notion Integration Token is not found")
12+
print(
13+
"""
14+
Welcome to notionput!
15+
To get started, you need to save your Notion Integration Token.
16+
Find your token at
17+
https://www.notion.so/my-integrations
18+
Then run shell command:
19+
$export NOTION_TOKEN="<Your Token>"
20+
21+
If you want to save this environment variable after reboot,
22+
put upper command in your shell resource(ex: .bashrc or .zshrc)
23+
"""
24+
)
25+
exit(1)

page.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def create_page(self, database_id, properties=None):
1818
Create a page
1919
:param database_id: Identifier for a Notion database
2020
:param properties: Property values of this page
21-
:param children: Page content for the new page
2221
:return:
2322
"""
2423
if properties is None:

parse.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Parser:
2+
def __init__(self):
3+
self.options = object
4+
5+
def get_option(self,parser):
6+
# create OptionParser object
7+
8+
# add options
9+
parser.add_option('-d','--notion-db-id',
10+
dest = 'notion_db_id',
11+
type = 'string',
12+
help = 'specify the id of the database notion')
13+
parser.add_option('-f', '--json-file',
14+
dest = 'json_input',
15+
type = 'string',
16+
help = 'specify the input json file')
17+
18+
(options, args) = parser.parse_args()
19+
self.options = options
20+
if (options.notion_db_id == None):
21+
print("exit")
22+
exit(0)
23+
elif (options.json_input == None):
24+
print("exit")
25+
exit(0)

0 commit comments

Comments
 (0)