Merge pull request #52 from HugoLiconV/feature/weather-international-support
add international support for weather module
This commit is contained in:
commit
fbc02ca665
1 changed files with 23 additions and 42 deletions
|
@ -5,77 +5,58 @@ fahrenheit=$1
|
||||||
|
|
||||||
load_request_params()
|
load_request_params()
|
||||||
{
|
{
|
||||||
|
|
||||||
city=$(curl -s https://ipinfo.io/city 2> /dev/null)
|
city=$(curl -s https://ipinfo.io/city 2> /dev/null)
|
||||||
region=$(curl -s https://ipinfo.io/region 2> /dev/null)
|
region=$(curl -s https://ipinfo.io/region 2> /dev/null)
|
||||||
zip=$(curl -s https://ipinfo.io/postal 2> /dev/null | tail -1)
|
|
||||||
country_w_code=$(curl -w "\n%{http_code}\n" -s https://ipinfo.io/country 2> /dev/null)
|
|
||||||
country=`grep -Eo [a-zA-Z]+ <<< "$country_w_code"`
|
|
||||||
exit_code=`grep -Eo [0-9]{3} <<< "$country_w_code"`
|
|
||||||
|
|
||||||
region_code_url=http://www.ip2country.net/ip2country/region_code.html
|
|
||||||
weather_url=https://forecast.weather.gov/zipcity.php
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#substitute region code for regions in north america
|
|
||||||
get_region_code()
|
fetch_weather_information()
|
||||||
{
|
{
|
||||||
curl -s $region_code_url | grep $region &> /dev/null && region=$(curl -s $region_code_url | grep $region | cut -d ',' -f 2)
|
display_weather=$1
|
||||||
echo $region
|
# it gets the weather condition textual name (%C), the temperature (%t), and the location (%l)
|
||||||
|
curl -sL curl wttr.in\?format="+%C+%t$display_weather"
|
||||||
}
|
}
|
||||||
|
|
||||||
weather_information()
|
#get weather display
|
||||||
{
|
display_weather()
|
||||||
curl -sL $weather_url?inputstring=$zip | grep myforecast-current | grep -Eo '>.*<' | sed -E 's/>(.*)</\1/'
|
|
||||||
}
|
|
||||||
get_temp()
|
|
||||||
{
|
{
|
||||||
if $fahrenheit; then
|
if $fahrenheit; then
|
||||||
echo $(weather_information | grep 'deg;F' | cut -d '&' -f 1)
|
display_weather='&u' # for USA system
|
||||||
else
|
else
|
||||||
echo $(( ($(weather_information | grep 'deg;F' | cut -d '&' -f 1) - 32) * 5 / 9 ))
|
display_weather='&m' # for metric system
|
||||||
fi
|
fi
|
||||||
|
weather_information=$(fetch_weather_information $display_weather)
|
||||||
|
|
||||||
|
weather_condition=$(echo $weather_information | cut -d "+" -f 1 | cut -d "-" -f 1) # Sunny, Snow, etc
|
||||||
|
temperature=$(echo $weather_information | cut -d '+' -f 2) # +31°C, -3°F, etc
|
||||||
|
unicode=$(forecast_unicode $weather_condition)
|
||||||
|
|
||||||
|
echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature
|
||||||
}
|
}
|
||||||
|
|
||||||
forecast_unicode()
|
forecast_unicode()
|
||||||
{
|
{
|
||||||
forecast=$(weather_information | head -n 1)
|
weather_condition=$(echo $weather_condition | awk '{print tolower($0)}')
|
||||||
|
|
||||||
if [[ $forecast =~ 'Snow' ]]; then
|
if [[ $weather_condition =~ 'snow' ]]; then
|
||||||
echo '❄ '
|
echo '❄ '
|
||||||
elif [[ (($forecast =~ 'Rain') || ($forecast =~ 'Shower')) ]]; then
|
elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then
|
||||||
echo '☂ '
|
echo '☂ '
|
||||||
elif [[ (($forecast =~ 'Overcast') || ($forecast =~ 'Cloud')) ]]; then
|
elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then
|
||||||
echo '☁ '
|
echo '☁ '
|
||||||
elif [[ $forecast = 'NA' ]]; then
|
elif [[ $weather_condition = 'NA' ]]; then
|
||||||
echo ''
|
echo ''
|
||||||
else
|
else
|
||||||
echo '☀ '
|
echo '☀ '
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
#get weather display if in US
|
|
||||||
display_weather()
|
|
||||||
{
|
|
||||||
if [ $country = 'US' ]; then
|
|
||||||
echo "$(forecast_unicode)$(get_temp)° "
|
|
||||||
else
|
|
||||||
echo ''
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
# don't run the rest of the script unless we can safely get all the information
|
|
||||||
load_request_params
|
load_request_params
|
||||||
|
|
||||||
if [[ $exit_code -eq 429 ]]; then
|
|
||||||
echo "Request Limit Reached"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
# process should be cancelled when session is killed
|
# process should be cancelled when session is killed
|
||||||
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
|
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
|
||||||
echo "$(display_weather)$city, $(get_region_code)"
|
echo "$(display_weather) $city, $region"
|
||||||
else
|
else
|
||||||
echo "Location Unavailable"
|
echo "Location Unavailable"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue