Last active
January 8, 2023 14:04
-
-
Save bitbutter/c5c95a0c2a12cd9de85d9a334a898a29 to your computer and use it in GitHub Desktop.
Convert kindle highlight .json file to tana paste format
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
#add to C:\Users\username\AppData\Roaming\espanso\scripts\ | |
import json | |
import sys | |
sys.stdout.reconfigure(encoding='utf-8') | |
import io | |
import os | |
import traceback | |
def is_json_file(file): | |
filename, file_extension = os.path.splitext(file) | |
# Split the filename of the latest downloaded file into the base filename and file extension | |
# Check if the file extension is '.json' | |
if file_extension == '.json': | |
return True | |
else: | |
return False | |
def get_newest_json_file(directory): | |
# Get a list of all files in the specified directory | |
files = os.listdir(directory) | |
# Filter the list of files to only include those with a .json extension | |
json_files = [f for f in files if f.endswith('.json')] | |
# Sort the list of files by modification time, in reverse order | |
json_files = sorted(json_files, key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) | |
# Return the first (newest) file in the sorted list | |
return json_files[0] | |
def from_json_to_tanapaste(data) -> str: | |
# Create a list to hold the tana Paste lines | |
lines = [] | |
# Add the tana Paste prefix | |
lines.append("%%tana%%") | |
# Add the book title and ASIN | |
lines.append(f"- {data['title']} #book") | |
lines.append(f" - authors:: {data['authors']} #person") | |
if data['asin'] is not None: | |
lines.append(f" - ASIN:: {data['asin']}") | |
lines.append(f" - url:: https://www.amazon.com/exec/obidos/ASIN/{data['asin']}") | |
# Add the highlights | |
lines.append(" - Highlights") | |
for highlight in data["highlights"]: | |
lines.append(f" - {highlight['text']} #[[kindle highlight]]") | |
lines.append(f" - url:: {highlight['location']['url']}") | |
if highlight["note"] is not None: | |
lines.append(f" - note:: {highlight['note']}") | |
# Return the tana Paste string | |
return "\n".join(lines) | |
# Get the full path to the user's home directory | |
home_dir = os.path.expanduser('~') | |
# Append the 'Downloads' directory to the home directory | |
downloads_dir = os.path.join(home_dir, 'Downloads') | |
latest_file = get_newest_json_file(downloads_dir) | |
latest_file_path = os.path.join(downloads_dir, latest_file) | |
tanapastestring="not yet set" | |
if is_json_file(latest_file): | |
# Open the JSON file | |
with io.open(latest_file_path, encoding="utf-8") as f: | |
# Parse the JSON data | |
tanapastestring = from_json_to_tanapaste(json.load(f)) | |
# translate to tanapaste | |
# pass the tana paste formatted string back to espanso | |
#tanapastestring="We’re trying to teach you to be dangerous—to the enemy. Dangerous even without a knife" | |
print(tanapastestring) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment