redyt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. # If notify-send is not found, use echo as notifier
  3. command -v "notify-send" > /dev/null && notifier="echo" || notifier="notify-send"
  4. # check dependencies
  5. for prog in dmenu jq nomacs wget curl; do
  6. ! command -v "$prog" > /dev/null &&
  7. $notifier "redyt" "Error: Program '$prog' not found." && exit 1
  8. done
  9. # set dirs
  10. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/redyt"
  11. configdir="${XDG_CONFIG_HOME:-$HOME/.config}/redyt"
  12. # if dirs do not exist, create them
  13. [ ! -d "$cachedir" ] && mkdir -p "$cachedir"
  14. [ ! -d "$configdir" ] && mkdir -p "$configdir"
  15. # remove all cached images
  16. rm "${cachedir:?}"/*
  17. # set defaults
  18. defaultsub="memes"
  19. [ -f "$configdir/subreddits" ] || echo "$defaultsub" > "$configdir/subreddits"
  20. limit=100
  21. # select sorting (default: sort by hot)
  22. # sorting=$(printf "hot\nnew\n" | dmenu -p "Select Sorting" -i -l 10)
  23. # [ -z "$sorting" ] &&
  24. sorting="hot"
  25. # when no argument given, use dmenu to ask for subreddit
  26. if [ -z "$1" ]; then
  27. subreddit=$(dmenu -p "Select Subreddit r/" -i -l 10 < \
  28. "$configdir/subreddits" | cut -d\| -f1 | awk '{$1=$1;print}')
  29. # if no subreddit was chosen, exit
  30. [ -z "$subreddit" ] && exit 1
  31. else
  32. subreddit="$1"
  33. fi
  34. # send a notification
  35. $notifier "Redyt" "📩 Downloading your 🖼️ Memes"
  36. # download the subreddit feed, containing only the first 100 entries (limit),
  37. # and store it inside $cachedir/tmp.json
  38. curl -H "User-agent: 'your bot 0.1'" \
  39. "https://www.reddit.com/r/$subreddit/$sorting.json?limit=$limit" \
  40. > "$cachedir/tmp.json"
  41. # get a list of images
  42. imgs="$(jq '.' < "$cachedir/tmp.json" | grep url_overridden_by_dest | \
  43. grep -Eo "http(s|)://.*(jpg|png)\b" | sort -u)"
  44. # if there are no images, exit
  45. [ -z "$imgs" ] && $notifier "Redyt" \
  46. "sadly, there are no images for subreddit $subreddit, " \
  47. "please try again later!" && exit 1
  48. # set welcome img
  49. cp "$configdir/redyt.png" "$cachedir"
  50. # download + display images in cache dir
  51. wget -bq -P "$cachedir" "$imgs" > /dev/null 2> /dev/null
  52. devour nomacs "$cachedir/redyt.png" & #> /dev/null 2> /dev/null