refactor the script

This commit is contained in:
Adithya 2023-10-15 13:12:42 +05:30
parent b671219992
commit 47ec309886
Signed by: adtya
GPG key ID: 48FC9915FFD326D0

View file

@ -15,51 +15,54 @@ DIR="${1:-/tmp}"
mkdir -p "${DIR}" mkdir -p "${DIR}"
CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}" CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}"
if [ ! -e ${CONFIG_DIR}/wallpaper_config.json ]; then CONFIG_FILE="${CONFIG_DIR}/wallpaper_config.json"
echo '{"tags":null,"categories":"100","purity":"100", "sorting":"random", "size":null, "ratios":null, "colors":null, "ai_filter":1, "range": "1M"}' | jq > ${CONFIG_DIR}/wallpaper_config.json if [ ! -e ${CONFIG_FILE} ]; then
printf '{"tags":null,"categories":"100","purity":"100", "sorting":"random", "size":null, "ratios":null, "colors":null, "ai_filter":1, "range": "1M"}' | jq > ${CONFIG_FILE}
fi fi
AI_FILTER="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.ai_filter // empty')" CONFIG="$(cat "${CONFIG_FILE}")"
AI_FILTER="$(printf "${CONFIG}" | jq -r '.ai_filter // empty')"
if [ -n "${AI_FILTER}" ]; then if [ -n "${AI_FILTER}" ]; then
AI_FILTER="ai_art_filter=${AI_FILTER}&" AI_FILTER="ai_art_filter=${AI_FILTER}&"
fi fi
TAGS="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.tags // empty')" TAGS="$(printf "${CONFIG}" | jq -r '.tags // empty')"
if [ -n "${TAGS}" ]; then if [ -n "${TAGS}" ]; then
TAGS="q=$(echo ${TAGS} | sed -e 's/ /%20/g' -e 's/+/%2B/g' -e 's/-/%2D/g')&" TAGS="q=$(echo ${TAGS} | sed -e 's/ /%20/g' -e 's/+/%2B/g' -e 's/-/%2D/g')&"
fi fi
CATEGORIES="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.categories // empty')" CATEGORIES="$(printf "${CONFIG}" | jq -r '.categories // empty')"
if [ -n "${CATEGORIES}" ]; then if [ -n "${CATEGORIES}" ]; then
CATEGORIES="categories=${CATEGORIES}&" CATEGORIES="categories=${CATEGORIES}&"
fi fi
PURITY="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.purity // empty')" PURITY="$(printf "${CONFIG}" | jq -r '.purity // empty')"
if [ -n "${PURITY}" ]; then if [ -n "${PURITY}" ]; then
PURITY="purity=${PURITY}&" PURITY="purity=${PURITY}&"
fi fi
SIZE="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.size // empty')" SIZE="$(printf "${CONFIG}" | jq -r '.size // empty')"
if [ -n "${SIZE}" ]; then if [ -n "${SIZE}" ]; then
SIZE="atleast=${SIZE}&" SIZE="atleast=${SIZE}&"
fi fi
RATIOS="$(cat $CONFIG_DIR/wallpaper_config.json | jq -r '.ratios // empty')" RATIOS="$(printf "${CONFIG}" | jq -r '.ratios // empty')"
if [ -n "${RATIOS}" ]; then if [ -n "${RATIOS}" ]; then
RATIOS="ratios=${RATIOS}&" RATIOS="ratios=${RATIOS}&"
fi fi
COLORS="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.colors // empty')" COLORS="$(printf "${CONFIG}" | jq -r '.colors // empty')"
if [ -n "${COLORS}" ]; then if [ -n "${COLORS}" ]; then
COLORS="colors=${COLORS}&" COLORS="colors=${COLORS}&"
fi fi
SORTING="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.sorting // empty')" SORTING="$(printf "${CONFIG}" | jq -r '.sorting // empty')"
if [ -n "${SORTING}" ]; then if [ -n "${SORTING}" ]; then
SORTING="sorting=${SORTING}&" SORTING="sorting=${SORTING}&"
fi fi
RANGE="$(cat ${CONFIG_DIR}/wallpaper_config.json | jq -r '.range // empty')" RANGE="$(printf "${CONFIG}" | jq -r '.range // empty')"
if [ -n "${RANGE}" ]; then if [ -n "${RANGE}" ]; then
RANGE="topRange=${RANGE}&" RANGE="topRange=${RANGE}&"
fi fi
@ -70,8 +73,8 @@ CURL_CMD="${CURL_BASE_CMD} \"${URL}\""
ID="$(eval ${CURL_CMD} | jq -r '.data[0].id')" ID="$(eval ${CURL_CMD} | jq -r '.data[0].id')"
CURL_CMD="${CURL_BASE_CMD} \"${WALLHAVEN_BASE_URL}/w/${ID}\"" CURL_CMD="${CURL_BASE_CMD} \"${WALLHAVEN_BASE_URL}/w/${ID}\""
IMAGE_META=$(eval ${CURL_CMD}) IMAGE_META=$(eval ${CURL_CMD})
IMAGE_URL="$(echo "${IMAGE_META}" | jq -r '.data.path')" IMAGE_URL="$(printf "${IMAGE_META}" | jq -r '.data.path')"
IMAGE_ID="$(echo "${IMAGE_META}" | jq -r '.data.id')" IMAGE_ID="$(printf "${IMAGE_META}" | jq -r '.data.id')"
FILENAME="wallhaven-${IMAGE_ID}" FILENAME="wallhaven-${IMAGE_ID}"
curl --silent -L --output "${DIR}/${FILENAME}.image" "${IMAGE_URL}" curl --silent -L --output "${DIR}/${FILENAME}.image" "${IMAGE_URL}"
echo "${DIR}/${FILENAME}.image" echo "${DIR}/${FILENAME}.image"