2023-05-16 00:19:04 +05:30
#!/bin/sh
set -eu
2023-10-13 21:38:15 +05:30
WALLHAVEN_BASE_URL = "https://wallhaven.cc/api/v1"
WALLHAVEN_API_KEY = " $( secret-tool lookup application wallhaven-api-key) "
if [ -n " ${ WALLHAVEN_API_KEY } " ] ; then
API_KEY_HEADER = " -H \"X-API-Key: ${ WALLHAVEN_API_KEY } \" "
fi
CURL_BASE_CMD = " curl --silent ${ API_KEY_HEADER } "
2023-05-18 20:09:34 +05:30
DIR = " ${ 1 :- /tmp } "
2023-10-13 21:38:15 +05:30
mkdir -p " ${ DIR } "
2023-05-16 00:19:04 +05:30
2023-10-13 21:38:15 +05:30
CONFIG_DIR = " ${ XDG_CONFIG_HOME :- ${ HOME } /.config } "
2023-10-15 13:12:42 +05:30
CONFIG_FILE = " ${ CONFIG_DIR } /wallpaper_config.json "
if [ ! -e ${ CONFIG_FILE } ] ; then
2023-10-18 22:00:56 +05:30
echo '{"tags":null,"categories":"100","purity":"100", "sorting":"random", "size":null, "ratios":null, "colors":null, "ai_filter":1, "range": "1M"}' | jq > ${ CONFIG_FILE }
2023-10-13 22:04:13 +05:30
fi
2023-10-15 13:12:42 +05:30
CONFIG = " $( cat " ${ CONFIG_FILE } " ) "
2023-10-18 22:00:56 +05:30
AI_FILTER = " $( echo " ${ CONFIG } " | jq -r '.ai_filter // empty' ) "
2023-10-13 22:04:13 +05:30
if [ -n " ${ AI_FILTER } " ] ; then
AI_FILTER = " ai_art_filter= ${ AI_FILTER } & "
2023-10-13 13:23:05 +05:30
fi
2023-10-18 22:00:56 +05:30
TAGS = " $( echo " ${ CONFIG } " | jq -r '.tags // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ TAGS } " ] ; then
2023-10-15 13:46:37 +05:30
TAGS = " q= $( echo ${ TAGS } | sed -e 's/ /%20/g' -e 's/+/%2B/g' -e 's/-/%2D/g' -e 's/:/%3A/g' ) & "
2023-10-13 12:36:02 +05:30
fi
2023-10-18 22:00:56 +05:30
CATEGORIES = " $( echo " ${ CONFIG } " | jq -r '.categories // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ CATEGORIES } " ] ; then
CATEGORIES = " categories= ${ CATEGORIES } & "
2023-10-13 13:23:05 +05:30
fi
2023-10-18 22:00:56 +05:30
PURITY = " $( echo " ${ CONFIG } " | jq -r '.purity // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ PURITY } " ] ; then
PURITY = " purity= ${ PURITY } & "
2023-10-13 13:23:05 +05:30
fi
2023-10-18 22:00:56 +05:30
SIZE = " $( echo " ${ CONFIG } " | jq -r '.size // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ SIZE } " ] ; then
SIZE = " atleast= ${ SIZE } & "
fi
2023-10-18 22:00:56 +05:30
RATIOS = " $( echo " ${ CONFIG } " | jq -r '.ratios // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ RATIOS } " ] ; then
RATIOS = " ratios= ${ RATIOS } & "
fi
2023-10-18 22:00:56 +05:30
COLORS = " $( echo " ${ CONFIG } " | jq -r '.colors // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ COLORS } " ] ; then
COLORS = " colors= ${ COLORS } & "
fi
2023-10-18 22:00:56 +05:30
SORTING = " $( echo " ${ CONFIG } " | jq -r '.sorting // empty' ) "
2023-10-13 21:38:15 +05:30
if [ -n " ${ SORTING } " ] ; then
SORTING = " sorting= ${ SORTING } & "
2023-10-13 13:23:05 +05:30
fi
2023-10-18 22:00:56 +05:30
RANGE = " $( echo " ${ CONFIG } " | jq -r '.range // empty' ) "
2023-10-15 13:02:49 +05:30
if [ -n " ${ RANGE } " ] ; then
RANGE = " topRange= ${ RANGE } & "
fi
2024-01-04 21:51:22 +05:30
notify-send -r 1234 -u normal -a Wallpapers -i information -t 5000 "Wallpapers" "Fetching a list of wallpapers..."
2023-10-15 13:02:49 +05:30
URL = " ${ WALLHAVEN_BASE_URL } /search? ${ TAGS } ${ CATEGORIES } ${ PURITY } ${ SIZE } ${ RATIOS } ${ COLORS } ${ AI_FILTER } ${ SORTING } ${ RANGE } "
2023-10-13 21:38:15 +05:30
CURL_CMD = " ${ CURL_BASE_CMD } \" ${ URL } \" "
2023-10-15 13:42:02 +05:30
RESULT = " $( eval ${ CURL_CMD } ) "
2023-10-18 22:00:56 +05:30
NO_OF_IMAGES = " $( echo " ${ RESULT } " | jq -r '.meta.total' ) "
2023-10-15 22:27:32 +05:30
if [ " ${ NO_OF_IMAGES } " -eq 0 ] ; then
2024-01-04 21:51:22 +05:30
notify-send -r 1234 -u normal -a Wallpapers -i information -t 5000 "Wallpapers" "No images available for current configuration."
2023-10-15 22:27:32 +05:30
exit 1
fi
2023-10-15 21:49:04 +05:30
RANDOM_ITEM = " $( shuf -i 0-$(( NO_OF_IMAGES-1)) -n 1 --random-source= /dev/urandom) "
ITEM_PAGE = $(( RANDOM_ITEM/24))
ITEM_NUMBER = $(( RANDOM_ITEM%24))
if [ " ${ ITEM_PAGE } " -gt 0 ] ; then
2023-10-18 22:00:56 +05:30
SEED = " $( echo " ${ RESULT } " | jq -r '.meta.seed // empty' ) "
2023-10-15 22:27:11 +05:30
if [ -n " ${ SEED } " ] ; then
SEED = " seed= ${ SEED } & "
fi
CURL_CMD = " ${ CURL_BASE_CMD } \" ${ URL } ${ SEED } page= $(( ITEM_PAGE+1)) \" "
2023-10-15 21:49:04 +05:30
RESULT = " $( eval ${ CURL_CMD } ) "
fi
2023-10-18 22:00:56 +05:30
ID = " $( echo " ${ RESULT } " | jq -r " .data[ ${ ITEM_NUMBER } ].id " ) "
2024-01-04 21:51:22 +05:30
notify-send -r 1234 -u normal -a Wallpapers -i information -t 5000 "Wallpapers" " Got ${ NO_OF_IMAGES } images. Using image ${ ID } from page $(( ITEM_PAGE+1)) ... "
2023-10-18 22:00:56 +05:30
IMAGE_URL = " $( echo " ${ RESULT } " | jq -r " .data[ ${ ITEM_NUMBER } ].path " ) "
2023-10-15 14:18:20 +05:30
FILENAME = " ${ IMAGE_URL ##*/ } "
2024-01-04 21:51:22 +05:30
notify-send -r 1234 -u normal -a Wallpapers -i information -t 5000 "Wallpapers" " Downloading image: ${ IMAGE_URL } "
2023-10-15 14:18:20 +05:30
curl --silent -L --output-dir " ${ DIR } " -o " ${ FILENAME } " " ${ IMAGE_URL } "
2024-01-04 21:51:22 +05:30
notify-send -r 1234 -u normal -a Wallpapers -i information -t 5000 "Wallpapers" " Downloaded image: ${ DIR } / ${ FILENAME } "
2023-10-15 14:18:20 +05:30
echo " ${ DIR } / ${ FILENAME } "
2023-05-16 00:19:04 +05:30