Add translated script

This commit is contained in:
Antonio J. Delgado 2023-04-19 17:53:13 +03:00
parent bc5c78e9e5
commit 074e8e4764
2 changed files with 274 additions and 0 deletions

183
earth_wallpaper.sh Executable file
View file

@ -0,0 +1,183 @@
#!/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
apt install imagemagick
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

91
shared_functions.sh Executable file
View file

@ -0,0 +1,91 @@
#!/bin/bash
function message() {
red="\e[1;31m"
yellow="\e[1;33m"
blue="\e[34m"
if [ -z "${APP}" ]; then
app=$(basename "${0}" .sh)
else
app="${APP}"
fi
current_date=$(date '+%Y-%m-%d %H:%M:%S')
text="${1}"
if [ -n "${text}" ]; then
if [ ${#} -gt 1 ]; then
print="${2}"
shift
else
print=""
fi
if [ ${#} -gt 1 ]; then
extra_args="${2}"
shift
subject="${2}"
shift
recipient="${2}"
shift
else
extra_args=""
fi
# shellcheck disable=SC2153
if [ -z "${LOG_FILE}" ];then
if [ -w /var/log ]; then
log_file="/var/log/${app}.log"
else
mkdir -p "${HOME}/log/"
log_file="${HOME}/log/${app}.log"
fi
else
log_file="${LOG_FILE}"
fi
mkdir -p "$(dirname "${log_file}")"
if [[ "${print}" =~ [Ww][Aa][Rr][Nn] ]]; then
echo -e "${yellow}${current_date} ${text}"
else
if [[ "${print}" =~ [Ee][Rr][Rr] ]]; then
>&2 echo -e "${red}${current_date} ${text}"
else
if [[ ( -n "${DEBUG}" ) && ( "${DEBUG}" == "true" ) || ( "${DEBUG}" -gt "0" ) ]] || [[ "${print}" =~ [Pp] ]] || [[ "${print}" =~ [Pp][Rr][Ii][Nn][Tt] ]]; then
echo -e "${blue}${current_date} ${text}"
fi
fi
fi
echo "${current_date} ${text}" >> "${log_file}"
logger -t "${app}" "${text}"
if [ "${extra_args}" == "to_mail" ]; then
echo "${text}" | mail -s "${subject}" "${recipient}"
fi
fi
}
function run_and_log() {
extra_args=""
if [ "${1}" == "to_mail" ]; then
extra_args="to_mail"
shift
subject="${1}"
shift
recipient="${1}"
shift
fi
CMD=$*
message "Running and logging: ${CMD}"
if [[ ( -n "${DEBUG}" ) && ( "${DEBUG}" == "true" ) ]]; then
temp_file=$(mktemp /tmp/tmp.XXXX.log)
message "Debug log file '${temp_file}'"
output=$(${CMD} 2>&1 |tee "${temp_file}")
else
output=$(${CMD} 2>&1)
fi
return_code="${?}"
for line in "${output[@]}"
do
message "${line}" "${extra_args}" "${subject}" "${recipient}"
done <<< "${output[@]}"
if [ "${return_code}" != "0" ]; then
message "Command run finished with error ${return_code}." "${extra_args}" "${subject}" "${recipient}"
else
message "Command ran without errors." "${extra_args}" "${subject}" "${recipient}"
fi
}