use more features form the wallhaven api

This commit is contained in:
Adithya 2023-10-13 21:38:15 +05:30
parent df0eab2e22
commit 1584b8dce7
Signed by: adtya
GPG key ID: 48FC9915FFD326D0
2 changed files with 60 additions and 34 deletions

View file

@ -65,7 +65,7 @@ _: {
files = [ files = [
".config/gh/hosts.yml" ".config/gh/hosts.yml"
".config/wallpaper_config" ".config/wallpaper_config.json"
]; ];
}; };
} }

View file

@ -2,42 +2,68 @@
set -eu set -eu
DIR="${1:-/tmp}" WALLHAVEN_BASE_URL="https://wallhaven.cc/api/v1"
mkdir -p "$DIR"
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}"
if [ ! -e $CONFIG_DIR/wallpaper_config ]; then
echo '{"tags":null,"categories":"100","purity":"100"}' | jq > $CONFIG_DIR/wallpaper_config
fi
TAGS="$(cat $CONFIG_DIR/wallpaper_config | jq -r '.tags // empty')"
if [ -n "$TAGS" ]; then
TAGS="q=$TAGS&"
fi
CATEGORIES="$(cat $CONFIG_DIR/wallpaper_config | jq -r '.categories // empty')"
if [ -n "$CATEGORIES" ]; then
CATEGORIES="categories=$CATEGORIES&"
fi
PURITY="$(cat $CONFIG_DIR/wallpaper_config | jq -r '.purity // empty')"
if [ -n "$PURITY" ]; then
PURITY="purity=$PURITY&"
fi
WALLHAVEN_API_KEY="$(secret-tool lookup application wallhaven-api-key)" WALLHAVEN_API_KEY="$(secret-tool lookup application wallhaven-api-key)"
if [ -n "$WALLHAVEN_API_KEY" ]; then if [ -n "${WALLHAVEN_API_KEY}" ]; then
WALLHAVEN_API_KEY="apikey=$WALLHAVEN_API_KEY&" API_KEY_HEADER="-H \"X-API-Key: ${WALLHAVEN_API_KEY}\""
fi
CURL_BASE_CMD="curl --silent ${API_KEY_HEADER}"
DIR="${1:-/tmp}"
mkdir -p "${DIR}"
CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}"
if [ ! -e ${CONFIG_DIR}/wallpaper_config.json ]; then
echo '{"tags":null,"categories":"100","purity":"100", "sorting":"random", "size":null, "ratios":null, "colors":null}' | jq > ${CONFIG_DIR}/wallpaper_config.json
fi
TAGS="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.tags // empty')"
if [ -n "${TAGS}" ]; then
TAGS="q=$(echo ${TAGS} | sed -e 's/ /%20/g' -e 's/+/%2B/g' -e 's/-/%2D/g')&"
fi
CATEGORIES="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.categories // empty')"
if [ -n "${CATEGORIES}" ]; then
CATEGORIES="categories=${CATEGORIES}&"
fi
PURITY="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.purity // empty')"
if [ -n "${PURITY}" ]; then
PURITY="purity=${PURITY}&"
fi
SIZE="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.size // empty')"
if [ -n "${SIZE}" ]; then
SIZE="atleast=${SIZE}&"
fi
RATIOS="$(cat $CONFIG_DIR/wallpaper_config.json | jq -r '.ratios // empty')"
if [ -n "${RATIOS}" ]; then
RATIOS="ratios=${RATIOS}&"
fi
COLORS="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.colors // empty')"
if [ -n "${COLORS}" ]; then
COLORS="colors=${COLORS}&"
fi
SORTING="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.sorting // empty')"
if [ -n "${SORTING}" ]; then
SORTING="sorting=${SORTING}&"
fi fi
notify-send -r 9897 -i information -t 1000 "Wallpapers" "Downloading..." notify-send -r 9897 -i information -t 1000 "Wallpapers" "Downloading..."
URL="https://wallhaven.cc/api/v1/search?${WALLHAVEN_API_KEY}${TAGS}${CATEGORIES}${PURITY}atleast=3840x2160&ratios=16x9&sorting=random" URL="${WALLHAVEN_BASE_URL}/search?${TAGS}${CATEGORIES}${PURITY}${SIZE}${RATIOS}${COLORS}${SORTING}"
ID="$(curl --silent "$URL" | jq -r '.data[0].id')" CURL_CMD="${CURL_BASE_CMD} \"${URL}\""
IMAGE_META=$(curl --silent "https://wallhaven.cc/api/v1/w/$ID?${WALLHAVEN_API_KEY}") ID="$(eval ${CURL_CMD} | jq -r '.data[0].id')"
IMAGE_URL="$(echo "$IMAGE_META" | jq -r '.data.path')" CURL_CMD="${CURL_BASE_CMD} \"${WALLHAVEN_BASE_URL}/w/${ID}\""
IMAGE_ID="$(echo "$IMAGE_META" | jq -r '.data.id')" IMAGE_META=$(eval ${CURL_CMD})
FILENAME="wallhaven-$IMAGE_ID" IMAGE_URL="$(echo "${IMAGE_META}" | jq -r '.data.path')"
curl --silent -L --output "/tmp/$FILENAME" "$IMAGE_URL" IMAGE_ID="$(echo "${IMAGE_META}" | jq -r '.data.id')"
convert "/tmp/$FILENAME" "$DIR/$FILENAME.jpg" FILENAME="wallhaven-${IMAGE_ID}"
echo "$DIR/$FILENAME.jpg" curl --silent -L --output "/tmp/${FILENAME}" "${IMAGE_URL}"
convert "/tmp/${FILENAME}" "${DIR}/${FILENAME}.jpg"
echo "${DIR}/${FILENAME}.jpg"