Skip to content

Commit

Permalink
feat: better support for 3.10, use threads instead of processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Strvm committed Sep 5, 2023
1 parent 4aca9dd commit 1671ce7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
```python
from python_notion_exporter import NotionExporter

# Important to run in __main__ or in a method due to multiprocessing.
if __name__ == "__main__":
exporter = NotionExporter(
token_v2="YOUR_NOTION_TOKEN",
Expand Down
25 changes: 11 additions & 14 deletions src/python_notion_exporter/main.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import enum
import concurrent
import json
import logging
import multiprocessing
import os
import shutil
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from multiprocessing import Pool, freeze_support

import requests
from tqdm import tqdm


class ExportType(enum.StrEnum):
class ExportType():
MARKDOWN = "markdown"
HTML = "html"
PDF = "pdf"


class ViewExportType(enum.StrEnum):
class ViewExportType():
CURRENT_VIEW = "currentView"
ALL = "all"

Expand Down Expand Up @@ -77,10 +77,10 @@ def _export(self, id):
url = "https://www.notion.so/api/v3/enqueueTask"
id = self._to_uuid_format(s=id)
export_options = {
"exportType": self.export_type.value,
"exportType": self.export_type,
"locale": "en",
"timeZone": "Europe/London",
"collectionViewExportType": self.current_view_export_type.value,
"collectionViewExportType": self.current_view_export_type,
"flattenExportFiletree": self.flatten_export_file_tree,
}

Expand Down Expand Up @@ -184,11 +184,12 @@ def _unpack(self):

def process(self):
logging.info(f"Exporting {len(self.pages)} pages...")
with Pool(processes=self.workers) as pool:

with ThreadPoolExecutor(max_workers=self.workers) as executor:
with tqdm(total=len(self.pages), dynamic_ncols=True) as pbar:
for result in pool.imap_unordered(
self._process_page, self.pages.items()
):
futures = {executor.submit(self._process_page, item): item for item in self.pages.items()}
for future in concurrent.futures.as_completed(futures):
result = future.result()
if result["state"] == "failure":
continue
name = result["name"]
Expand All @@ -200,7 +201,3 @@ def process(self):
pbar.update(1)

self._unpack()


if __name__ == "__main__":
freeze_support()

0 comments on commit 1671ce7

Please sign in to comment.