Add logging to the NASA APOD download script

This commit is contained in:
Ohad Livne 2025-07-18 17:02:33 +03:00
parent ec2016c6d1
commit c352ad925c
Signed by: libohad-dev
GPG key ID: 34FDC68B51191A4D

View file

@ -13,10 +13,16 @@ WALLPAPERS="$(systemd-path user)"/Pictures/nasa
mkdir --parents "${WALLPAPERS}" mkdir --parents "${WALLPAPERS}"
APOD_URL="https://api.nasa.gov/planetary/apod?api_key=${API_KEY}" APOD_URL="https://api.nasa.gov/planetary/apod?api_key=${API_KEY}"
echo "Fetching data from API"
response="$(curl --silent "${APOD_URL}")" response="$(curl --silent "${APOD_URL}")"
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_date="$(printf '%s' "${response}" | jq --raw-output .date)"
picture_url="$(printf '%s' "${response}" | jq --raw-output .hdurl)" picture_url="$(printf '%s' "${response}" | jq --raw-output .hdurl)"
echo "Generating output filename"
extension="${picture_url##*.}" extension="${picture_url##*.}"
filename="${picture_date}.${extension}" filename="${picture_date}.${extension}"
outfile="${WALLPAPERS}/${filename}" outfile="${WALLPAPERS}/${filename}"
@ -24,18 +30,23 @@ outfile="${WALLPAPERS}/${filename}"
if [ -f "${outfile}" ]; then if [ -f "${outfile}" ]; then
echo "Target file already exists" echo "Target file already exists"
else else
echo "Creating temporary file"
tempfile="$(mktemp)" tempfile="$(mktemp)"
echo "Fetching the daily picture"
curl \ curl \
--location \ --location \
--output "${tempfile}" \ --output "${tempfile}" \
--silent \ --silent \
"${picture_url}" "${picture_url}"
echo "Transaction-like file update"
mv "${tempfile}" "${outfile}" mv "${tempfile}" "${outfile}"
fi fi
echo "Updating the wallpaper image"
ln \ ln \
--force \ --force \
--symbolic \ --symbolic \
"${outfile}" \ "${outfile}" \
"${WALLPAPERS}/today" "${WALLPAPERS}/today"
echo "Refreshing the desktop background"
swaymsg output "*" bg "${SWAY_WALLPAPER}" fill swaymsg output "*" bg "${SWAY_WALLPAPER}" fill