memeatlas-sync 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. # constants
  3. conf="${XDG_CONFIG_DIR:-$HOME/.config}/memeatlas-sync"
  4. cache="${XDG_CACHE_DIR:-$HOME/.cache}/memeatlas-sync"
  5. base_url="https://www.memeatlas.com/"
  6. ex_base_url="https:\/\/www.memeatlas.com\/"
  7. oldindex="none"
  8. # check dependencies
  9. for prog in jq wget; do
  10. ! command -v "$prog" > /dev/null &&
  11. echo "Error: Program '$prog' not found." && exit 1
  12. done
  13. error() {
  14. echo "Error: $@" && exit 1
  15. }
  16. # make sure needed dirs exist
  17. [ -d "$conf" ] || mkdir -p "$conf" || error "Could not create config directory"
  18. [ -d "$cache" ] || mkdir -p "$cache" || error "Could not create cache directory"
  19. # get dest dir
  20. [ -f "$conf/config" ] || error "No config file found"
  21. dest="$(cat "$conf/config")"
  22. # TODO: check $dest and s/~/$HOME/
  23. echo "Fetching Index ..."
  24. [ -f "$cache/index" ] && mv "$cache/index" "$cache/index.old" && oldindex=
  25. [ -f "$cache/index.html" ] && rm "$cache/index.html"
  26. wget -q --show-progress -P "$cache" "$base_url"
  27. categories="$(sed '0,/index.html/d; 0,/contact/!d' "$conf/index.html" | \
  28. head -n -1 | sed 's/^.*href="//; s/.html.*$//')"
  29. for c in $categories; do
  30. [ -f "$cache/$c.html" ] && rm "$cache/$c.html"
  31. done
  32. echo "$categories" | sed "s/^/$ex_base_url/; s/$/.html/" | wget -q --show-progress -P "$cache" -i -
  33. printf "\nGenerating Index ...\n"
  34. # generate new index
  35. for c in $categories; do
  36. sed '0,/id="list/d; /flexListItem/!d; s/^.*href="//; s/".*$//' \
  37. "$cache/$c.html" >> "$cache/index"
  38. done
  39. # check what files are new, create download list
  40. if [ -z "$oldindex" ]; then
  41. [ -f "$cache/dl" ] && rm "$cache/dl"
  42. while IFS='' read -r line; do
  43. grep -q "$line" "$cache/index.old" || echo "$line" >> "$cache/dl"
  44. done < "$cache/index"
  45. else
  46. cp "$cache/index" "$cache/dl"
  47. fi
  48. if [ ! -f "$cache/dl" ]; then
  49. printf "\nDownloading Skipped, already up-to-date...\n"
  50. else
  51. printf "\nDownloading Images...\n"
  52. sed "s/^/$ex_base_url/;" "$cache/dl" | \
  53. wget -q --show-progress -P "$dest" -i -
  54. fi