Created
April 6, 2023 15:44
-
-
Save harperreed/7f3f9aa1d9e0bb5c35174b47a40b3fc5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import json | |
import zipfile | |
import os | |
import html2markdown | |
from pathlib import Path | |
from datetime import datetime | |
source = 'notes.zip' | |
with zipfile.ZipFile(source, 'r') as zip_ref: | |
data = json.loads(zip_ref.read('Standard Notes Backup and Import File.txt').decode('utf-8')) | |
def generate_markdown_filename(title, note_number, max_title_length=50): | |
if len(title) > max_title_length: | |
title = title[:max_title_length] + '...' | |
filename = title.replace(" ", "_").replace("/", "_") + f"_{note_number}.md" | |
return filename | |
def save_markdown_file(directory, filename, content, creation_date): | |
Path(directory).mkdir(parents=True, exist_ok=True) | |
file_path = os.path.join(directory, filename) | |
with open(file_path, "w", encoding='utf-8') as text_file: | |
text_file.write(content) | |
timestamp = datetime.timestamp(creation_date) | |
os.utime(file_path, (timestamp, timestamp)) | |
# Populate the tag_notes_links dictionary | |
tag_notes_links = {} | |
for key, item in enumerate(data['items']): | |
if item['content_type'] == 'Tag': | |
for inner_key, reference in enumerate(item['content']['references']): | |
if 'uuid' not in reference: | |
continue | |
if reference['uuid'] in tag_notes_links: | |
tag_notes_links[reference['uuid']].append(item['content']['title']) | |
else: | |
tag_notes_links[reference['uuid']] = [item['content']['title']] | |
markdown_directory = 'markdown' | |
note_number = 1 | |
for key, item in enumerate(data['items']): | |
if item['content_type'] == 'Note': | |
if 'title' in item ['content']: | |
title = item['content']['title'] | |
else: | |
title = f"untitled_note_{note_number}" | |
text = item['content']['text'] | |
# Convert HTML content to Markdown format | |
text = html2markdown.convert(text) | |
# Include the title as H1 heading at the top of the note content | |
content = f"# {title}\n\n{text}" | |
# Append tags (if any) to the bottom of the note content as hashtags | |
if item['uuid'] in tag_notes_links: | |
tags = ' '.join([f"#{tag}" for tag in tag_notes_links[item['uuid']]]) | |
content += f"\n\n{tags}" | |
# Extract the creation date and convert it to a datetime object | |
creation_date_str = item['created_at'][0:19] | |
creation_date = datetime.strptime(creation_date_str, '%Y-%m-%dT%H:%M:%S') | |
# Generate the Markdown filename based on the note title | |
markdown_filename = generate_markdown_filename(title, note_number) | |
# Save the note content to the Markdown file | |
save_markdown_file(markdown_directory, markdown_filename, content, creation_date) | |
# Log the filename and note length | |
print(f"Saved file '{markdown_filename}' with note length {len(content)} characters.") | |
note_number += 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment