dotfiles/bin/apod

84 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# apod.sh largely based on
# https://gist.github.com/mtking2/c38948f731d9017fee4edb36667c2a70
http_proxy=""
green="\033[32m"
white="\033[0m"
blue="\033[34m"
path="$HOME/Pictures/apod"
mkdir -p $path
rm -f $path/*.html
echo -e "$(date "+%Y-%m-%d %H:%M:%S") get apod started ${green}${white}"
wget -q https://apod.nasa.gov/apod/ap$(date +%y%m%d).html --content-on-error -P $path
index=$(find $path -type f -name "*.html")
echo -e "${blue}try \"\$(grep -i \"IMG SRC=\" $index | cut -d\\\" -f2)\":${white} $(grep -i "IMG SRC=" $index || echo "is null")"
echo -e "${blue}try \"\$(grep -i \"a href=\"image\" $index | cut -d\\\" -f2)\":${white} $(grep -i "a href=\"image" $index || echo "is null")"
get_img() {
img="$(grep -i "a href=\"image" $index | cut -d\" -f2 | head -1 | xargs)"
if [[ -z "$img" ]]; then
img="$(grep -i "IMG SRC=" $index | cut -d\" -f2 | head -1 | xargs)"
fi
}
get_img
# sometimes the APOD is an iframe containing a video/flash/js element
# this condition should detect the absence of an image and use the previous APOD
if [[ -z "$img" ]]; then
yesterday="https://apod.nasa.gov/apod/ap$(date --date yesterday +%y%m%d).html"
echo -e "${blue}Using yesterday's image:${white} $yesterday"
wget -q $yesterday -O $index
get_img
fi
img_name="$(echo "$img" | grep image | head -1 | cut -d\/ -f3)"
# deal with external images
if [[ $img =~ ^https?://.*$ ]]; then
url=$img
else
url="https://apod.nasa.gov/apod/$img"
fi
apod_title=$(grep "APOD:" $index | sed 's/<\/\?[^>]\+>//g' | sed 's/ APOD: //' | head -1)
if [[ "$apod_title" =~ ^Astronomy.*$ ]]; then
apod_title=$(echo $img_name | sed 's/....$//' )
fi
apod_date=$(grep "^$(date +%Y\ %B)" $index | xargs | head -1 | sed 's/ /-/g')
echo -e "${blue}path:${white} $path"
echo -e "${blue}apod_title:${white} $apod_title"
echo -e "${blue}img:${white} $img"
echo -e "${blue}img_name:${white} "$img_name""
echo -e "${blue}url:${white} $url"
echo -e "${blue}apod_date:${white} $apod_date"
filename=$(echo $apod_title | xargs | sed 's/- //g' | sed "s/ /-/g").jpg
wget -q $url -O "$path/$filename"
# GNOME
# possible values for gsettings: "wallpaper", "centered", "scaled", "stretched", "zoom", "spanned"
# gsettings set org.gnome.desktop.background picture-uri "File://$path/wallpaper.jpg"
# gsettings set org.gnome.desktop.background picture-options zoom
# Cinnamon
#gsettings set org.cinnamon.desktop.background picture-uri "file://$path/$apod_date-$short_title-$img_name"
# gsettings set org.cinnamon.desktop.background picture-uri "file://$path/$filename"
# gsettings set org.cinnamon.desktop.background picture-options scaled
# Xfce
# xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/image-path --set $path/wallpaper.jpg
# feh
# feh --bg-scale $path/wallpaper.jpg
xdg-open "$path/$filename" 2>&1 >/dev/null &
rm -f $index
echo -e "$(date "+%Y-%m-%d %H:%M:%S") done ${green}${white}" && \
notify-send -u critical -i ~/.icons/Flat-Remix-Blue-Dark/apps/scalable/weather-widget.svg "apod just changed ✓" "...to $apod_title"