Skip to content

Commit

Permalink
fix: show processed notes count during upload
Browse files Browse the repository at this point in the history
fix #33
  • Loading branch information
vzhd1701 committed May 25, 2022
1 parent 7a6f270 commit 0546956
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
9 changes: 7 additions & 2 deletions enex2notion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Optional

from enex2notion.cli_wkhtmltopdf import ensure_wkhtmltopdf
from enex2notion.enex_parser import iter_notes
from enex2notion.enex_parser import count_notes, iter_notes
from enex2notion.enex_uploader import (
BadTokenException,
NoteUploadFailException,
Expand Down Expand Up @@ -83,7 +83,9 @@ def upload(self, enex_file: Path):

notebook_root = self._get_notebook_root(enex_file.stem)

for note in iter_notes(enex_file):
notes_total = count_notes(enex_file)

for note_idx, note in enumerate(iter_notes(enex_file), 1):
if note.note_hash in self.done_hashes:
logger.debug(f"Skipping note '{note.title}' (already uploaded)")
continue
Expand All @@ -96,6 +98,9 @@ def upload(self, enex_file: Path):
continue

if notebook_root is not None:
logger.info(
f"Uploading note {note_idx} out of {notes_total} '{note.title}'"
)
_upload_note(notebook_root, note, note_blocks)
self.done_hashes.add(note.note_hash)

Expand Down
17 changes: 17 additions & 0 deletions enex2notion/enex_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@
logger = logging.getLogger(__name__)


def count_notes(enex_file: Path) -> int:
total_notes = 0

with open(enex_file, "rb") as f:
context = ElementTree.iterparse(f, events=("start", "end"))

_, root = next(context)

for event, elem in context:
if event == "end" and elem.tag == "note":
total_notes += 1

root.clear()

return total_notes


def iter_notes(enex_file: Path):
with open(enex_file, "rb") as f:
context = ElementTree.iterparse(f, events=("start", "end"))
Expand Down
7 changes: 2 additions & 5 deletions enex2notion/enex_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ def get_import_root(client, title):


def upload_note(root, note: EvernoteNote, note_blocks):
logger.info(f"Creating new page for note '{note.title}'")
logger.debug(f"Creating new page for note '{note.title}'")
new_page = _make_page(note, root)

# Escape % to prevent progress bar crashing
note_title = note.title.replace("%", "%%")

try:
for block in Bar(f"Uploading '{note_title}'").iter(note_blocks):
for block in Bar().iter(note_blocks):
upload_block(new_page, block)
except HTTPError:
if isinstance(new_page, CollectionRowBlock):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def mock_api(mocker):

@pytest.fixture()
def fake_note_factory(mocker):
mock_count = mocker.patch("enex2notion.cli.count_notes")
mock_iter = mocker.patch("enex2notion.cli.iter_notes")
mock_iter.return_value = [mocker.MagicMock(note_hash="fake_hash", is_webclip=False)]
mock_count.side_effect = lambda x: len(mock_iter.return_value)

return mock_iter

Expand Down

0 comments on commit 0546956

Please sign in to comment.