Skip to content

Instantly share code, notes, and snippets.

@bitbutter
Last active January 8, 2023 14:04

Revisions

  1. bitbutter revised this gist Jan 8, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions kindle_json_to_tanapaste.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #add to C:\Users\username\AppData\Roaming\espanso\scripts\
    import json
    import sys
    sys.stdout.reconfigure(encoding='utf-8')
  2. bitbutter created this gist Jan 8, 2023.
    70 changes: 70 additions & 0 deletions kindle_json_to_tanapaste.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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)