├── README.md └── redyt /README.md: -------------------------------------------------------------------------------- 1 | # redyt 2 | 3 | Scrape Reddit images from the Terminal 4 | 5 | This shell script will automatically create the following folder: 6 | 7 | - `~/.config/redyt` 8 | 9 | Also, another file, containing the subreddits, will be created: 10 | 11 | - `~/.config/redyt/subreddit.txt` 12 | 13 | If the user does not modify this file, it will contain, by default, subreddit `linuxmemes`. 14 | However, this default subreddit can be changed by modifying the variable `defaultsub`. 15 | 16 | Note that you can either pass a subreddit as an argument (`./redyt [your-subreddit]`) 17 | or, from `dmenu`, type the name of another subreddit. 18 | 19 | Here's an example of a custom `subreddit.txt`: http://0x0.st/-rbq.txt 20 | 21 | Please note: you will need to install the following programs: 22 | - dmenu (User Input) 23 | - jq (JSON parsing) 24 | - sxiv (Image Viewing) 25 | 26 | `notify-send` is also recommended but, if not present, `echo` will be used as a notifier. 27 | 28 | ## Usage 29 | 30 | ```sh 31 | $ redyt [-l LIMIT] [-v] [-k] [SUBREDDIT] 32 | $ redyt [--limit LIMIT] [--verbose] [--keep] [SUBREDDIT] 33 | ``` 34 | 35 | ## Arguments 36 | 37 | * `-l LIMIT, --limit LIMIT` set the maximum number of files to be downloaded 38 | * `-k, --keep` don't remove files after quitting `sxiv` 39 | * `-v, --verbose` use notifier 40 | -------------------------------------------------------------------------------- /redyt: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Check if necessary programs are installed 4 | for prog in dmenu jq sxiv; do 5 | [ ! "$(which "$prog")" ] && echo "Please install $prog!" && exit 1 6 | done 7 | # If notify-send is not installed, use echo as notifier 8 | [ ! "$(which notify-send)" ] && notifier="echo" || notifier="notify-send" 9 | 10 | # args 11 | while [ $# -gt 0 ]; do 12 | case $1 in 13 | -l|--limit) 14 | shift 15 | LIMIT=$1 16 | case $LIMIT in 17 | ''|*[!0-9]*) 18 | echo 'limit is NaN' 19 | exit 1 20 | esac 21 | shift 22 | ;; 23 | -f|--filter) 24 | FILTER=1 25 | shift 26 | ;; 27 | -k|--keep) 28 | KEEP=1 29 | shift 30 | ;; 31 | -v|--verbose) 32 | VERBOSE=1 33 | shift 34 | ;; 35 | *) 36 | subreddit=$1 37 | shift 38 | ;; 39 | esac 40 | done 41 | 42 | # Default config directory 43 | configdir="${XDG_CONFIG_HOME:-$HOME/.config}/redyt" 44 | 45 | # Create .config/redyt if it does not exist to prevent 46 | # the program from not functioning properly 47 | [ ! -d "$configdir" ] && echo "Directory $configdir does not exist, creating..." && mkdir -p "$configdir" 48 | 49 | # Default subreddit that will be inserted in "subreddit.txt" 50 | # if it does not exist 51 | defaultsub="linuxmemes" 52 | 53 | # If subreddit.txt does not exist, create it to prevent 54 | # the program from not functioning properly 55 | [ ! -f "$configdir/subreddit.txt" ] && echo "$defaultsub" >> "$configdir/subreddit.txt" 56 | 57 | # If no argument is passed 58 | if [ -z "$subreddit" ]; then 59 | # Ask the user to enter a subreddit 60 | subreddit=$(dmenu -p "Select Subreddit r/" -i -l 10 < "$configdir/subreddit.txt" | awk -F "|" '{print $1}') 61 | 62 | # If no subreddit was chosen, exit 63 | [ -z "$subreddit" ] && echo "no sub chosen" && exit 1 64 | fi 65 | 66 | # Default directory used to store the feed file and fetched images 67 | cachedir="/tmp/redyt" 68 | 69 | # If cachedir does not exist, create it 70 | if [ ! -d "$cachedir" ]; then 71 | echo "$cachedir does not exist, creating..." 72 | mkdir -p "$cachedir" 73 | fi 74 | 75 | # Send a notification 76 | [ $VERBOSE -eq 1 ] && $notifier "Redyt" "📩 Downloading your 🖼️ Memes" 77 | 78 | # Download the subreddit feed, containing only the 79 | # first 100 entries (limit), and store it inside 80 | # cachedir/tmp.json 81 | curl -H "User-agent: 'your bot 0.1'" "https://www.reddit.com/r/$subreddit/hot.json?limit=${LIMIT:-100}" > "$cachedir/tmp.json" 82 | 83 | # Create a list of images 84 | imgs=$(jq '.' < "$cachedir/tmp.json" | grep url_overridden_by_dest | grep -Eo "http(s|)://.*(jpg|png)\b" | sort -u) 85 | 86 | # If there are no images, exit 87 | [ -z "$imgs" ] && $notifier "Redyt" "sadly, there are no images for subreddit $subreddit, please try again later!" && exit 1 88 | 89 | # Download images to $cachedir 90 | for img in $imgs; do 91 | if [ ! -e "$cachedir/${img##*/}" ]; then 92 | wget -P "$cachedir" $img 93 | fi 94 | done 95 | 96 | # Send a notification 97 | [ $VERBOSE -eq 1 ] && $notifier "Redyt" "👍 Download Finished, Enjoy! 😊" 98 | rm "$cachedir/tmp.json" 99 | 100 | # Display the images 101 | if [ $FILTER -eq 1 ]; then 102 | sxiv -a -o "$cachedir" 103 | else 104 | sxiv -a "$cachedir" 105 | fi 106 | 107 | # Once finished, remove all of the cached images 108 | [ ! $KEEP -eq 1 ] && rm "${cachedir:?}"/* 109 | --------------------------------------------------------------------------------