Skip to content

Commit

Permalink
add another frontmatter option and fix timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Sep 19, 2024
1 parent 3df7496 commit b3d23a9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/formats/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ def convert_posts(self):

for posts_file in posts_files:
for post in json.loads(posts_file.read_text(encoding="utf-8")):
updated_time = post["timestamp"] * 1000
updated_time = dt.datetime.utcfromtimestamp(post["timestamp"])
post_body = ""

for post_datum in post["data"]:
for post_data_key, post_data_value in post_datum.items():
match post_data_key:
case "update_timestamp":
updated_time = post_data_value * 1000
updated_time = dt.datetime.utcfromtimestamp(
post_data_value
)
case "post":
post_body = fix_encoding_error(post_data_value)
case _:
Expand Down Expand Up @@ -131,7 +133,7 @@ def convert_posts(self):
imf.Note(
post_title,
post_body,
created=post["timestamp"] * 1000,
created=dt.datetime.utcfromtimestamp(post["timestamp"]),
updated=updated_time,
source_application=self.format,
**att_metadata,
Expand Down
9 changes: 7 additions & 2 deletions src/formats/google_keep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Convert Google Keep notes to the intermediate format."""

import datetime as dt
from pathlib import Path
import json

Expand Down Expand Up @@ -33,8 +34,12 @@ def convert(self, file_or_folder: Path):
note_imf = imf.Note(
note_keep["title"],
note_keep["textContent"],
created=note_keep["userEditedTimestampUsec"] // 1000,
updated=note_keep["userEditedTimestampUsec"] // 1000,
created=dt.datetime.utcfromtimestamp(
note_keep["userEditedTimestampUsec"] // (10**6)
),
updated=dt.datetime.utcfromtimestamp(
note_keep["userEditedTimestampUsec"] // (10**6)
),
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],
Expand Down
5 changes: 3 additions & 2 deletions src/formats/synology_note_station.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Convert Synology Note Station notes to the intermediate format."""

from dataclasses import dataclass
import datetime as dt
import difflib
import hashlib
import json
Expand Down Expand Up @@ -195,8 +196,8 @@ def convert(self, file_or_folder: Path):
note_imf = imf.Note(
note["title"],
body,
created=note["ctime"],
updated=note["mtime"],
created=dt.datetime.utcfromtimestamp(note["ctime"]),
updated=dt.datetime.utcfromtimestamp(note["mtime"]),
source_application=self.format,
tags=[imf.Tag(tag) for tag in note.get("tag", [])],
resources=resources,
Expand Down
22 changes: 21 additions & 1 deletion src/importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Convert the intermediate format to Markdown."""

import dataclasses
import logging
import os.path
from pathlib import Path
Expand Down Expand Up @@ -110,6 +111,26 @@ def __init__(self, progress_bars, config):
def write_note(self, note: imf.Note):
assert note.path is not None
match self.frontmatter:
case "all":
metadata = {}
for field in dataclasses.fields(imf.Note):
match field.name:
case (
"body"
| "resources"
| "note_links"
| "original_id"
| "joplin_id"
| "path"
):
continue # included elsewhere or no metadata
case "tags":
metadata["tags"] = [tag.title for tag in note.tags]
case _:
if (value := getattr(note, field.name)) is not None:
metadata[field.name] = value
post = frontmatter.Post(note.body, **metadata)
frontmatter.dump(post, note.path)
case "joplin":
# https://joplinapp.org/help/dev/spec/interop_with_frontmatter/
# Arbitrary metadata will be ignored.
Expand All @@ -132,7 +153,6 @@ def write_note(self, note: imf.Note):
frontmatter.dump(post, note.path)
case "obsidian":
# https://help.obsidian.md/Editing+and+formatting/Properties#Property+format
# TODO: Add arbitrary metadata?
metadata = {}
if note.tags:
metadata["tags"] = [tag.title for tag in note.tags]
Expand Down
1 change: 0 additions & 1 deletion src/intermediate_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class Note:
longitude: float | None = None
altitude: float | None = None

# TODO: remove?
source_application: str | None = None

# internal data
Expand Down
2 changes: 1 addition & 1 deletion src/jimmy_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main():
parser.add_argument(
"--frontmatter",
default=None,
choices=(None, "joplin", "obsidian"),
choices=(None, "all", "joplin", "obsidian"),
help="Frontmatter type.",
)
parser.add_argument(
Expand Down

0 comments on commit b3d23a9

Please sign in to comment.