ch-ch-changes - bookdarts - Extract KOReader generated highlights and publish them in a variety of formats↵ DIR Log DIR Files DIR Refs --- DIR commit d764d2841ca02118755f4d8c28fd40838f0c674f DIR parent bf2ac9859ab8bb75fa728c89a9d6682f98931d4d HTML Author: Scarlett McAllister <no+reply@roygbyte.com> Date: Wed, 18 Oct 2023 21:47:05 -0300 ch-ch-changes Diffstat: M bookdarts.py | 25 +++++++++++++++---------- M bookdarts.sh | 14 ++++++++------ M export-from-device.sh | 4 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) --- DIR diff --git a/bookdarts.py b/bookdarts.py @@ -1,4 +1,8 @@ ############################################# +# Usage +# > cat highlights.json | python bookdarts.py + +############################################# # Dependencies import sys import fileinput @@ -13,6 +17,7 @@ import re # Configuration variables EXPORT = dict() EXPORT['DOC_WIDTH'] = 70 +EXPORT['HOST'] = "roygbyte.com" ############################################# # Function definitions @@ -37,8 +42,10 @@ def format_entry(entry): return "%s\n%s\n%s\n" % (date, highlight, chapter) except CalledProcessError as e: print("CalledProcessError:", e) + exit except Exception as e: print("Exception", e) + exit def format_document(document): formatted_document = [] @@ -64,23 +71,25 @@ def format_document(document): print("Exception:", e) exit -def format_document_gph_link(document): +def format_document_link(document, path_to_bookdarts): return "[%s|%s|%s|%s|%s]" %\ - (1, # Type + (0, # Type document['link_label'], # Selector - document['filename'], # Path to file - "", # Host + path_to_bookdarts + document['filename'], # Path to file + EXPORT['HOST'], # Host 70) def publish_index(documents): with open("publish/index.gph", "w", encoding="UTF-8") as file: for document in documents: - file.write(format_document_gph_link(document) + "\n") + file.write(format_document_link(document, + "/bookdarts/documents/") + "\n") def publish_entries(entries, document): "Need the entries for the document and the given document." with open (f"publish/documents/{document['filename']}", "w", encoding="UTF-8") as file: + file.write(document['title'] + "\n\n") file.write("\n\n".join(entries)) ############################################# @@ -99,11 +108,7 @@ except JSONDecodeError as e: f'{e.msg=} {e.pos=} {e.lineno=}') exit formatted_documents = list(map(format_document, documents)) +formatted_documents.sort(key=lambda document: document['title']) publish_index(formatted_documents) for document in formatted_documents: publish_entries(document['entries'], document) - # Port -# Format the documents and then print them -# for document in list(map(format_document, documents)): - # print("\n#",document['title']) - # print("\n\n".join(document['entries'])) DIR diff --git a/bookdarts.sh b/bookdarts.sh @@ -1,14 +1,15 @@ #!/bin/sh # Configuration -HIGHLIGHTS_FILE="highlights-converted.json" +HIGHLIGHTS_FILE="highlights-from-device.json" PUBLISH_DIR="publish" GPH_MENU_FILE="$PUBLISH_DIR/index.gph" MARKDOWN_DOCUMENTS_DIR="$PUBLISH_DIR/documents" # Functions log() { - printf "$1\n" + return 1 + #printf "$1" } standard_input_to_file() { @@ -111,8 +112,9 @@ standard_input_to_file "$GPH_MENU_FILE" <<EOF EOF INDEX=0 +log "\n\n" while [ $(( $INDEX < $DOCUMENTS_COUNT )) = 1 ]; do - printf "Publishing document $INDEX \n" + log "\nDocument $INDEX/$DOCUMENTS_COUNT" TITLE=$(nth_element "$INDEX" | get_document_attribute title) AUTHOR=$(nth_element "$INDEX" | get_document_attribute author) DOCUMENT_FILENAME=$(printf "$TITLE - $AUTHOR" \ @@ -129,18 +131,18 @@ while [ $(( $INDEX < $DOCUMENTS_COUNT )) = 1 ]; do # this document. nth_element "$INDEX" | create_entries_file $DOCUMENT_PUBLISHING_FILE CURR_ENTRY=0 - log "Found $ENTRIES_COUNT entries." + log "\rFound $ENTRIES_COUNT entries." # Populate the file with highlights. if [ $ENTRIES_COUNT > 0 ]; then while [ $(( $CURR_ENTRY < $ENTRIES_COUNT )) = 1 ]; do - #log "Publishing entry $CURR_ENTRY..." + log "\rPublishing entry $CURR_ENTRY..." # TODO: Progress bar! cat "$INDEX-entries.temp" | get_document_attribute entry $CURR_ENTRY \ | publish_entry $DOCUMENT_PUBLISHING_FILE CURR_ENTRY=$(( $CURR_ENTRY + 1 )) done # Construct gph menu item for this document's highlights file. - log "Constructing gph menu for document $INDEX..." + log "\rConstructing gph menu for document $INDEX..." create_gph_menu_item "1" "$TITLE" \ "$DOCUMENT_PUBLISHING_FILE" \ >> $GPH_MENU_FILE DIR diff --git a/export-from-device.sh b/export-from-device.sh @@ -6,8 +6,8 @@ BOOKDARTS_DIR=$PWD DEVICE_DIR=/media/kobo HIGHLIGHTS_DIR=.adds/koreader/clipboard HIGHLIGHTS_FILENAME_REGEX=".*-all-books.json" -HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR" \ - | sort \ +HIGHLIGHTS_FILE=$(ls -a "$DEVICE_DIR/$HIGHLIGHTS_DIR/" \ + | sort -r \ | grep -E "$HIGHLIGHTS_FILENAME_REGEX" \ | head -n 1)