Skip to content

Commit 29311ff

Browse files
committed
feat: clean and doc
1 parent 22f0277 commit 29311ff

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/python_notion_exporter/main.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,35 @@
1313

1414

1515
class ExportType:
16+
"""Represent the different types of export formats."""
17+
1618
MARKDOWN = "markdown"
1719
HTML = "html"
1820
PDF = "pdf"
1921

2022

2123
class ViewExportType:
24+
"""Represent the different view types for export."""
25+
2226
CURRENT_VIEW = "currentView"
2327
ALL = "all"
2428

2529

2630
class NotionExporter:
31+
"""Class to handle exporting Notion content."""
32+
2733
def __init__(
2834
self,
2935
token_v2: str,
3036
file_token: str,
3137
pages: dict,
32-
export_directory=None,
33-
flatten_export_file_tree=True,
34-
export_type=ExportType.MARKDOWN,
35-
current_view_export_type=ViewExportType.CURRENT_VIEW,
36-
include_files=False,
37-
recursive=True,
38-
workers=multiprocessing.cpu_count(),
38+
export_directory: str = None,
39+
flatten_export_file_tree: bool = True,
40+
export_type: ExportType = ExportType.MARKDOWN,
41+
current_view_export_type: ViewExportType = ViewExportType.CURRENT_VIEW,
42+
include_files: bool = False,
43+
recursive: bool = True,
44+
workers: int = multiprocessing.cpu_count(),
3945
):
4046
"""
4147
Initializes the NotionExporter class.
@@ -74,21 +80,28 @@ def __init__(
7480
self.workers = workers
7581
os.makedirs(f"{self.export_directory}{self.export_name}", exist_ok=True)
7682

77-
def _to_uuid_format(self, s):
83+
def _to_uuid_format(self, input_string: str) -> str:
7884
"""
7985
Converts a string to UUID format.
8086
8187
Args:
82-
s (str): The input string.
88+
input_string (str): The input string.
8389
8490
Returns:
8591
str: The string in UUID format.
8692
"""
87-
if "-" == s[8] and "-" == s[13] and "-" == s[18] and "-" == s[23]:
88-
return s
89-
return f"{s[:8]}-{s[8:12]}-{s[12:16]}-{s[16:20]}-{s[20:]}"
90-
91-
def _get_format_options(self, export_type: ExportType, include_files=False):
93+
if (
94+
"-" == input_string[8]
95+
and "-" == input_string[13]
96+
and "-" == input_string[18]
97+
and "-" == input_string[23]
98+
):
99+
return input_string
100+
return f"{input_string[:8]}-{input_string[8:12]}-{input_string[12:16]}-{input_string[16:20]}-{input_string[20:]}"
101+
102+
def _get_format_options(
103+
self, export_type: ExportType, include_files: bool = False
104+
) -> dict:
92105
"""
93106
Retrieves format options based on the export type and whether to include files.
94107
@@ -108,18 +121,18 @@ def _get_format_options(self, export_type: ExportType, include_files=False):
108121

109122
return format_options
110123

111-
def _export(self, id):
124+
def _export(self, page_id: str) -> str:
112125
"""
113126
Initiates the export of a Notion page.
114127
115128
Args:
116-
id (str): The ID of the Notion page.
129+
page_id (str): The ID of the Notion page.
117130
118131
Returns:
119132
str: The task ID of the initiated export.
120133
"""
121134
url = "https://www.notion.so/api/v3/enqueueTask"
122-
id = self._to_uuid_format(s=id)
135+
page_id = self._to_uuid_format(input_string=page_id)
123136
export_options = {
124137
"exportType": self.export_type,
125138
"locale": "en",
@@ -141,7 +154,7 @@ def _export(self, id):
141154
"eventName": "exportBlock",
142155
"request": {
143156
"block": {
144-
"id": id,
157+
"id": page_id,
145158
},
146159
"recursive": self.recursive,
147160
"exportOptions": export_options,
@@ -155,7 +168,7 @@ def _export(self, id):
155168
).json()
156169
return response["taskId"]
157170

158-
def _get_status(self, task_id):
171+
def _get_status(self, task_id: str) -> dict:
159172
"""
160173
Fetches the status of an export task.
161174
@@ -174,7 +187,7 @@ def _get_status(self, task_id):
174187
).json()["results"]
175188
return response[0]
176189

177-
def _download(self, url):
190+
def _download(self, url: str):
178191
"""
179192
Downloads an exported file from a given URL.
180193
@@ -189,7 +202,7 @@ def _download(self, url):
189202
) as f:
190203
f.write(response.content)
191204

192-
def _process_page(self, page_details):
205+
def _process_page(self, page_details: tuple) -> dict:
193206
"""
194207
Processes an individual Notion page for export.
195208
@@ -222,7 +235,7 @@ def _process_page(self, page_details):
222235
"pagesExported": pages_exported,
223236
}
224237

225-
def _wait_for_export_completion(self, task_id):
238+
def _wait_for_export_completion(self, task_id: str) -> tuple[dict, str, str, int]:
226239
"""
227240
Waits until a given export task completes or fails.
228241

0 commit comments

Comments
 (0)