#!/usr/bin/env bash link="https://source.unsplash.com/random/" dir="$HOME/.config/styli.sh" # https://www.reddit.com/r/Animewallpaper/search/.json?q=flair_name%3ADesktop&restrict_sr=1 reddit(){ useragent="thevinter" timeout=60 # TODO: Implement sort and top sort="hot" top_time="" while : do case "${1}" in -s | --search) search=${2} ; shift 2 ;; -f | --flair) flair=${2} ; shift 2 ;; -r | --sub) sub=${2} ; shift 2;; -- | '') shift; break ;; *) echo "Unexpected option: $1 - this should not happen." ; usage ;; esac done RED_PARSED_ARGUMENTS=$(getopt -a -n $0 -o f:s:r: --long flair:,search:,sub: -- "$@") RED_VALID_ARGUMENTS=$? if [ "$RED_VALID_ARGUMENTS" != "0" ]; then usage fi if [ -z $sub ] then readarray subreddits < "$dir/subreddits" a=${#subreddits[@]} b=$(($RANDOM % $a)) sub=${subreddits[$b]} sub="$(echo -e "${sub}" | tr -d '[:space:]')" fi if [ -z "$search" ] && [ -z "$flair" ] then url="https://www.reddit.com/r/$sub/$sort/.json?raw_json=1&t=$top_time" else if [ -z "$flair" ] then url="https://www.reddit.com/r/$sub/search/.json?q=$search&restrict_sr=1" elif [ -z "$search" ] then url="https://www.reddit.com/r/$sub/search/.json?q=flair_name%3A$flair&restrict_sr=1" else url="https://www.reddit.com/r/$sub/search/.json?q=$search+flair_name%3A$flair&restrict_sr=1" fi fi content=`wget -T $timeout -U "$useragent" -q -O - $url` # urls=$(echo -n "$content"| jq -r '.data.children[]|select(.data.post_hint|test("image")?) | .data.preview.images[0].source.url') urls=$(echo -n "$content"| jq -r '.data.children[]|select(.data.post_hint|test("image")?) | .data.url') names=$(echo -n "$content"| jq -r '.data.children[]|select(.data.post_hint|test("image")?) | .data.title') ids=$(echo -n "$content"| jq -r '.data.children[]|select(.data.post_hint|test("image")?) | .data.id') arrURLS=($urls) arrNAMES=($names) arrIDS=($ids) wait # prevent spawning too many processes size=${#arrURLS[@]} idx=$(($RANDOM % $size)) target_url=${arrURLS[$idx]} target_name=${arrNAMES[$idx]} target_id=${arrIDS[$idx]} ext=`echo -n "${target_url##*.}"|cut -d '?' -f 1` newname=`echo $target_name | sed "s/^\///;s/\// /g"`_"$subreddit"_$target_id.$ext wget -T $timeout -U "$useragent" --no-check-certificate -q -P down -O "$dir/wallpaper.jpg" $target_url &>/dev/null } usage(){ echo "Usage: styli.sh [-s | --search ] [-f | --flair ] [-h | --height ] [-w | --width ] [-b | --fehbg ] [-c | --fehopt ] [-r | --subreddit ] [-l | --link ] [-p | --termcolor]" exit 2 } if [ ! -d "$dir" ] then mkdir -p "$dir" fi pywal=0 PARSED_ARGUMENTS=$(getopt -a -n $0 -o f:h:w:s:l:b:r:c:p --long flair:,search:,hight:,width:,fehbg:,fehopt:,subreddit:,termcolor -- "$@") VALID_ARGUMENTS=$? if [ "$VALID_ARGUMENTS" != "0" ]; then usage fi while : do case "${1}" in -b | --fehbg) bgtype=${2} ; shift 2 ;; -s | --search) search=${2} ; shift 2 ;; -f | --flair) flair=${2} ; shift 2 ;; -h | --height) height=${2} ; shift 2 ;; -w | --width) width=${2} ; shift 2 ;; -l | --link) link=${2} ; shift 2 ;; -r | --subreddit) sub=${2} ; shift 2 ;; -c | --fehopt) custom=${2} ; shift 2 ;; -p | --termcolor) pywal=1 ; shift ;; -- | '') shift; break ;; *) echo "Unexpected option: $1 - this should not happen." ; usage ;; esac done feh=(feh) if [ ! -z $bgtype ]; then if [ $bgtype == 'bg-center' ]; then feh+=(--bg-center) fi if [ $bgtype == 'bg-fill' ]; then feh+=(--bg-fill) fi if [ $bgtype == 'bg-max' ]; then feh+=(--bg-max) fi if [ $bgtype == 'bg-scale' ]; then feh+=(--bg-scale) fi if [ $bgtype == 'bg-tile' ]; then feh+=(--bg-tile) fi else feh+=(--bg-scale) fi if [ ! -z $custom ]; then feh+=($custom) fi if [ $link = "reddit" ] || [ ! -z $sub ] then if [ ! -z $flair ] then reddit -f "$flair" else reddit -r "$sub" fi feh+=("$dir/wallpaper.jpg") "${feh[@]}" if [ $pywal -eq 1 ]; then wal -c wal -i "$dir/wallpaper.jpg" -n fi else if [ ! -z $height ] || [ ! -z $width ]; then link="${link}${width}x${height}"; else link="${link}1920x1080"; fi if [ ! -z $search ] then link="${link}/?${search}" fi wget -q -O "$dir/wallpaper" $link feh+=("$dir/wallpaper") "${feh[@]}" if [ $pywal -eq 1 ]; then wal -c wal -i "$dir/wallpaper" -n fi fi