Skip to content

Commit

Permalink
fix: prevent crash for notes with empty resource
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Dec 6, 2021
1 parent 9b706b2 commit 157ace7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion enex2notion/enex_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import hashlib
import logging
import mimetypes
import uuid
from collections import defaultdict
Expand All @@ -11,6 +12,8 @@

from dateutil.parser import isoparse

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class EvernoteResource(object):
Expand Down Expand Up @@ -137,7 +140,11 @@ def _convert_resource(resource_raw):
ext = mimetypes.guess_extension(resource_raw["mime"]) or ""
file_name = f"{uuid.uuid4()}{ext}"

data_bin = base64.b64decode(resource_raw["data"]["#text"])
if resource_raw["data"].get("#text"):
data_bin = base64.b64decode(resource_raw["data"]["#text"])
else:
logger.warning("Empty resource")
data_bin = b""
data_md5 = hashlib.md5(data_bin).hexdigest()

return EvernoteResource(
Expand Down
5 changes: 4 additions & 1 deletion enex2notion/enex_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def upload_note(root, note: EvernoteNote, note_blocks):
logger.info(f"Creating new page for note '{note.title}'")
new_page = _make_page(note, root)

for block in Bar(f"Uploading '{note.title.replace('%', '%%')}'").iter(note_blocks):
# Escape % to prevent progress bar crashing
note_title = note.title.replace("%", "%%")

for block in Bar(f"Uploading '{note_title}'").iter(note_blocks):
upload_block(new_page, block)

# Set proper name after everything is uploaded
Expand Down

0 comments on commit 157ace7

Please sign in to comment.