-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion_integration.py
236 lines (188 loc) · 8.71 KB
/
notion_integration.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import sys, os
import ast
import requests
import json
from . import urls
from dotenv import dotenv_values
from os.path import join, dirname
sys.path.append('..')
from .api_connection_header_definition import ApiConnectionHeaderDefinition
dotenv_path = join(dirname(__file__), '.env')
env_values = dotenv_values(dotenv_path)
PARENT_ID = env_values['PARENT_ID']
class DataBase:
"""DataBase object definition, needed to create a Notion Data Base or to use a existing one."""
def __init__(self, parent_id:str, database_name:str, properties:list = {}):
"""Constructor Method
Args:
parent_id (str): Database Parent Id in Notion. i.e.: xxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx; where x is hexadecimal digit
database_name (str): Given name to database.
properties (list, optional):List of properties (column, titles, filter, etc.) required for the database only when one of it is being created. Defaults to {}.
"""
self.parent_id = parent_id
self.database_name = database_name
self.properties = properties
self.api_connection_header_definition = ApiConnectionHeaderDefinition()
self.headers = self.api_connection_header_definition.get_header()
self.database_id = ''
self.database_dictionary = {}
if not properties:
self.database_dictionary = self.get_existing_database()
def get_database_id(self):
"""getter method for de DB id
Returns:
str: DB id provided by Notion API
"""
return self.database_id
def get_databases(self):
"""getter method for de DB object
Returns:
object: DB data in JSON format.
"""
url = urls.DATABASE_OR_PAGE_SEARCH
search_params = {'filter': {'value': 'database', 'property': 'object'}}
try:
search_response = requests.post(
url,
json=search_params, headers=self.headers)
except Exception:
client_error = {'object':'error', 'status':'400', 'code':'client error', 'message':'unable to connect to API'}
return client_error
return search_response.json()
def get_existing_database(self):
"""getter of DB object if it exists in Notion
Returns:
dictionary: 'object' == 'error' || 'object'=='boolean'; 'result' == True if DB exists; 'database' == DB object data if exists
"""
database_results = self.get_databases()
if database_results['object'] == 'error':
error_dict_str = "{'object': 'error','message' : 'Error: status={error_status}, code={error_code}, message={error_message}".format(error_status = database_results['status'], error_code = database_results['code'], error_message=database_results['message'])
error_dict = ast.literal_eval(error_dict_str)
return error_dict
#navigate json response
databases_results = database_results['results']
for database in databases_results:
title = database['title'][0]['text']['content']
if title == self.database_name:
database_exists_object = {'object':'boolean', 'results' : True, 'message':'database name you provided already exists', 'database': database}
self.database_id = database['id']
return database_exists_object
database_does_not_exists_object = {'object':'boolean', 'results': False, 'message':'database name you provided do not exists', 'database':None}
return database_does_not_exists_object
def get_database_parent_data(self):
"""getter of DB parent data, defined in Notion configuration.
Returns:
dictionary: dictionary configuration, 'type' of parent; 'page_id' = string Notion page ID format
"""
return {
'type' : 'page_id',
'page_id' : self.parent_id
}
def get_database_title_data_to_payload(self):
"""getter of DB title object, with format parameters
TO_REFACTOR: decuple each parameter.
Returns:
list: list of objects as needed by API
"""
return [
{
'type' : 'text',
'text' : {
'content' : self.database_name,
'link' : None
},
'plain_text': self.database_name,
'href': None,
'annotations': {
'bold': False,
'italic': False,
'strikethrough': False,
'underline': False,
'code': False,
'color': 'default'
},
}
]
def get_database_properties_data(self):
"""getter of database properties defined by user
Returns:
dictionary: properties defined by user.
"""
properties_object = {}
for propertie in self.properties:
properties_object.update(propertie)
return properties_object
def get_database_payload(self):
"""getter of HTTP request payload as required by API
Returns:
dictionary: dictionary of parent, title and properties data as defined in the rest of getters
"""
return {'parent': self.get_database_parent_data(),
'title': self.get_database_title_data_to_payload(),
'properties': self.get_database_properties_data()
}
def set_database_id(self, database_id:str):
"""setter method for database id in an already instanciated DataBase Object once it has been created
Args:
database_id (str): string with DB id provided by Notion API
Returns:
str: informative text with new database id
"""
self.database_id = database_id
return "database id {db_id} registered".format(db_id=self.database_id)
def create_database_if_not_exists(self):
"""function to create DB validating before if it does not exist in Notion.
Returns:
JSON: respose from API after HTTP request (POST) to create a new DB.
"""
#error triying to querying if db exists
database_exist_validation = self.get_existing_database()
if database_exist_validation['object'] == 'error':
return database_exist_validation
#db already exists
if database_exist_validation['object'] == 'boolean':
if database_exist_validation['results']:
return database_exist_validation
url = urls.DATABASES
payload = self.get_database_payload()
header = self.headers
response = requests.post(url, json=payload, headers=header)
response_json = response.json()
if response_json['object'] == 'database':
self.set_database_id(response_json['id'])
#API response format
return response_json
class Page:
"""Page definition, page instance is a row register in a DB required object"""
def __init__(self, database_object):
self.database_object = database_object
self.api_connection_header_definition = ApiConnectionHeaderDefinition()
self.headers = self.api_connection_header_definition.get_header()
...
def add_row_to_database(self, row_data:dict):
"""new row data to be added to a DB
REFACTOR: Decouple specific data format from this class method
Args:
row_data (dict): key:value dictionary regarding to DB object's columns
"""
book_title=row_data['Book Title']
author=row_data['Author']
type_of_element=row_data['Type of Element']
position=row_data['Position']
highlighted_text=row_data['Highlignted Text']
page=row_data['page']
date=row_data['Date' ]
data = {
'Book Title': {'title': [{'text': {'content': book_title}}]},
'Author': {'rich_text': [{'text': {'content': author}}]},
'Type of Element': {'select': {'name': type_of_element['name']}},
'page': {'number': page},
'Position': {'rich_text': [{'text': {'content': position}}]},
'Highlignted Text': {'rich_text': [{'text': {'content': highlighted_text}}]},
'Date': {'date': {'start': date, 'end': None}},
}
url = urls.PAGES
print(self.database_object.get_database_id())
payload = {'parent':{'database_id': self.database_object.database_id}, 'properties': data}
response = requests.post(url=url, json=payload, headers=self.headers)
print(response.json())