Skip to content

Commit d02a054

Browse files
committed
feat: package
1 parent dda8150 commit d02a054

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
1. **Initialization**:
2020

2121
```python
22-
exporter = NotionExporter(
23-
token_v2="YOUR_NOTION_TOKEN",
24-
file_token="YOUR_FILE_TOKEN",
25-
pages={"Page Name": "Page ID"},
26-
export_directory="path/to/save",
27-
flatten_export_file_tree=True,
28-
export_type=ExportType.MARKDOWN,
29-
current_view_export_type=ViewExportType.CURRENT_VIEW,
30-
include_files=True,
31-
recursive=True
32-
)
22+
# Important to run in __main__ or in a method due to multiprocessing.
23+
if __name__ == "__main__":
24+
exporter = NotionExporter(
25+
token_v2="YOUR_NOTION_TOKEN",
26+
file_token="YOUR_FILE_TOKEN",
27+
pages={"Page Name": "Page ID"},
28+
export_directory="path/to/save",
29+
flatten_export_file_tree=True,
30+
export_type=ExportType.MARKDOWN,
31+
current_view_export_type=ViewExportType.CURRENT_VIEW,
32+
include_files=True,
33+
recursive=True
34+
)
3335
```
3436

3537
You will need to get the `token_v2` and `file_token` values from your Notion cookies. The `pages` dictionary should contain pairs of `page_name: page_id` for each page you want to export.

src/python_notion_exporter/main.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import shutil
77
import time
88
from datetime import datetime
9-
from multiprocessing import Pool
9+
from multiprocessing import Pool, freeze_support
1010

1111
import requests
1212
from tqdm import tqdm
@@ -58,12 +58,12 @@ def __init__(
5858
self.workers = workers
5959
os.makedirs(f"{self.export_directory}{self.export_name}", exist_ok=True)
6060

61-
def to_uuid_format(self, s):
61+
def _to_uuid_format(self, s):
6262
if "-" == s[8] and "-" == s[13] and "-" == s[18] and "-" == s[23]:
6363
return s
6464
return f"{s[:8]}-{s[8:12]}-{s[12:16]}-{s[16:20]}-{s[20:]}"
6565

66-
def get_format_options(self, export_type: ExportType, include_files=False):
66+
def _get_format_options(self, export_type: ExportType, include_files=False):
6767
format_options = {}
6868
if export_type == ExportType.PDF:
6969
format_options["pdfFormat"] = "Letter"
@@ -73,9 +73,9 @@ def get_format_options(self, export_type: ExportType, include_files=False):
7373

7474
return format_options
7575

76-
def export(self, id):
76+
def _export(self, id):
7777
url = "https://www.notion.so/api/v3/enqueueTask"
78-
id = self.to_uuid_format(s=id)
78+
id = self._to_uuid_format(s=id)
7979
export_options = {
8080
"exportType": self.export_type.value,
8181
"locale": "en",
@@ -86,7 +86,7 @@ def export(self, id):
8686

8787
# Update the exportOptions with format-specific options
8888
export_options.update(
89-
self.get_format_options(
89+
self._get_format_options(
9090
export_type=self.export_type, include_files=self.include_files
9191
)
9292
)
@@ -111,7 +111,7 @@ def export(self, id):
111111
).json()
112112
return response["taskId"]
113113

114-
def get_status(self, task_id):
114+
def _get_status(self, task_id):
115115
url = "https://www.notion.so/api/v3/getTasks"
116116

117117
payload = json.dumps({"taskIds": [task_id]})
@@ -121,7 +121,7 @@ def get_status(self, task_id):
121121
).json()["results"]
122122
return response[0]
123123

124-
def download(self, url):
124+
def _download(self, url):
125125
response = requests.request("GET", url, headers=self.download_headers)
126126
file_name = url.split("/")[-1][100:]
127127
with open(
@@ -130,9 +130,9 @@ def download(self, url):
130130
) as f:
131131
f.write(response.content)
132132

133-
def process_page(self, page_details):
133+
def _process_page(self, page_details):
134134
name, id = page_details
135-
task_id = self.export(id)
135+
task_id = self._export(id)
136136

137137
status, state, error, pages_exported = self._wait_for_export_completion(
138138
task_id=task_id
@@ -143,7 +143,7 @@ def process_page(self, page_details):
143143

144144
export_url = status.get("status", {}).get("exportURL")
145145
if export_url:
146-
self.download(export_url)
146+
self._download(export_url)
147147
else:
148148
logging.warning(f"Failed to get exportURL for {name}")
149149

@@ -157,7 +157,7 @@ def process_page(self, page_details):
157157
def _wait_for_export_completion(self, task_id):
158158
"""Helper method to wait until the export is complete or failed."""
159159
while True:
160-
status = self.get_status(task_id)
160+
status = self._get_status(task_id)
161161
# print(status)
162162

163163
if not status:
@@ -174,7 +174,7 @@ def _wait_for_export_completion(self, task_id):
174174
)
175175
time.sleep(1)
176176

177-
def unpack(self):
177+
def _unpack(self):
178178
directory_path = f"{self.export_directory}{self.export_name}"
179179
for file in os.listdir(directory_path):
180180
if file.endswith(".zip"):
@@ -187,7 +187,7 @@ def process(self):
187187
with Pool(processes=self.workers) as pool:
188188
with tqdm(total=len(self.pages), dynamic_ncols=True) as pbar:
189189
for result in pool.imap_unordered(
190-
self.process_page, self.pages.items()
190+
self._process_page, self.pages.items()
191191
):
192192
if result["state"] == "failure":
193193
continue
@@ -199,4 +199,8 @@ def process(self):
199199
)
200200
pbar.update(1)
201201

202-
self.unpack()
202+
self._unpack()
203+
204+
205+
if __name__ == "__main__":
206+
freeze_support()

0 commit comments

Comments
 (0)