Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. #!/bin/sh
  2. # This is a small script to open a book from your calibre library
  3. # Using dmenu.
  4. USERNAME="yigitcolakoglu"
  5. PASSWORD="$(pass show Server/calibre.yigitcolakoglu.com/yigitcolakoglu)"
  6. HOST="https://calibre.fr1nge.xyz"
  7. function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
  8. rawurlencode() {
  9. local string="${1}"
  10. local strlen=${#string}
  11. local encoded=""
  12. local pos c o
  13. for (( pos=0 ; pos<strlen ; pos++ )); do
  14. c=${string:$pos:1}
  15. case "$c" in
  16. [-_.~a-zA-Z0-9] ) o="${c}" ;;
  17. * ) printf -v o '%%%02x' "'$c"
  18. esac
  19. encoded+="${o}"
  20. done
  21. echo "${encoded}" # You can either set a return variable (FASTER)
  22. REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
  23. }
  24. if [ -d "$XDG_RUNTIME_DIR/books" ]; then
  25. selection="$(find "$XDG_RUNTIME_DIR/books" -type f | dmenu -l 5 -p "Please enter the book's name")"
  26. else
  27. selection="$(printf "" | dmenu -p "Please enter the book's name")"
  28. fi
  29. if [ -f "$selection" ]; then
  30. filename="$selection"
  31. else
  32. bookname="$selection"
  33. query=$(rawurlencode "$bookname")
  34. [ -z "$query" ] && exit
  35. XML=$(curl -s -u "$USERNAME:$PASSWORD" "$HOST/opds/search?query=$query")
  36. menu=$(echo $XML | xpath -q -e '//entry/title | //entry/author/name | //entry/link[@rel="http://opds-spec.org/acquisition"][position()<=1]/@href')
  37. book=$(echo "$menu" | \
  38. sed 's/<title>\(.*\)<\/title>/\1|/;N;s/<name>\(.*\)<\/name>/\1|/;N;s/href="\(.*\)"/\1/;s/\n/ /g' | \
  39. column -s '|' -t -T 1 -c 200| dmenu -i -p "Please select the book" -l 5)
  40. [ -z "$book" ] && exit
  41. name=$(echo "$book" | sed 's/\s\{2,\}/\n/g' | head -n 1)
  42. author=$(echo "$book" | sed 's/\s\{2,\}/\n/g' | head -n 2 | tail -n 1)
  43. path=$(echo "$book" | sed 's/\s\{2,\}/\n/g' | tail -n 1)
  44. mkdir -p "$XDG_RUNTIME_DIR/books"
  45. notify-send -a " Downloading" "$(printf "%s\n%s" "$name" "$author")"
  46. wget -nc -P "$XDG_RUNTIME_DIR/books" -q --content-disposition \
  47. --user "$USERNAME" --password "$PASSWORD" "$HOST$path"
  48. out=$(curl -s --head -u "$USERNAME:$PASSWORD" "$HOST$path" | \
  49. grep "Content-Disposition" |\
  50. sed -E 's/Content-Disposition:.*filename=([^;]*);.*/\1/g')
  51. filename="$XDG_RUNTIME_DIR/books/$(urldecode $out)"
  52. fi
  53. xdg-open "$filename"