54 lines
1.5 KiB
Bash
Executable file
54 lines
1.5 KiB
Bash
Executable file
#! /usr/bin/bash
|
|
|
|
set -euo pipefail
|
|
IFS=$'\n\t'
|
|
|
|
API_KEY="${NASA_API_KEY:-DEMO_KEY}"
|
|
if [ "${API_KEY}" = "DEMO_KEY" ]; then
|
|
echo "No API key set! using demo key."
|
|
fi
|
|
|
|
WALLPAPERS="$(systemd-path user)"/Pictures/nasa
|
|
|
|
mkdir --parents "${WALLPAPERS}"
|
|
|
|
APOD_URL="https://api.nasa.gov/planetary/apod?api_key=${API_KEY}"
|
|
echo "Fetching data from API"
|
|
response="$(curl --silent --write-out '{"rate-limit": %header{x-ratelimit-remaining}}' "${APOD_URL}" | jq --slurp add)"
|
|
echo "Received response from API"
|
|
# shellcheck disable=SC2005
|
|
echo "$(printf '%s' "${response}" | jq '.')"
|
|
echo "Parsing data"
|
|
picture_date="$(printf '%s' "${response}" | jq --raw-output .date)"
|
|
picture_url="$(printf '%s' "${response}" | jq --raw-output .hdurl)"
|
|
rate_remaining="$(printf '%s' "${response}" | jq '."rate-limit"')"
|
|
echo "Remaining requests until rate limit: ${rate_remaining}"
|
|
|
|
echo "Generating output filename"
|
|
extension="${picture_url##*.}"
|
|
filename="${picture_date}.${extension}"
|
|
outfile="${WALLPAPERS}/${filename}"
|
|
|
|
if [ -f "${outfile}" ]; then
|
|
echo "Target file already exists"
|
|
else
|
|
echo "Creating temporary file"
|
|
tempfile="$(mktemp)"
|
|
echo "Fetching the daily picture"
|
|
curl \
|
|
--location \
|
|
--output "${tempfile}" \
|
|
--silent \
|
|
"${picture_url}"
|
|
echo "Transaction-like file update"
|
|
mv "${tempfile}" "${outfile}"
|
|
fi
|
|
|
|
echo "Updating the wallpaper image"
|
|
ln \
|
|
--force \
|
|
--symbolic \
|
|
"${outfile}" \
|
|
"${WALLPAPERS}/today"
|
|
echo "Refreshing the desktop background"
|
|
swaymsg output "*" bg "${SWAY_WALLPAPER}" fill
|