2020-03-15 00:57:01 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-04-29 20:33:51 +02:00
|
|
|
fahrenheit=$1
|
2020-10-16 11:56:41 +02:00
|
|
|
location=$2
|
2020-04-29 20:33:51 +02:00
|
|
|
|
2020-10-16 11:56:41 +02:00
|
|
|
display_location()
|
2020-07-14 06:02:47 +02:00
|
|
|
{
|
2020-10-16 11:56:41 +02:00
|
|
|
if $location; then
|
|
|
|
city=$(curl -s https://ipinfo.io/city 2> /dev/null)
|
|
|
|
region=$(curl -s https://ipinfo.io/region 2> /dev/null)
|
|
|
|
echo " $city, $region"
|
|
|
|
else
|
|
|
|
echo ''
|
|
|
|
fi
|
2020-07-14 06:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fetch_weather_information()
|
2020-03-15 03:21:31 +01:00
|
|
|
{
|
2020-07-06 02:22:47 +02:00
|
|
|
display_weather=$1
|
2020-10-16 12:41:02 +02:00
|
|
|
# it gets the weather condition textual name (%C), and the temperature (%t)
|
2020-10-16 12:32:43 +02:00
|
|
|
curl -sL wttr.in\?format="+%C+%t$display_weather"
|
2020-03-15 03:21:31 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 02:22:47 +02:00
|
|
|
#get weather display
|
2020-03-15 03:21:31 +01:00
|
|
|
display_weather()
|
|
|
|
{
|
2020-07-06 02:22:47 +02:00
|
|
|
if $fahrenheit; then
|
|
|
|
display_weather='&u' # for USA system
|
2020-03-15 03:21:31 +01:00
|
|
|
else
|
2020-07-06 02:22:47 +02:00
|
|
|
display_weather='&m' # for metric system
|
2020-03-15 03:21:31 +01:00
|
|
|
fi
|
2020-07-14 06:02:47 +02:00
|
|
|
weather_information=$(fetch_weather_information $display_weather)
|
|
|
|
|
2020-10-16 13:26:28 +02:00
|
|
|
weather_condition=$(echo $weather_information | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc
|
|
|
|
temperature=$(echo $weather_information | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc
|
2020-07-14 06:02:47 +02:00
|
|
|
unicode=$(forecast_unicode $weather_condition)
|
|
|
|
|
|
|
|
echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:56:41 +02:00
|
|
|
forecast_unicode()
|
2020-07-14 06:02:47 +02:00
|
|
|
{
|
2020-08-05 03:50:02 +02:00
|
|
|
weather_condition=$(echo $weather_condition | awk '{print tolower($0)}')
|
2020-07-14 06:02:47 +02:00
|
|
|
|
2020-08-05 03:50:02 +02:00
|
|
|
if [[ $weather_condition =~ 'snow' ]]; then
|
2020-07-14 06:02:47 +02:00
|
|
|
echo '❄ '
|
2020-08-05 03:50:02 +02:00
|
|
|
elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then
|
2020-07-14 06:02:47 +02:00
|
|
|
echo '☂ '
|
2020-08-05 03:50:02 +02:00
|
|
|
elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then
|
2020-07-14 06:02:47 +02:00
|
|
|
echo '☁ '
|
|
|
|
elif [[ $weather_condition = 'NA' ]]; then
|
|
|
|
echo ''
|
|
|
|
else
|
|
|
|
echo '☀ '
|
|
|
|
fi
|
2020-03-15 03:21:31 +01:00
|
|
|
}
|
2020-03-15 00:57:01 +01:00
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
2020-03-15 05:13:46 +01:00
|
|
|
# process should be cancelled when session is killed
|
2020-03-15 03:21:31 +01:00
|
|
|
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
|
2020-10-16 11:56:41 +02:00
|
|
|
echo "$(display_weather)$(display_location)"
|
2020-03-15 03:21:31 +01:00
|
|
|
else
|
|
|
|
echo "Location Unavailable"
|
|
|
|
fi
|
2020-03-15 00:57:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#run main driver program
|
|
|
|
main
|