Skip to content

Commit

Permalink
keep: don't fail if some key is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Sep 20, 2024
1 parent b3d23a9 commit dcaa965
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tarfile
import tempfile
import time
import uuid
import zipfile

import enlighten
Expand Down Expand Up @@ -51,6 +52,10 @@ def try_transfer_dicts(source: dict, target: dict, keys: list[str | tuple[str, s
target[target_key] = value


def create_unique_title() -> str:
return f"unnamed_{uuid.uuid4().hex}"


###########################################################
# stats
###########################################################
Expand Down
26 changes: 17 additions & 9 deletions src/formats/google_keep.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,32 @@ def convert(self, file_or_folder: Path):
# take only the exports in json format
for file_ in self.root_path.glob("**/*.json"):
note_keep = json.loads(Path(file_).read_text(encoding="utf-8"))
tags_keep = [label["name"] for label in note_keep.get("labels", [])]

title = note_keep.get("title", "")
self.logger.debug(f'Converting note "{title}"')

tags_keep = [
label["name"]
for label in note_keep.get("labels", [])
if "name" in label
]

resources_keep = []
for resource_keep in note_keep.get("attachments", []):
resources_keep.append(
imf.Resource(file_.parent.absolute() / resource_keep["filePath"])
)

note_imf = imf.Note(
note_keep["title"],
note_keep["textContent"],
created=dt.datetime.utcfromtimestamp(
note_keep["userEditedTimestampUsec"] // (10**6)
),
updated=dt.datetime.utcfromtimestamp(
note_keep["userEditedTimestampUsec"] // (10**6)
),
title,
note_keep.get("textContent", ""),
source_application=self.format,
# Labels / tags don't have a separate id. Just use the name as id.
tags=[imf.Tag(tag) for tag in tags_keep],
resources=resources_keep,
)
if (value := note_keep.get("createdTimestampUsec")) is not None:
note_imf.created = dt.datetime.utcfromtimestamp(value // (10**6))
if (value := note_keep.get("userEditedTimestampUsec")) is not None:
note_imf.updated = dt.datetime.utcfromtimestamp(value // (10**6))
self.root_notebook.child_notes.append(note_imf)
4 changes: 2 additions & 2 deletions src/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import platform
import shutil
import urllib.parse
import uuid

import frontmatter

import common
import intermediate_format as imf


Expand Down Expand Up @@ -44,7 +44,7 @@ def safe_path(path: Path | str, system: str = SYSTEM) -> Path | str:
"""
safe_name = path if isinstance(path, str) else path.name
if safe_name == "":
return f"unnamed_{uuid.uuid4().hex}"
return common.create_unique_title()

# https://stackoverflow.com/a/31976060
match system:
Expand Down

0 comments on commit dcaa965

Please sign in to comment.