summaryrefslogtreecommitdiff
path: root/.scripts/redeat
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2022-03-12 19:54:51 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2022-03-12 19:54:51 +0530
commit54b772cae06eb25adf33297e35234f33ded81a7b (patch)
treef287ee48faa8013e4a8f0ee764a6d3cd850bb24d /.scripts/redeat
parent7197e45d94dbcf4877d585fff8e634896876ae83 (diff)
neovim addition
Diffstat (limited to '.scripts/redeat')
-rwxr-xr-x.scripts/redeat108
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:?}"/*