Skip to content

Instantly share code, notes, and snippets.

@bitbutter
Last active March 7, 2023 09:13
Show Gist options
  • Save bitbutter/247ee9257ab0241c8e008158dbe3ce9f to your computer and use it in GitHub Desktop.
Save bitbutter/247ee9257ab0241c8e008158dbe3ce9f to your computer and use it in GitHub Desktop.
Python script to be called by Espanso trigger, for importing kindle hightlights to Tana. Put this in C:\Users\[username]\AppData\Roaming\espanso\scripts
import sys
sys.stdout.reconfigure(encoding='utf-8')
from datetime import datetime
# Open the clippings file and read its contents
with open("E:/documents/My Clippings.txt", "r", encoding="utf-8") as f:
contents = f.read()
# Split the contents into separate notes
notes = contents.split("==========\n")
# Create a dictionary to hold the book titles and their corresponding last note timestamps
book_dict = {}
# Loop through the notes and extract the book title and last note timestamp for each one
for note in notes:
lines = note.split("\n")
if len(lines) > 2:
book_title = lines[0]
last_line = lines[1]
date_str = last_line.split("Added on ")[-1].strip()
try:
date_obj = datetime.strptime(date_str, "%A, %B %d, %Y, %I:%M %p")
last_note_timestamp = date_obj.timestamp()
except ValueError:
continue
if book_title in book_dict:
if last_note_timestamp > book_dict[book_title]:
book_dict[book_title] = last_note_timestamp
else:
book_dict[book_title] = last_note_timestamp
# Sort the book titles by their corresponding last note timestamps in reverse chronological order
sorted_books = sorted(book_dict.keys(), key=lambda x: book_dict[x], reverse=True)
# Print the sorted book titles, separated by line breaks
for book_title in sorted_books:
# skip the ones that give unicode conversion errors. wtf.
try:
print(book_title)
except:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment