parent
473ef08109
commit
0eb643896b
@ -1,74 +1,156 @@ |
|||||||
#!/usr/bin/env bash |
#!/usr/bin/env bash |
||||||
|
|
||||||
# ----------------------------------------------------------------------------- |
|
||||||
# Info: |
############################################################################## |
||||||
# author: Miroslav Vidovic |
# |
||||||
# file: books-search.sh |
# Find and open an ebook from your Calibre library without, ahem, opening your |
||||||
# created: 13.08.2017.-08:06:54 |
# Calibre library. Feeds in format, author, and title to fzf / rofi for you |
||||||
# revision: --- |
# to choose, then uses epy/epr/conversion for TUI/CLI and xdg-open for GUI. |
||||||
# version: 1.0 |
# |
||||||
# https://github.com/miroslavvidovic/rofi-scripts |
# Originally inspired by a script by [Miroslav Vidovic](https://github.com/miroslavvidovic/rofi-scripts) |
||||||
# ----------------------------------------------------------------------------- |
# though rewritten, expanded, and changed quite a freaking lot. |
||||||
# Requirements: |
# |
||||||
# rofi |
# Requires Calibre books be stored in the default structure of |
||||||
# Description: |
# /AUTHOR/TITLE (NUBMER)/BOOKFILE |
||||||
# Use rofi to search my books. |
# |
||||||
# Usage: |
# For example, an example is |
||||||
# books-search.sh |
# /home/steven/documents/Calibre Library/Douglas Adams/So Long, and Thanks for All the Fish (362)/So Long, and Thanks for All the Fish - Douglas Adams.epub |
||||||
# ----------------------------------------------------------------------------- |
# |
||||||
# Script: |
# Creates a flat file "database" on first run in $HOME/.cache/book_search_cache |
||||||
|
# which can be regenerated (cronjob, etc) by running this script with the -e |
||||||
|
# switch. |
||||||
|
# |
||||||
|
# You can also regenerate it immediately before a run by using the -r switch |
||||||
|
############################################################################## |
||||||
|
|
||||||
|
|
||||||
|
#TODO: Get file info without relying on Calibre structure |
||||||
|
|
||||||
# Books directory |
# Books directory |
||||||
BOOKS_DIR=~/Books |
BOOKS_DIR="/home/steven/documents/Calibre Library/" |
||||||
mkdir -p ~/Books |
FD_FIND=$(which fdfind) |
||||||
|
EPY=$(which epy) #https://github.com/wustho/epy |
||||||
# Save find result to F_ARRAY |
EPR=$(which epr) #https://github.com/wustho/epr |
||||||
readarray -t F_ARRAY <<< "$(find "$BOOKS_DIR" -type f -name '*.pdf')" |
declare -a FILES |
||||||
|
REGEN="false" |
||||||
# Associative array for storing books |
CliOnly="true" |
||||||
# key => book name |
CacheFile="$HOME/.cache/book_search_cache" |
||||||
# value => absolute path to the file |
# So that I don't have to worry about the structure of the path too hard |
||||||
# BOOKS['filename']='path' |
SLASHES=$(echo "$BOOKS_DIR" | grep -o '/' | wc -l) |
||||||
declare -A BOOKS |
((SLASHES++)) |
||||||
|
|
||||||
# Add elements to BOOKS array |
|
||||||
get_books() { |
gen_list (){ |
||||||
|
|
||||||
# if [ ${#F_ARRAY[@]} != 0 ]; then |
# I guess this could be done with a do-while loop and process substitution |
||||||
if [[ ! -z ${F_ARRAY[@]} ]]; then |
# instead, but I'm on call and it's 0318 and I don't want to tear it apart. |
||||||
for i in "${!F_ARRAY[@]}" |
|
||||||
do |
if [ -f "$FD_FIND" ];then |
||||||
path=${F_ARRAY[$i]} |
mapfile FILES < <(fdfind -a -e epub -e pdf -e mobi -e azw3 . "$BOOKS_DIR") |
||||||
file=$(basename "${F_ARRAY[$i]}") |
else |
||||||
BOOKS+=(["$file"]="$path") |
mapfile FILES < <(find -H "$BOOKS_DIR" -type f -iname "*.pdf" -or -iname "*.epub" -or -iname "*.mobi" -or -iname "*.azw3") |
||||||
|
fi |
||||||
|
|
||||||
|
echo "" > $CacheFile |
||||||
|
for ((i = 0; i < ${#FILES[@]}; i++));do |
||||||
|
FILES[$i]=$(echo "${FILES[$i]}" | tr -d '\n') |
||||||
|
shortfile=$(basename "${FILES[$i]}") |
||||||
|
EXT=${shortfile##*.} |
||||||
|
PATH0=$(dirname "${FILES[$i]}") |
||||||
|
PATH1=$(echo "$PATH0" | cut -d '/' -f ${SLASHES}- ) |
||||||
|
#echo "$PATH1 @" |
||||||
|
AUTHOR=$(echo "$PATH1" | awk -F '/' '{print $1}') |
||||||
|
TITLE=$(echo "$PATH1" | awk -F '/' '{print $2}' | awk -F '(' '{print $1}') |
||||||
|
printf "%s | %s | %s | %s \n" "${EXT}" "${AUTHOR}" "${TITLE}" "${FILES[$i]}" >> $CacheFile |
||||||
done |
done |
||||||
else |
|
||||||
echo "$BOOKS_DIR is empty!" |
|
||||||
echo "Please put some books in it." |
|
||||||
echo "Only .pdf files are accepted." |
|
||||||
exit |
|
||||||
fi |
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
# List for rofi |
|
||||||
gen_list(){ |
|
||||||
for i in "${!BOOKS[@]}" |
|
||||||
do |
|
||||||
echo "$i" |
|
||||||
done |
|
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
main() { |
main() { |
||||||
get_books |
|
||||||
book=$( (gen_list) | rofi -dmenu -i -matching fuzzy -no-custom -location 0 -p "Book > " ) |
|
||||||
|
|
||||||
if [ -n "$book" ]; then |
if [ "$REGEN" != "false" ];then |
||||||
xdg-open "${BOOKS[$book]}" |
gen_list |
||||||
fi |
fi |
||||||
|
if [ ! -f $CacheFile ];then |
||||||
|
gen_list |
||||||
|
fi |
||||||
|
|
||||||
|
if [ "$CliOnly" == "true" ];then |
||||||
|
SelectedBook=$(cat $CacheFile | fzf --no-hscroll -m --height 60% --border --ansi --no-bold --header "Which Book?" ) |
||||||
|
else |
||||||
|
#use ROFI, not zenity |
||||||
|
SelectedBook=$(cat $CacheFile | rofi -i -dmenu -p "Which Book?" -theme DarkBlue) |
||||||
|
fi |
||||||
|
|
||||||
|
#extra xargs to strip newlines and whitespace |
||||||
|
book=$( echo "$SelectedBook" | awk -F '|' '{print $4}' | xargs) |
||||||
|
type=$( echo "$SelectedBook" | awk -F '|' '{print $1}' | xargs) |
||||||
|
echo "$book" |
||||||
|
read |
||||||
|
if [ -n "$book" ]; then |
||||||
|
if [ "$CliOnly" == "true" ];then |
||||||
|
case "$type" in |
||||||
|
pdf) |
||||||
|
;; |
||||||
|
mobi|azw3) |
||||||
|
;; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if [ -f "$EPY" ];then |
||||||
|
executeme=$(printf "%s \"%s\"" "$EPY" "$book") |
||||||
|
eval "$executeme" |
||||||
|
elif [ -f "$EPR" ];then |
||||||
|
executeme=$(printf "%s \"%s\"" "$EPR" "$book") |
||||||
|
eval "$executeme" |
||||||
|
else |
||||||
|
xdg-open "${book}" |
||||||
|
fi |
||||||
|
else |
||||||
|
xdg-open "${book}" |
||||||
|
fi |
||||||
|
|
||||||
|
# |
||||||
|
else |
||||||
|
echo "Book file not found!" 1>&2 |
||||||
|
exit 1 |
||||||
|
fi |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
main |
|
||||||
|
|
||||||
exit 0 |
############################################################################## |
||||||
|
# Show the Help |
||||||
|
############################################################################## |
||||||
|
display_help(){ |
||||||
|
echo "###################################################################" |
||||||
|
echo "# booksearch.sh [-h|-r]" |
||||||
|
echo "# -h show help " |
||||||
|
echo "# -r regenerate booklist (and run) " |
||||||
|
echo "# -e regenerate booklist (and exit) " |
||||||
|
echo "# -g use GUI (rofi) " |
||||||
|
echo "###################################################################" |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
# Read in variables |
||||||
|
|
||||||
|
while [ $# -gt 0 ]; do |
||||||
|
option="$1" |
||||||
|
case $option in |
||||||
|
-h) display_help |
||||||
|
exit |
||||||
|
shift ;; |
||||||
|
-r) REGEN="true" |
||||||
|
shift ;; |
||||||
|
-e) gen_list |
||||||
|
exit |
||||||
|
shift ;; |
||||||
|
-g) CliOnly="false" |
||||||
|
shift ;; |
||||||
|
esac |
||||||
|
done |
||||||
|
|
||||||
|
main |
||||||
|
Loading…
Reference in new issue