#!/bin/bash # # earth_wallpaper.sh # # Description: Obtain an image of the planet earth centered over # a city, with the night shadow according to the current time and # overlay of information # cur_dir=$(dirname "${0}") # shellcheck disable=SC1091 [ -r "${cur_dir}/shared_functions.sh" ] && . "${cur_dir}/shared_functions.sh" check_internet() { default_route=$(/sbin/route -n | egrep "^0.0.0.0") if [ "$default_route" == "" ]; then return 1 fi ping -q -c 2 en.wikipedia.org &> /dev/null return $? } check_font() { font_name="${1}" convert_cmd="$(which convert)" if "${convert_cmd}" -list font | grep Font: | awk '{print($2)}' | grep font_name > /dev/null; then echo "${font_name}" else new_font_name="$("${convert_cmd}" -list font | grep Font: | awk '{print($2)}' |head -n 1)" if [ "${new_font_name}" != "" ]; then message "Couldn't find font '${font_name}' in the list from convert, so I'm using the first '${new_font_name}'" echo "${new_font_name}" else message "Error. No fonts available for convert!" p exit 1 fi fi } usage() { echo "Usage:" echo "$(basename "${0}") --city [--help] [--temperature-unit celsius|fahrenheit] [--wallpaper-file ] [--archive-folder ] [--screen-width ] [--font-name ] [--debug] [--logfile ]" } temp_unit=celsius wallpaper_file="${HOME}/wallpaper.jpg" wallpaper_file2="/etc/lightdm/background.jpg" archive_folder="${HOME}/earth_pics" screen_width=1366 # `convert -list font` to get the list of fonts font_name="Bitstream-Vera-Sans" gsettings_cmd=$(which gsettings) font_name=$("${gsettings_cmd}" get org.gnome.desktop.interface font-name | sed "s/'//g" | sed 's/ [0-9]*$//g') while [ $# -gt 0 ] do case "$1" in "--help") shift usage exit 0 ;; "--temperature-unit") shift temp_unit="${1}" shift ;; "--city") shift city="${1}" shift ;; "--wallpaper-file") shift wallpaper_file="${1}" shift ;; "--archive-folder") shift archive_folder="${1}" shift ;; "--screen-width") shift screen_width="${1}" shift ;; "--font-name") shift font_name="${1}" shift ;; "--debug") shift DEBUG=1 ;; '--logfile') shift LOG_FILE="$1" shift ;; *) message "Unknown parameter '${1}'" shift ;; esac done if [ "${temp_unit}" == "celsius" ]; then short_temp_unit='C' elif [ "${temp_unit}" == "fahrenheit" ]; then short_temp_unit='F' else message "Temperature unit '${temp_unit}' is not one of 'celsius' or 'fahrenheit'" p exit 2 fi check_internet || exit 1 font_name="$(check_font "${font_name}")" message "Using font '${font_name}'" curl_cmd=$(which curl) if [ -z ${curl_cmd} ]; then echo "It seems like curl is not installed." exit 3 fi export DISPLAY=:0.0 if [[ -z ${city} ]]; then echo "You must indicate a city to center the image." exit 4 fi str_city=$(echo "$city" | sed "s/ /_/g" -) message "str_city $str_city" wiki_url="https://en.wikipedia.org/wiki/$str_city" python3_cmd=$(which python3) if [ "${python3_cmd}" == "" ]; then echo "Python3 not found" exit 3 fi escaped_city=$("${python3_cmd}" -c 'import urllib.parse; import sys; print(urllib.parse.quote(sys.argv[1]))' "${str_city}") wiki_url="https://en.wikipedia.org/w/api.php?action=query&prop=coordinates&format=json&titles=${escaped_city}" message "wiki_url $wiki_url" wiki_content=$("${curl_cmd}" -s $wiki_url) latitude=$(echo "${wiki_content}" | jq '.query.pages[].coordinates[0].lat') if (( $(echo "${latitude} > 0" | bc -l) )); then o_latitude=North else o_latitude=South fi latitude="${latitude//-/}" message "Latitude '${latitude} ${o_latitude}'" longitude=$(echo "${wiki_content}" | jq '.query.pages[].coordinates[0].lon') if (( $(echo "${longitude} > 0" | bc -l) )); then o_longitude=East else o_longitude=West fi longitude="${longitude//-/}" message "Longitude '${longitude} ${o_longitude}'" #url='https://www.fourmilab.ch/cgi-bin/Earth?img=learth.evif&imgsize=1366&dynimg=y&opt=-l&lat=41&ns=North&lon=0&ew=East&alt=35785&tle=&date=0&utc=&jd=' url="https://www.fourmilab.ch/cgi-bin/Earth?img=learth.evif&imgsize=$screen_width&dynimg=y&opt=-l&lat=$latitude&ns=$o_latitude&lon=$longitude&ew=$o_longitude&alt=35785&tle=&date=0&utc=&jd=" message "URL $url" cur_date=$(date +%Y-%m-%d) time=$(date +%H.%M.%S) date="$cur_date_$time" if [[ -e $wallpaper_file ]]; then rm "${wallpaper_file}" -rf fi "${curl_cmd}" -s $url > $wallpaper_file error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code downloading image from '$url' to '$wallpaper_file'" exit 1 fi if [[ "$o_latitude" == "South" ]]; then g_latitude="-$g_latitude" fi if [[ "$o_longitude" == "West" ]]; then g_longitude="-$g_longitude" fi weather_url="https://api.wunderground.com/api/be78b9c2c0dbd00f/conditions/lang:ES/q/$g_latitude,$g_longitude.xml" weather_url="https://api.open-meteo.com/v1/forecast?latitude=${g_latitude}&longitude=${g_longitude}¤t_weather=true&temperature_unit=${temp_unit}" message "Weather URL: ${weather_url}" current_weather="$("${curl_cmd}" -s "$weather_url")" temperature=$(echo "${current_weather}" | jq '.current_weather.temperature') text="Host: ${HOSTNAME}\n${cur_date} ${time}\n${city} ${temperature}ยบ ${short_temp_unit}" convert_cmd=$(which convert) if [ ! -x "${convert_cmd}" ]; then if [ "$(which apt)" != "" ]; then apt install imagemagick elif [ "$(which yum)" != "" ]; then yum install imagemagick elif [ "${which pacman}" != "" ]; then pacman -S imagemagick else message "Install imagemagick using your package manager." p exit 1 fi fi "${convert_cmd}" $wallpaper_file -pointsize 50 -font "${font_name}" -fill "#505050" -gravity SouthEast -annotate +0+0 "$text" $wallpaper_file error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code adding date to the image" fi mkdir $archive_folder -p if pgrep lightdm > /dev/null then if [[ -w "$wallpaper_file2" ]] then cp "$wallpaper_file" "$wallpaper_file2" else echo "I'm not able to write to $wallpaper_file2" fi fi # Store the image cp "$wallpaper_file" "$archive_folder/pic_$date.jpg" -rfp error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code saving a copy of the image" fi # Set the gnome 2 desktop background if [ "${gconf_cmd}" != "" ]; then run_and_log "${gconf_cmd}" --type string --set /desktop/gnome/background/picture_options scaled error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting option scaled for the wallpaper" fi run_and_log "${gconf_cmd}" --type string --set /desktop/gnome/background/picture_filename "${wallpaper_file}" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome 2" fi fi if [ "${gsettings_cmd}" != "" ]; then run_and_log "${gsettings_cmd}" set org.gnome.desktop.background picture-uri "file://$wallpaper_file" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi run_and_log "${gsettings_cmd}" set org.gnome.desktop.background picture-uri-dark "file://$wallpaper_file" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi run_and_log "${gsettings_cmd}" set org.gnome.desktop.background picture-options "scaled" run_and_log "${gsettings_cmd}" set org.gnome.desktop.background primary-color '#000000' run_and_log "${gsettings_cmd}" set com.canonical.unity-greeter background "file://$wallpaper_file2" run_and_log "${gsettings_cmd}" set org.gnome.desktop.screensaver picture-uri "file://$wallpaper_file2" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi run_and_log "${gsettings_cmd}" set org.mate.background picture-filename "file://$wallpaper_file" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi run_and_log "${gsettings_cmd}" set org.cinnamon.desktop.background picture-uri "file://$wallpaper_file" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi run_and_log "${gsettings_cmd}" set com.ubuntu.login-screen.background picture-uri "file://$wallpaper_file2" error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code setting image file as wallpaper in Gnome" fi fi if pgrep gnome-session > /dev/null then message "This is Gnome, no XFdesktop to reload." else run_and_log xfdesktop --reload 2> /dev/null error_code=$? if [[ "$error_code" != "0" ]]; then echo "Error $error_code reloading xfdesktop" > /dev/null fi fi