Created
May 15, 2022 12:56
-
-
Save emanuele-f/a17bd759cb597351b046555f21812120 to your computer and use it in GitHub Desktop.
Convert standardnotes backup to notally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import json | |
from dateutil import parser | |
import xml.etree.cElementTree as ET | |
from xml.dom import minidom | |
def dt_to_ms(ds): | |
return int(parser.parse(ds).timestamp() * 1000) | |
def json_to_xml(data): | |
root = ET.Element("exported-notes") | |
notes = ET.SubElement(root, "notes") | |
for item in data["items"]: | |
if item["content_type"] != "Note": | |
continue | |
text = item["content"]["text"] | |
title = item["content"]["title"] | |
created = dt_to_ms(item["created_at"]) | |
note = ET.SubElement(notes, "note") | |
ET.SubElement(note, "date-created").text = str(created) | |
ET.SubElement(note, "pinned").text = "false" | |
ET.SubElement(note, "title").text = title | |
ET.SubElement(note, "body").text = text | |
return root | |
def main(): | |
if len(sys.argv) != 2: | |
print("Usage: %s input.txt" % os.path.basename(sys.argv[0])) | |
exit(1) | |
data = None | |
with open(sys.argv[1], "r") as f: | |
data = json.load(f) | |
if not data or not data["items"]: | |
print("Invalid input format") | |
exit(1) | |
xml = json_to_xml(data) | |
print(minidom.parseString(ET.tostring(xml)).toprettyxml(indent=" ")) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment