diff options
Diffstat (limited to '.scripts/redeat')
-rwxr-xr-x | .scripts/redeat | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/.scripts/redeat b/.scripts/redeat new file mode 100755 index 0000000..e282131 --- /dev/null +++ b/.scripts/redeat @@ -0,0 +1,108 @@ +#!/bin/sh + +# Check if necessary programs are installed +for prog in dmenu jq sxiv; do + [ ! "$(which "$prog")" ] && echo "Please install $prog!" && exit 1 +done +# If notify-send is not installed, use echo as notifier +[ ! "$(which notify-send)" ] && notifier="echo" || notifier="notify-send" + +# args +while [ $# -gt 0 ]; do + case $1 in + -l|--limit) + shift + LIMIT=$1 + case $LIMIT in + ''|*[!0-9]*) + echo 'limit is NaN' + exit 1 + esac + shift + ;; + -f|--filter) + FILTER=1 + shift + ;; + -k|--keep) + KEEP=0 + shift + ;; + -v|--verbose) + VERBOSE=1 + shift + ;; + *) + subreddit=$1 + shift + ;; + esac +done + +# Default config directory +configdir="${XDG_CONFIG_HOME:-$HOME/.config}/redyt" + +# Create .config/redyt if it does not exist to prevent +# the program from not functioning properly +[ ! -d "$configdir" ] && echo "Directory $configdir does not exist, creating..." && mkdir -p "$configdir" + +# Default subreddit that will be inserted in "subreddit.txt" +# if it does not exist +defaultsub="linuxmemes" + +# If subreddit.txt does not exist, create it to prevent +# the program from not functioning properly +[ ! -f "$configdir/subreddit.txt" ] && echo "$defaultsub" >> "$configdir/subreddit.txt" + +# If no argument is passed +if [ -z "$subreddit" ]; then + # Ask the user to enter a subreddit + subreddit=$(dmenu -p "Select Subreddit r/" -i -l 10 < "$configdir/subreddit.txt" | awk -F "|" '{print $1}') + + # If no subreddit was chosen, exit + [ -z "$subreddit" ] && echo "no sub chosen" && exit 1 +fi + +# Default directory used to store the feed file and fetched images +cachedir="/tmp/redyt" + +# If cachedir does not exist, create it +if [ ! -d "$cachedir" ]; then + echo "$cachedir does not exist, creating..." + mkdir -p "$cachedir" +fi + +# Send a notification +[ $VERBOSE = 1 ] && $notifier "Redyt" "📩 Downloading your 🖼️ Memes" + +# Download the subreddit feed, containing only the +# first 100 entries (limit), and store it inside +# cachedir/tmp.json +curl -H "User-agent: 'your bot 0.1'" "https://www.reddit.com/r/$subreddit/hot.json?limit=${LIMIT:-100}" > "$cachedir/tmp.json" + +# Create a list of images +imgs=$(jq '.' < "$cachedir/tmp.json" | grep url_overridden_by_dest | grep -Eo "http(s|)://.*(jpg|png)\b" | sort -u) + +# If there are no images, exit +[ -z "$imgs" ] && $notifier "Redyt" "sadly, there are no images for subreddit $subreddit, please try again later!" && exit 1 + +# Download images to $cachedir +for img in $imgs; do + if [ ! -e "$cachedir/${img##*/}" ]; then + wget -P "$cachedir" $img + fi +done + +# Send a notification +[ $VERBOSE -eq 1 ] && $notifier "Redyt" "👍 Download Finished, Enjoy! 😊" +rm "$cachedir/tmp.json" + +# Display the images +if [ $FILTER = 1 ]; then + sxiv -a -o "$cachedir" +else + sxiv -a "$cachedir" +fi + +# Once finished, remove all of the cached images +[ ! $KEEP = 1 ] && rm "${cachedir:?}"/* |