earth_wallpaper/earth_wallpaper.sh

256 lines
7.9 KiB
Bash
Raw Normal View History

2023-04-19 16:53:13 +02:00
#!/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 $?
}
2023-04-19 17:12:30 +02:00
check_font() {
font_name="${1}"
2023-04-19 17:14:20 +02:00
convert_cmd="$(which convert)"
2023-04-19 17:12:30 +02:00
if "${convert_cmd}" -list font | grep Font: | awk '{print($2)}' | grep font_name > /dev/null; then
return "${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}'"
return "${new_font_name}"
else
message "Error. No fonts available for convert!" p
exit 1
fi
fi
}
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
2023-04-19 17:13:32 +02:00
"--city")
shift
city="${1}"
shift
;;
2023-04-19 17:12:30 +02:00
"--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
2023-04-19 16:53:13 +02:00
check_internet || exit 1
2023-04-19 17:12:30 +02:00
font_name="$(check_font "${font_name}")"
2023-04-19 16:53:13 +02:00
message "Using font '${font_name}'"
2023-04-19 17:12:30 +02:00
curl_cmd=$(which curl)
if [ -z ${curl_cmd} ]; then
echo "It seems like curl is not installed."
2023-04-19 16:53:13 +02:00
exit 3
fi
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"
2023-04-19 17:12:30 +02:00
wiki_content=$("${curl_cmd}" -s $wiki_url)
2023-04-19 16:53:13 +02:00
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
2023-04-19 17:12:30 +02:00
"${curl_cmd}" -s $url > $wallpaper_file
2023-04-19 16:53:13 +02:00
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"
2023-04-19 17:12:30 +02:00
temperature=$("${curl_cmd}" -s "$weather_url" | egrep -o "<temp_c>-?[0-9]*\.?[0-9]?</temp_c>" | sed "s/<temp_c>//g" - | sed "s|</temp_c>||g" -)
2023-04-19 16:53:13 +02:00
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
2023-04-19 16:53:13 +02:00
fi
2023-04-19 17:02:19 +02:00
"${convert_cmd}" $wallpaper_file -pointsize 50 -font "${font_name}" -fill "#505050" -gravity SouthEast -annotate +0+0 "$text" $wallpaper_file
2023-04-19 16:53:13 +02:00
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