#!/usr/bin/env bash link="https://source.unsplash.com/random/" if [ -z ${XDG_CONFIG_HOME+x} ]; then XDG_CONFIG_HOME="${HOME}/.config" fi if [ -z ${XDG_HOME+x} ]; then XDG_HOME="${HOME}" fi confdir="${XDG_CONFIG_HOME}/styli.sh" if [ ! -d "${confdir}" ]; then mkdir -p "${confdir}" fi cachedir="${XDG_HOME}/Pictures/wallpapers" if [ ! -d "${cachedir}" ]; then mkdir -p "${cachedir}" fi wallpaper="${cachedir}/wallpaper.jpg" wallpath="" die() { printf "ERR: %s\n" "$1" >&2 exit 1 } # https://github.com/egeesin/alacritty-color-export alacritty_change() { DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.yml # Wal generates a shell script that defines color0..color15 SRC="$HOME"/.cache/wal/colors.sh [ -e "$SRC" ] || die "Wal colors not found, exiting script. Have you executed Wal before?" printf "Colors found, source ready.\n" READLINK=$( command -v greadlink || command -v readlink ) # Get config file if [ -n "$1" ]; then [ -e "$1" ] || die "Selected config doesn't exist, exiting script." printf "Config found, destination ready.\n" CFG=$1 [ -L "$1" ] && { printf "Following symlink to config...\n" CFG=$($READLINK -f "$1") } else # Default config path in Mac systems [ -e "$DEFAULT_MACOS_CONFIG" ] || die "Alacritty config not found, exiting script." CFG="$DEFAULT_MACOS_CONFIG" [ -L "$DEFAULT_MACOS_CONFIG" ] && { printf "Following symlink to config...\n" CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG") } fi # Get hex colors from Wal cache # No need for shellcheck to check this, it comes from pywal # shellcheck disable=SC1090 . "$SRC" # Create temp file for sed results tempfile=$(mktemp) trap 'rm $tempfile' INT TERM EXIT # Delete existing color declarations generated by this script # If begin comment exists if grep -q '^# BEGIN ACE' "$CFG"; then # And if end comment exists if grep -q '^# END ACE' "$CFG"; then # Delete contents of the block printf "Existing generated colors found, replacing new colors...\n" sed '/^# BEGIN ACE/,/^# END ACE/ { /^# BEGIN ACE/! { /^# END ACE/!d; } }' "$CFG" > "$tempfile" \ && cat "$tempfile" > "$CFG" # If no end comment, don't do anything else die "No '# END ACE' comment found, please ensure it is present." fi # If no begin comment found else # Don't do anything and notify user if there's an end comment in the file ! grep -q '^# END ACE' "$CFG" || die "Found '# END ACE' comment, but no '# BEGIN ACE' comment found. Please ensure it is present." printf "There's no existing 'generated' colors, adding comments...\n"; printf '# BEGIN ACE\n# END ACE' >> "$CFG"; fi # Write new color definitions # We know $colorX is unset, we set it by sourcing above # shellcheck disable=SC2154 { sed "/^# BEGIN ACE/ r /dev/stdin" "$CFG" > "$tempfile" < "$CFG" \ && rm "$tempfile" trap - INT TERM EXIT printf "'%s' exported to '%s'\n" "$SRC" "$CFG" } reddit(){ useragent="thevinter" timeout=60 sort=$2 top_time=$3 if [ -z $sort ]; then sort="hot" fi if [ -z $top_time ]; then top_time="" fi if [ ! -z $1 ]; then sub=$1 else if [ ! -f "${confdir}/subreddits" ]; then echo "Please install the subreddits file in ${confdir}" exit 2 fi readarray subreddits < "${confdir}/subreddits" a=${#subreddits[@]} b=$(($RANDOM % $a)) sub=${subreddits[$b]} sub="$(echo -e "${sub}" | tr -d '[:space:]')" fi url="https://www.reddit.com/r/$sub/$sort/.json?raw_json=1&t=$top_time" 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') 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[@]} if [ $size -eq 0 ]; then echo The current subreddit is not valid. exit 1 fi 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 ${wallpaper} $target_url &>/dev/null } unsplash() { local search="${search// /_}" 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 ${wallpaper} $link } deviantart(){ client_id=16531 client_secret=68c00f3d0ceab95b0fac638b33a3368e payload="grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}" access_token=`curl --silent -d $payload https://www.deviantart.com/oauth2/token | jq -r '.access_token'` if [ ! -z $1 ]; then artist=$1 url="https://www.deviantart.com/api/v1/oauth2/gallery/?username=${artist}&mode=popular&limit=24" elif [ ! -z $search ]; then [[ "$search" =~ ^(tag:)(.*)$ ]] && tag=${BASH_REMATCH[2]} if [ ! -z $tag ]; then url="https://www.deviantart.com/api/v1/oauth2/browse/tags?tag=$tag&offset=${RANDOM:0:2}&limit=24" else url="https://www.deviantart.com/api/v1/oauth2/browse/popular?q=$search&limit=24&timerange=1month" fi else #url="https://www.deviantart.com/api/v1/oauth2/browse/hot?limit=24&offset=${offset}" topics=( "adoptables" "artisan-crafts" "anthro" "comics" "drawings-and-paintings" "fan-art" "poetry" "stock-images" "sculpture" "science-fiction" "traditional-art" "street-photography" "street-art" "pixel-art" "wallpaper" "digital-art" "photo-manipulation" "science-fiction" "fractal" "game-art" "fantasy" "3d" "drawings-and-paintings" "game-art" ) rand=$[$RANDOM % ${#topics[@]}] url="https://www.deviantart.com/api/v1/oauth2/browse/topic?limit=24&topic=${topics[$rand]}" fi content=`curl --silent -H "Authorization: Bearer ${access_token}" -H "Accept: application/json" -H "Content-Type: application/json" $url` urls=$(echo -n $content | jq -r '.results[].content.src') arrURLS=($urls) size=${#arrURLS[@]} idx=$(($RANDOM % $size)) target_url=${arrURLS[$idx]} wget --no-check-certificate -q -P down -O ${wallpaper} $target_url &>/dev/null } usage(){ echo "Usage: styli.sh [-s | --search ] [-h | --height ] [-w | --width ] [-b | --fehbg ] [-c | --fehopt ] [-a | --artist ] [-r | --subreddit ] [-l | --link ] [-p | --termcolor] [-d | --directory] [-k | --kde] [-x | --xfce] [-g | --gnome] [-m | --monitors ] [-n | --nitrogen] " exit 2 } type_check() { mime_types=("image/bmp" "image/jpeg" "image/gif" "image/png" "image/heic") isType=false for requiredType in "${mime_types[@]}" do if [ $dir ]; then imageType=$(file --mime-type "$HOME/Pictures/wallpaper_dump/${wallpaper}" | sed 's/.*image/image/') echo $imageType if [ "$requiredType" = "$imageType" ]; then isType=true break fi else imageType=$(file --mime-type ${wallpaper} | awk '{print $2}') if [ "$requiredType" = "$imageType" ]; then isType=true break fi fi done if [ $isType = false ]; then echo "MIME-Type missmatch. Downloaded file is not an image!" exit 1 fi } select_random_wallpaper () { wallpaper=$(ls $HOME/Pictures/wallpaper_dump | shuf -n 1) } pywal_cmd() { if [ $pywal -eq 1 ]; then wal -c wal -i ${wallpaper} -n -q if [ $TERM = alacritty ]; then alacritty_change fi fi } sway_cmd() { mode="fill" if [ ! -z $dir ]; then cp "$HOME/Pictures/wallpaper_dump/${wallpaper}" "$HOME/Pictures/wallpapers/wallpaper" swaymsg output "*" bg "$HOME/Pictures/wallpaper_dump/${wallpaper}" "${mode}" else swaymsg output "*" bg "${wallpaper}" "${mode}" fi } nitrogen_cmd() { for ((monitor=0; monitor < $monitors; monitor++)) do local nitrogen=(nitrogen --save --head=${monitor}) if [ ! -z $bgtype ]; then if [ $bgtype == 'bg-center' ]; then nitrogen+=(--set-centered) fi if [ $bgtype == 'bg-fill' ]; then nitrogen+=(--set-zoom-fill) fi if [ $bgtype == 'bg-max' ]; then nitrogen+=(--set-zoom) fi if [ $bgtype == 'bg-scale' ]; then nitrogen+=(--set-scaled) fi if [ $bgtype == 'bg-tile' ]; then nitrogen+=(--set-tiled) fi else nitrogen+=(--set-scaled) fi if [ ! -z $custom ]; then nitrogen+=($custom) fi nitrogen+=(${wallpaper}) "${nitrogen[@]}" done } kde_cmd() { if [ ! -z $dir ]; then qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript "var allDesktops = desktops();print (allDesktops);for (i=0;i