earth_wallpaper/earth_wallpaper.sh

192 lines
6.7 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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"
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')
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_internet || exit 1
message "Using font '${font_name}'"
wget_cmd=$(which wget)
if [ -z ${wget_cmd} ]; then
echo "It seems like wget is not installed."
exit 3
fi
city="$1"
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"
message "wiki_url $wiki_url"
wiki_content=$("${wget_cmd}" -q -O - $wiki_url)
latitude=$(echo $wiki_content | egrep -o "[0-9]{1,2}°[0-9]?[0-9]??[0-9]?[0-9]?″?[NS]" | head -n 1)
message "latitude $latitude"
longitude=$(echo $wiki_content | egrep -o "[0-9]{1,2}°[0-9]?[0-9]??[0-9]?[0-9]?″?[EW]" | head -n 1)
message "longitude $longitude"
g_latitude=$(echo $latitude | egrep -o "[0-9]{1,2}°" | sed "s/°//g" - )
message "g_latitude $g_latitude"
m_latitude=$(echo $latitude | egrep -o "[0-9]{1,2}" | sed "s///g" - )
message "m_latitude $m_latitude"
[[ "$m_latitude" == "" ]] && m_latitude=0
s_latitude=$(echo $latitude | egrep -o "[0-9]{1,2}″" | sed "s/″//g" - )
message "s_latitude $s_latitude"
[[ "$s_latitude" == "" ]] && s_latitude=0
o_latitude=$(echo $latitude | egrep -o "[NS]")
if [[ "$o_latitude" == "S" ]]; then
o_latitude=South
else
o_latitude=North
fi
message "o_latitude $o_latitude"
gm_latitude=$(echo "scale=2; $m_latitude / 60" | bc)
message "gm_latitude $gm_latitude"
gs_latitude=$(echo "scale=2; $m_latitude / 3600" | bc)
message "gs_latitude $gs_latitude"
g_latitude=$(echo "scale=2; $g_latitude + $gm_latitude + $gs_latitude" | bc)
message "g_latitude $g_latitude"
g_longitude=$(echo $longitude | egrep -o "[0-9]{1,2}°" | sed "s/°//g" - )
message "g_longitude $g_longitude"
m_longitude=$(echo $longitude | egrep -o "[0-9]{1,2}" | sed "s///g" - )
[[ "$m_longitude" == "" ]] && m_longitude=0
message "m_longitude $m_longitude"
s_longitude=$(echo $longitude | egrep -o "[0-9]{1,2}″" | sed "s/″//g" - )
[[ "$s_longitude" == "" ]] && s_longitude=0
message "s_longitude $s_longitude"
o_longitude=$(echo $longitude | egrep -o "[EW]")
if [[ "$o_longitude" == "E" ]]; then
o_longitude=East
else
o_longitude=West
fi
message "o_longitude $o_longitude"
gm_longitude=$(echo "scale=2; $m_longitude / 60" | bc)
message "gm_longitude $gm_longitude"
gs_longitude=$(echo "scale=2; $m_longitude / 3600" | bc)
message "gs_longitude $gs_longitude"
g_longitude=$(echo "scale=2; $g_longitude + $gm_longitude + $gs_longitude" | bc)
message "g_longitude $g_longitude"
#url='http://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="http://www.fourmilab.ch/cgi-bin/Earth?img=learth.evif&imgsize=$screen_width&dynimg=y&opt=-l&lat=$g_latitude&ns=$o_latitude&lon=$g_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
"${wget_cmd}" -q $url -O $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="http://api.wunderground.com/api/be78b9c2c0dbd00f/conditions/lang:ES/q/$g_latitude,$g_longitude.xml"
temperature=$("${wget_cmd}" -q -O - "$weather_url" | egrep -o "<temp_c>-?[0-9]*\.?[0-9]?</temp_c>" | sed "s/<temp_c>//g" - | sed "s|</temp_c>||g" -)
text="Image from $cur_date at $time\n$city $temperatureº C"
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
run_and_log "${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-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 "$wallpaper_file2"
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