-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplates.py
106 lines (89 loc) · 4.25 KB
/
templates.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
"""
This stores all the reusable code snippets to use throughout the project.
"""
import requests
import typing
import json
def get_row_ids(endpoint_url:str, headers:dict, database_id:str) -> tuple[int, list]:
"""
### Extracts the row IDs of a database.
Args:
endpoint_url (str): e.g. https://api.notion.com/v1
headers (dict): Additional information to pass with the http request
database_id (str): ID of the Database whose Row IDs are to be extracted
Returns:
int: Status code of the http request (e.g. 200-Success, 400-Failure)
list: If successful, row_ids. Otherwise, error message!
"""
# API endpoint to retrieve all pages (rows) in the database
query_url = f"{endpoint_url}/databases/{database_id}/query"
# Send a POST request to retrieve all pages
query_response = requests.post(query_url, headers=headers)
# Check if the request was successful
if query_response.status_code == 200:
data = query_response.json()
# Extract row IDs from the query_response
row_ids = [page["id"] for page in data["results"]]
return query_response.status_code, row_ids
else:
return query_response.status_code, query_response.text
def extract_id_of_an_inline_databases(endpoint_url:str, headers:dict, parent_page_id:str) -> str:
"""
### Extracts the ID of an inline database within a given page ID.
Args:
endpoint_url (str): e.g. https://api.notion.com/v1
headers (dict): Additional information to pass with the http request
parent_page_id (str): ID of the page whose inline database ID should be extracted.
Returns:
str: The ID of the inline database
"""
page_response = requests.get(f"{endpoint_url}/blocks/{parent_page_id}/children", headers=headers)
response_json = page_response.json()
children = response_json["results"]
for child_block in children:
if child_block["type"] == "child_database":
return child_block["id"]
def get_page_title(endpoint_url:str, headers:dict, page_id:str, page_title_property_name:str = "Name") -> str:
"""
### Returns the title of a given page
Args:
endpoint_url (str): e.g. https://api.notion.com/v1
headers (dict): Additional information to pass with the http request
page_id (str): ID of the page whose title should be extracted
page_title_property_name (str, optional): The name of the column which contains page names. Defaults to "Name".
Returns:
str: The title of the page
"""
page_response = requests.get(f"{endpoint_url}/pages/{page_id}", headers=headers)
page_data = page_response.json()
page_title = page_data["properties"][page_title_property_name]["title"][0]["text"]["content"]
return page_title
def get_child_databases(endpoint_url:str, headers:str, parent_db_id:str) -> typing.Union[dict, None]:
"""
### Returns a dictionary with all the child databases in the parent database paired with their names and ids.
Args:
endpoint_url (str): e.g. https://api.notion.com/v1
headers (dict): Additional information to pass with the http request
parent_db_id (str): The ID of the parent database which stores all the main subjects (e.g. StudySphere)
Returns:
dict: A dictionary with (key, value) pairs where key = child_database_name and value = database_id
__OR__
None: When an error occurs...
"""
# Pair up the subject name to study database
# Extracting page ids
db_id_extraction_status, main_db_page_ids = get_row_ids(endpoint_url, headers, parent_db_id)
if db_id_extraction_status != 200:
print(f"An Error Occurred! \nError -> {main_db_page_ids}") # When a failure occurs, main_db_page_ids contain the error message
return None
else:
pass
main_study_databases = {} # Data is stored in (key, value) pairs where key = subject name, value = database id
for page_id in main_db_page_ids:
inline_db_id = extract_id_of_an_inline_databases(endpoint_url, headers, page_id)
if inline_db_id:
page_title = get_page_title(endpoint_url, headers, page_id)
main_study_databases[page_title] = inline_db_id
else:
pass
return main_study_databases