dotfiles/.local/bin/get-nasa-apod

41 lines
953 B
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)"/Wallpapers
mkdir --parents "${WALLPAPERS}"
APOD_URL="https://api.nasa.gov/planetary/apod?api_key=${API_KEY}"
response="$(curl --silent "${APOD_URL}")"
picture_date="$(printf '%s' "${response}" | jq --raw-output .date)"
picture_url="$(printf '%s' "${response}" | jq --raw-output .hdurl)"
extension="${picture_url##*.}"
filename="${picture_date}.${extension}"
outfile="${WALLPAPERS}/${filename}"
if [ -f "${outfile}" ]; then
echo "Target file already exists"
exit 0
fi
tempfile="$(mktemp)"
curl \
--location \
--output "${tempfile}" \
--silent \
"${picture_url}"
mv "${tempfile}" "${outfile}"
ln \
--force \
--symbolic \
"${outfile}" \
"${WALLPAPERS}/today"
swaymsg output "*" bg "${SWAY_WALLPAPER}" fill