web-search.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. # -----------------------------------------------------------------------------
  3. # Info:
  4. # author: Miroslav Vidovic
  5. # file: web-search.sh
  6. # created: 24.02.2017.-08:59:54
  7. # revision: ---
  8. # version: 1.0
  9. # -----------------------------------------------------------------------------
  10. # Requirements:
  11. # rofi
  12. # Description:
  13. # Use rofi to search the web.
  14. # Usage:
  15. # web-search.sh
  16. # -----------------------------------------------------------------------------
  17. # Script:
  18. declare -A URLS
  19. # Default book search
  20. # https://b-ok.cc/s/?q="
  21. #
  22. URLS=(
  23. ["search"]="https://duckduckgo.com/?q="
  24. ["booty"]="https://thepiratebay.zone/search/"
  25. ["books"]="https://libgen.is/fiction/?criteria=&language=English&format=epub&q="
  26. ["comics"]="https://annas-archive.org/search?lang=en&content=book_comic&ext=&sort=newest&q="
  27. ["amazon"]="https://www.amazon.com/s?k="
  28. ["stackoverflow"]="http://stackoverflow.com/search?q="
  29. ["hoogle"]="https://www.google.com/search?q="
  30. ["code"]="https://searchcode.com/?q="
  31. ["beer"]="https://www.beeradvocate.com/search/?qt=beer&q="
  32. ["ebay"]="https://www.ebay.com/sch/i.html?LH_BIN=1&_nkw="
  33. ["metacritic"]="https://www.metacritic.com/search/all/"
  34. )
  35. # List for rofi
  36. gen_list() {
  37. for i in "${!URLS[@]}"; do
  38. echo "$i"
  39. done
  40. }
  41. urlencode() {
  42. # urlencode <string>
  43. local LANG=C
  44. local length="${#1}"
  45. for ((i = 0; i < length; i++)); do
  46. local c="${1:i:1}"
  47. case $c in
  48. [a-zA-Z0-9.~_-]) printf "$c" ;;
  49. *) printf '%%%02X' "'$c" ;;
  50. esac
  51. done
  52. }
  53. main() {
  54. # Pass the list to rofi
  55. platform=$( (gen_list) | rofi -dmenu -matching fuzzy -no-custom -location 0 -p "Search > ")
  56. if [[ -n "$platform" ]]; then
  57. query=$( (echo) | rofi -dmenu -matching fuzzy -location 0 -p "Query > ")
  58. if [[ -n "$query" ]]; then
  59. URL=${URLS[$platform]}$(urlencode "$query")
  60. qutebrowser --target window $URL
  61. else
  62. exit
  63. fi
  64. else
  65. exit
  66. fi
  67. }
  68. main
  69. exit 0