Skip to content

Instantly share code, notes, and snippets.

@dcorto
Created July 30, 2025 15:15
Show Gist options
  • Select an option

  • Save dcorto/ff9d3856e7823b56b7ee73ddb94da99d to your computer and use it in GitHub Desktop.

Select an option

Save dcorto/ff9d3856e7823b56b7ee73ddb94da99d to your computer and use it in GitHub Desktop.

Revisions

  1. dcorto created this gist Jul 30, 2025.
    199 changes: 199 additions & 0 deletions apod_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,199 @@
    #!/bin/bash
    # Based on the script by Josh Schreuder (https://gist.github.com/JoshSchreuder/882666)
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in
    # all copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    # THE SOFTWARE.
    #
    #
    # This script gets the today APOD and this description and sets as wallaper
    # for XFCE (tested on linux mint 22.1).
    #
    #
    # ********************************
    # *** OPTIONS
    # ********************************
    # Set this to 'yes' to save a description (to ~/description.txt) from APOD page
    GET_DESCRIPTION="no"

    # Set this to 'yes' to save a titled version of the image (require imagemagick installed)
    SAVE_TITLED="yes"

    # Set this to the directory you want pictures saved
    PICTURES_DIR=~/Imágenes/APOD

    # Set this to the directory you want description saved
    DESCRIPTION_DIR=~/Imágenes/APOD

    # Set the xfce4-desktop config option. For get the correct value, run the command:
    # xfconf-query -c xfce4-desktop -m
    # This command will monitor changes on the config, so change the desktop background as usually and see what changes
    XFCE4_DESKTOP_OPTION=/backdrop/screen0/monitorLVDS-1/workspace0/last-image

    # ********************************
    # *** FUNCTIONS
    # ********************************
    function get_page {
    echo "Downloading page to find image"
    wget https://apod.nasa.gov/apod/ --quiet -O /tmp/apod.html
    grep -m 1 jpg /tmp/apod.html | sed -e 's/<//' -e 's/>//' -e 's/.*=//' -e 's/"//g' -e 's/^/https:\/\/apod.nasa.gov\/apod\//' > /tmp/pic_url
    }

    function save_description {
    if [ ${GET_DESCRIPTION} == "yes" ]; then
    echo "Getting description from page"
    # Get description
    if [ -e $DESCRIPTION_DIR/description.txt ]; then
    rm $DESCRIPTION_DIR/description.txt
    fi

    if [ ! -e /tmp/apod.html ]; then
    get_page
    fi

    echo "Parsing description"
    sed -n '/<b> Explanation: <\/b>/,/<p> <center>/p' /tmp/apod.html |
    sed -e :a -e 's/<[^>]*>//g;/</N;//ba' |
    grep -Ev 'Explanation:' |
    tr '\n' ' ' |
    sed 's/ /\n\n/g' |
    awk 'NF { print $0 "\n" }' |
    sed 's/^[ \t]*//' |
    sed 's/[ \t]*$//' > $DESCRIPTION_DIR/description.txt
    fi
    }

    function save_titled {
    if [ ${SAVE_TITLED} == "yes" ]; then
    echo "Getting title from page"
    # Get title
    if [ ! -e /tmp/apod.html ]; then
    get_page
    fi

    echo "Parsing title"
    local title=$(cat /tmp/apod.html | grep -m 1 '<b> .* </b>' | sed -e 's/<[^>]*>//g' -e 's/^[ \t]*//;s/[ \t]*$//')

    if [ -z "$title" ]; then
    echo "Error: No se pudo obtener el título."
    return
    fi

    # gets the image width
    local width=$(identify -format '%w' "$PICTURES_DIR/${TODAY}_apod.jpg")

    # create temporal image with the title
    convert -size ${width}x -background '#0008' -fill white -pointsize 20 \
    -gravity center -font "DejaVu-Sans-Mono" "label:$title" \
    "/tmp/temp_title_apod.png"

    # compose the final titled image
    convert "$PICTURES_DIR/${TODAY}_apod.jpg" "/tmp/temp_title_apod.png" \
    -gravity south -geometry +0+64 -composite "$PICTURES_DIR/${TODAY}_apod_titled.jpg"
    fi
    }

    function clean_up {
    # Clean up
    echo "Cleaning up temporary files"
    if [ -e "/tmp/pic_url" ]; then
    rm /tmp/pic_url
    fi

    if [ -e "/tmp/apod.html" ]; then
    rm /tmp/apod.html
    fi

    if [ -e "/tmp/temp_title_apod.png" ]; then
    rm /tmp/temp_title_apod.png
    fi
    }

    # ********************************
    # *** MAIN
    # ********************************
    echo "===================="
    echo "== APOD Wallpaper =="
    echo "===================="
    # Set date
    TODAY=$(date +'%Y%m%d')

    # If we don't have the image already today
    if [ ! -e $PICTURES_DIR/${TODAY}_apod.jpg ]; then
    echo "We don't have the picture saved, save it"

    get_page

    # Got the link to the image
    PICURL=`/bin/cat /tmp/pic_url`

    echo "Picture URL is: ${PICURL}"

    echo "Downloading image"
    wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_apod.jpg

    echo "Setting image as wallpaper"
    if [ ${SAVE_TITLED} == "yes" ]; then
    save_titled
    xfconf-query -c xfce4-desktop -p $(xfconf-query -c xfce4-desktop -l | grep $XFCE4_DESKTOP_OPTION) -s $PICTURES_DIR/${TODAY}_apod_titled.jpg
    else
    xfconf-query -c xfce4-desktop -p $(xfconf-query -c xfce4-desktop -l | grep $XFCE4_DESKTOP_OPTION) -s $PICTURES_DIR/${TODAY}_apod.jpg
    fi

    save_description

    # Else if we have it already, check if it's the most updated copy
    else
    get_page

    # Got the link to the image
    PICURL=`/bin/cat /tmp/pic_url`

    echo "Picture URL is: ${PICURL}"

    # Get the filesize
    SITEFILESIZE=$(LC_ALL=C wget --spider $PICURL 2>&1 | grep Length | awk '{print $2}')
    FILEFILESIZE=$(stat -c %s $PICTURES_DIR/${TODAY}_apod.jpg)

    # If the picture has been updated
    if [ "$SITEFILESIZE" != "$FILEFILESIZE" ]; then
    echo "The picture has been updated, getting updated copy"
    rm $PICTURES_DIR/${TODAY}_apod.jpg

    # Got the link to the image
    PICURL=`/bin/cat /tmp/pic_url`

    echo "Downloading image"
    wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_apod.jpg

    echo "Setting image as wallpaper"
    if [ ${SAVE_TITLED} == "yes" ]; then
    save_titled
    xfconf-query -c xfce4-desktop -p $(xfconf-query -c xfce4-desktop -l | grep $XFCE4_DESKTOP_OPTION) -s $PICTURES_DIR/${TODAY}_apod_titled.jpg
    else
    xfconf-query -c xfce4-desktop -p $(xfconf-query -c xfce4-desktop -l | grep $XFCE4_DESKTOP_OPTION) -s $PICTURES_DIR/${TODAY}_apod.jpg
    fi

    save_description

    # If the picture is the same
    else
    echo "Picture is the same, finishing up"
    fi
    fi

    clean_up