Skip to content

Instantly share code, notes, and snippets.

@stilliard
Last active November 20, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stilliard/9f6b91ded8aab088b7e5f96c67d18191 to your computer and use it in GitHub Desktop.
Save stilliard/9f6b91ded8aab088b7e5f96c67d18191 to your computer and use it in GitHub Desktop.
nasa apod background - xubuntu / XFCE
#!/bin/bash
#
# Nasa Image Of The Day As Background
# Originally inspired by https://github.com/stblackbird/nasa-apod-background
#
# config
RESOLUTION_X=1360
RESOLUTION_Y=768
# location of image
NASA_SITE="https://apod.nasa.gov/apod/"
IMAGE_PATH="$HOME/.backgrounds/nasa/today_"
# remove previous image
rm -f "${IMAGE_PATH}"*
# download latest image html page
HTML=$(curl -s $NASA_SITE)
if [ -z "$HTML" ]; then
exit;
fi
# grab the path to the latest image
IMAGE_URL=$(echo "$HTML" | grep -oPa '(?<=<a href="image/)(.*?)(?=")' | head -n1 )
if [ -z "$IMAGE_URL" ]; then
exit;
fi
# add image file name to end of current path
IMAGE_FILE_NAME=$(echo "$IMAGE_URL" | grep -oP '(?<=/)(.*?)$' )
IMAGE_PATH="${IMAGE_PATH}${IMAGE_FILE_NAME}"
if [ -z "$IMAGE_FILE_NAME" ]; then
exit;
fi
# download the image
curl -sf "${NASA_SITE}image/${IMAGE_URL}" -o "$IMAGE_PATH"
if [ "$?" != 0 ]; then
# fallback to try <img> instead
IMAGE_URL=$(echo "$HTML" | grep -oPa '(?<=<IMG SRC="image/)(.*?)(?=")' | head -n1 )
if [ -z "$IMAGE_URL" ]; then
exit;
fi
curl -sf "${NASA_SITE}image/${IMAGE_URL}" -o "$IMAGE_PATH"
# still no lock, error
if [ "$?" != 0 ]; then
echo "curl error: ${NASA_SITE}image/${IMAGE_URL}"
exit
fi
fi
# scale image
convert "$IMAGE_PATH" -resize "${RESOLUTION_X}x${RESOLUTION_Y}" -background black -gravity center -extent "${RESOLUTION_X}x${RESOLUTION_Y}" "$IMAGE_PATH"
# grab title
IMAGE_TEXT=$(echo -e "$HTML" | grep -oPz '(?<=<center>\n\n<b> )(.*?)(?=</b> <br>)')
# add title text to top of image (http://www.imagemagick.org/Usage/annotating/)
convert "$IMAGE_PATH" -background black -fill white \
label:" Astronomy Picture of the Day: $IMAGE_TEXT " \
+swap -gravity Center -append "$IMAGE_PATH"
# fix being able to run this as cron...
# from: https://github.com/stblackbird/nasa-apod-background/blob/master/set_nasa_apod.sh
PID=$(pgrep xfce4-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | tr '\0' '\n' | cut -d= -f2-)
# set image as background
#xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitoreDP1/workspace0/last-image --set "$IMAGE_PATH"
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/last-image --set "$IMAGE_PATH"
# and on external monitor
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor1/workspace0/last-image --set "$IMAGE_PATH"
# done :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment