web-search.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ["books"]="http://1lib.us/s/"
  24. ["amazon"]="https://www.amazon.com/s?k="
  25. ["stackoverflow"]="http://stackoverflow.com/search?q="
  26. ["hoogle"]="https://www.google.com/search?q="
  27. ["code"]="https://searchcode.com/?q="
  28. ["beer"]="https://www.beeradvocate.com/search/?qt=beer&q="
  29. ["jira"]="https://15five-dev.atlassian.net/browse/ENG-"
  30. ["booty"]="https://thepiratebay.zone/search/"
  31. ["search"]="https://search.unbl.ink/?q="
  32. ["ebay"]="https://www.ebay.com/sch/i.html?LH_BIN=1&_nkw="
  33. ["ffcompany"]="https://admin.cloud100.15five.com/admin/dj/company/company/"
  34. ["ffuser"]="https://admin.cloud100.15five.com/admin/dj/ff/user/?q="
  35. ["ffsamlconf"]="https://admin.cloud100.15five.com/admin/dj/saml2/saml2config/?q="
  36. ["mathdisc"]="https://github.com/centerofci/mathesar/discussions/"
  37. ["mathissue"]="https://github.com/centerofci/mathesar/issues/"
  38. ["metacritic"]="https://www.metacritic.com/search/all/"
  39. )
  40. # List for rofi
  41. gen_list() {
  42. for i in "${!URLS[@]}"; do
  43. echo "$i"
  44. done
  45. }
  46. urlencode() {
  47. # urlencode <string>
  48. local LANG=C
  49. local length="${#1}"
  50. for ((i = 0; i < length; i++)); do
  51. local c="${1:i:1}"
  52. case $c in
  53. [a-zA-Z0-9.~_-]) printf "$c" ;;
  54. *) printf '%%%02X' "'$c" ;;
  55. esac
  56. done
  57. }
  58. main() {
  59. # Pass the list to rofi
  60. platform=$( (gen_list) | rofi -dmenu -matching fuzzy -no-custom -location 0 -p "Search > ")
  61. if [[ -n "$platform" ]]; then
  62. query=$( (echo) | rofi -dmenu -matching fuzzy -location 0 -p "Query > ")
  63. if [[ -n "$query" ]]; then
  64. URL=${URLS[$platform]}$(urlencode "$query")
  65. if [[ $platform = 'ffcompany' ]]; then
  66. surf $URL
  67. elif [[ $platform = 'ffuser' ]]; then
  68. surf $URL
  69. elif [[ $platform = 'ffsamlconf' ]]; then
  70. surf $URL
  71. else
  72. qutebrowser --target window $URL
  73. fi
  74. else
  75. exit
  76. fi
  77. else
  78. exit
  79. fi
  80. }
  81. main
  82. exit 0