13
13
14
14
15
15
class ExportType :
16
+ """Represent the different types of export formats."""
17
+
16
18
MARKDOWN = "markdown"
17
19
HTML = "html"
18
20
PDF = "pdf"
19
21
20
22
21
23
class ViewExportType :
24
+ """Represent the different view types for export."""
25
+
22
26
CURRENT_VIEW = "currentView"
23
27
ALL = "all"
24
28
25
29
26
30
class NotionExporter :
31
+ """Class to handle exporting Notion content."""
32
+
27
33
def __init__ (
28
34
self ,
29
35
token_v2 : str ,
30
36
file_token : str ,
31
37
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 (),
39
45
):
40
46
"""
41
47
Initializes the NotionExporter class.
@@ -74,21 +80,28 @@ def __init__(
74
80
self .workers = workers
75
81
os .makedirs (f"{ self .export_directory } { self .export_name } " , exist_ok = True )
76
82
77
- def _to_uuid_format (self , s ) :
83
+ def _to_uuid_format (self , input_string : str ) -> str :
78
84
"""
79
85
Converts a string to UUID format.
80
86
81
87
Args:
82
- s (str): The input string.
88
+ input_string (str): The input string.
83
89
84
90
Returns:
85
91
str: The string in UUID format.
86
92
"""
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 :
92
105
"""
93
106
Retrieves format options based on the export type and whether to include files.
94
107
@@ -108,18 +121,18 @@ def _get_format_options(self, export_type: ExportType, include_files=False):
108
121
109
122
return format_options
110
123
111
- def _export (self , id ) :
124
+ def _export (self , page_id : str ) -> str :
112
125
"""
113
126
Initiates the export of a Notion page.
114
127
115
128
Args:
116
- id (str): The ID of the Notion page.
129
+ page_id (str): The ID of the Notion page.
117
130
118
131
Returns:
119
132
str: The task ID of the initiated export.
120
133
"""
121
134
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 )
123
136
export_options = {
124
137
"exportType" : self .export_type ,
125
138
"locale" : "en" ,
@@ -141,7 +154,7 @@ def _export(self, id):
141
154
"eventName" : "exportBlock" ,
142
155
"request" : {
143
156
"block" : {
144
- "id" : id ,
157
+ "id" : page_id ,
145
158
},
146
159
"recursive" : self .recursive ,
147
160
"exportOptions" : export_options ,
@@ -155,7 +168,7 @@ def _export(self, id):
155
168
).json ()
156
169
return response ["taskId" ]
157
170
158
- def _get_status (self , task_id ) :
171
+ def _get_status (self , task_id : str ) -> dict :
159
172
"""
160
173
Fetches the status of an export task.
161
174
@@ -174,7 +187,7 @@ def _get_status(self, task_id):
174
187
).json ()["results" ]
175
188
return response [0 ]
176
189
177
- def _download (self , url ):
190
+ def _download (self , url : str ):
178
191
"""
179
192
Downloads an exported file from a given URL.
180
193
@@ -189,7 +202,7 @@ def _download(self, url):
189
202
) as f :
190
203
f .write (response .content )
191
204
192
- def _process_page (self , page_details ) :
205
+ def _process_page (self , page_details : tuple ) -> dict :
193
206
"""
194
207
Processes an individual Notion page for export.
195
208
@@ -222,7 +235,7 @@ def _process_page(self, page_details):
222
235
"pagesExported" : pages_exported ,
223
236
}
224
237
225
- def _wait_for_export_completion (self , task_id ) :
238
+ def _wait_for_export_completion (self , task_id : str ) -> tuple [ dict , str , str , int ] :
226
239
"""
227
240
Waits until a given export task completes or fails.
228
241
0 commit comments