pocketbook-notes (2525B)
1 #!/bin/sh 2 die() { printf "%s\n" "$1" >&2 && exit 1; } 3 4 usage() { 5 cat <<EOF 6 Usage: 7 pocketbook-notes command /path/to/books.db '%wildcarded%book%title%' 8 9 Commands: 10 get 11 clear 12 prune 13 help 14 EOF 15 } 16 for dep in 'sqlite3' 'sed' 'jq'; do 17 command -v "$dep" >/dev/null 2>&1 || die "Error: $dep not installed." 18 done 19 20 case "$1" in 21 get) 22 [ $# -eq 3 ] || { usage; die "Three arguments required."; } 23 books_db="$2" 24 [ -f "$books_db" ] || die "Couldn't read $books_db." 25 title_wild="$3" 26 # to grep a whole DB, do: `sqlite3 <filename> .dump | grep -i 'whatever'` 27 # don't judge me on that jq script. I know that double-capture sucks. 28 sqlite3 "$books_db" \ 29 "pragma foreign_keys = on; 30 select Tags.Val 31 from items join tags on (Items.OID = Tags.ItemID) 32 where Items.ParentID in (select OID from Books where Title like \"$title_wild\") 33 and TypeId = (select OID from TypeNames where TypeName = \"obj.book_mark\") 34 and TagId = (select OID from TagNames where TagName = \"bm.quotation\");" \ 35 | sed '$!s/$/,/; 1s/^/[/; $s/$/]/' \ 36 | jq -r '[to_entries | .[] | select(.value | has("begin")) | .value] 37 | map({ 38 text: (.text | gsub("\n"; "")), 39 page: (.begin | capture("(?<page>(?<=word\\?page=)[0-9]+)") | .page | tonumber), 40 offset: (.begin | capture("(?<offs>(?<=offs=)[0-9]+(?=##))") | .offs | tonumber) 41 }) | sort_by(.page, .offset) | .[].text' \ 42 | sed '/^$/d; s/^/* /' 43 ;; 44 clear) 45 [ $# -eq 3 ] || { usage; die "Three arguments required."; } 46 books_db="$2" 47 [ -f "$books_db" ] || die "Couldn't read $books_db." 48 title_wild="$3" 49 printf "Clear all notes from %s? " "$title_wild" 50 read -r conf 51 case $conf in 52 Y*|y*) 53 sqlite3 "$books_db" \ 54 "pragma foreign_keys = on; 55 delete from Items 56 where OID in (select Tags.ItemID from Tags where Tags.TagId = (select OID from TagNames where TagName = 'bm.quotation')) 57 and Items.ParentId = (select OID from Books where Title like '$title_wild') 58 and Items.TypeId = (select OID from TypeNames where TypeName = 'obj.book_mark');" 59 printf "Notes cleared.\n" 60 ;; 61 *) 62 printf "Cancelled.\n" 63 exit 0 64 ;; 65 esac 66 ;; 67 prune) 68 printf "TODO: remove all notes from books not present on device.\n" 69 ;; 70 help) 71 usage 72 exit 0 73 ;; 74 *) 75 printf "Unsupported command.\n" 76 ;; 77 esac