bookmarks-dl.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/sh
  2. #―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
  3. # Name: bookmarks-dl
  4. # Desc: A script used to download remote bookmarks into the XBEL format.
  5. # Auth: Jaidyn Ann <jadedctrl@posteo.at>
  6. # Date: 2023-09-02
  7. # Reqs: lynx, jq, gsed
  8. # Lisc: GPLv3
  9. #―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
  10. SOURCE_DIRS="./sources/ $HOME/.local/libexec/bookmarks-dl/sources/ /usr/local/libexec/bookmarks-dl/sources/ /usr/libexec/bookmarks-dl/sources/"
  11. usage() {
  12. 1>&2 echo "usage: $(basename "$0") SOURCE ..."
  13. 1>&2 echo " $(basename "$0") --list"
  14. 1>&2 echo " $(basename "$0") --help"
  15. 1>&2 echo ""
  16. 1>&2 echo " SOURCE is a source of bookmarks."
  17. 1>&2 echo " You can see a list of sources with '--list'."
  18. }
  19. # Return the paths to all available bookmarks-dl “source” scripts.
  20. all_sources() {
  21. find $SOURCE_DIRS -type f -name '*.sh' \
  22. 2> /dev/null
  23. }
  24. # Return the path to a specific bookmarks-dl source.
  25. get_source() {
  26. local script_name="$1"
  27. all_sources \
  28. | grep "/$script_name.sh" \
  29. | head -1
  30. }
  31. # List all available bookmarks-dl sources user-friendly-like.
  32. list_sources() {
  33. for source in $(all_sources); do
  34. printf '%s\t%s\n' \
  35. "$(basename "$source" | sed 's/\.sh//')" \
  36. "$source"
  37. done
  38. }
  39. # The function called to parse arguments of a source and begin downloading
  40. # bookmarks-dl.sh
  41. # This should be overloaded by a “source” script.
  42. source_start() {
  43. exit 4
  44. }
  45. # ————————————————————————————————————————
  46. # MISC. UTILS
  47. # ————————————————————————————————————————
  48. # Trims preceding and trailing spaces of a string.
  49. # Be warned: Uses extended regexps, a GNUism!
  50. trim_spaces() {
  51. sed -E 's%^[[:space:]]+%%g' \
  52. | sed -E 's%[[:space:]]+$%%g'
  53. }
  54. # Given some HTML, return it’s plain-text and deescaped form.
  55. html_text_deescape() {
  56. lynx -stdin -dump -nolist --assume_charset=utf8 --display_charset=utf8 \
  57. | trim_spaces
  58. }
  59. # Print a piped string in HTML-escaped form.
  60. html_escape() {
  61. json_escape \
  62. | jq -r '. | @html'
  63. }
  64. # Print a piped string in JSON-escaped format.
  65. json_escape() {
  66. sed 's!"!\\"!g' \
  67. | perl -pe 's!\n!\\n!' \
  68. | sed 's/^/"/' \
  69. | sed 's/$/"/'
  70. }
  71. # In case we want to look (mostly) like a normal web-browser.
  72. curl_browseresque() {
  73. curl $@ \
  74. --compressed \
  75. -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  76. -H 'sec-ch-ua-mobile: ?0' \
  77. -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' \
  78. -H 'sec-ch-ua-platform: "Linux"' \
  79. -H 'Accept: applincation/json' \
  80. -H 'Accept-Language: en-US,en;q=0.5' \
  81. -H 'Accept-Encoding: gzip, deflate, br' \
  82. -H 'DNT: 1' \
  83. -H 'Connection: keep-alive' \
  84. -H 'Sec-Fetch-Dest: empty' \
  85. -H 'Sec-Fetch-Mode: cors' \
  86. -H 'Sec-Fetch-Site: same-origin' \
  87. -H 'TE: trailers'
  88. }
  89. # ————————————————————————————————————————
  90. # INVOCATION
  91. # ————————————————————————————————————————
  92. SOURCE_NAME="$1"
  93. case "$SOURCE_NAME" in
  94. --list|list)
  95. list_sources
  96. exit 0
  97. ;;
  98. --help|-h|help|'')
  99. usage
  100. exit 1
  101. ;;
  102. *)
  103. SOURCE="$(get_source "$SOURCE_NAME")"
  104. if test -f "$SOURCE"; then
  105. source "$SOURCE"
  106. fi
  107. if test "$?" -ne 0 -o ! -f "$SOURCE"; then
  108. 1>&2 echo "The source '$SOURCE_NAME' couldn’t be found."
  109. 1>&2 echo "Try '$(basename "$0") --list' to see a list of possible sources."
  110. fi
  111. ;;
  112. esac
  113. if test -z "$1"; then
  114. usage
  115. exit 1
  116. else
  117. shift
  118. fi
  119. # This should be overloaded.
  120. source_start $@