From 107d7ab44058e3ecfcb7eea1fdc0f764682b5682 Mon Sep 17 00:00:00 2001 From: Hugo Licon Date: Sun, 5 Jul 2020 18:22:47 -0600 Subject: [PATCH 1/3] add international support for weather module It uses [wttr.in](https://github.com/chubin/wttr.in) to get the weather information and the current city based on the IP address --- scripts/weather.sh | 68 ++++++---------------------------------------- 1 file changed, 9 insertions(+), 59 deletions(-) diff --git a/scripts/weather.sh b/scripts/weather.sh index 985af91..58b03e2 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -3,79 +3,29 @@ fahrenheit=$1 -load_request_params() -{ - - city=$(curl -s https://ipinfo.io/city 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() -{ - curl -s $region_code_url | grep $region &> /dev/null && region=$(curl -s $region_code_url | grep $region | cut -d ',' -f 2) - echo $region -} - weather_information() { - curl -sL $weather_url?inputstring=$zip | grep myforecast-current | grep -Eo '>.*<' | sed -E 's/>(.*)/dev/null; then - echo "$(display_weather)$city, $(get_region_code)" + echo "$(display_weather)" else echo "Location Unavailable" fi From 304dd700c9db59aa1feec51d7913ec4132fc1b4f Mon Sep 17 00:00:00 2001 From: Hugo Licon Date: Mon, 13 Jul 2020 22:02:47 -0600 Subject: [PATCH 2/3] Show unicode characters for forecast, remove plus Show unicode characters for forecast instead of an emoji. Remove the plus sign for positive degrees. Show city, region instead of city, country --- scripts/weather.sh | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/scripts/weather.sh b/scripts/weather.sh index 58b03e2..95274fb 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -3,11 +3,18 @@ fahrenheit=$1 -weather_information() +load_request_params() +{ + city=$(curl -s https://ipinfo.io/city 2> /dev/null) + region=$(curl -s https://ipinfo.io/region 2> /dev/null) +} + + +fetch_weather_information() { display_weather=$1 - # it gets the weather condition (%c), the temperature (%t), and the location (%l) - curl -sL curl wttr.in\?format="+%c+%t+%l$display_weather" + # 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" } #get weather display @@ -18,14 +25,38 @@ display_weather() else display_weather='&m' # for metric system fi - echo $(weather_information $display_weather) + weather_information=$(fetch_weather_information $display_weather) + + weather_condition=$(echo $weather_information | awk '{print $1;}') # Sunny, Snow, etc + temperature=$(echo $weather_information | awk '{print $2;}') # +31°C, -3°F, etc + unicode=$(forecast_unicode $weather_condition) + + echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature +} + +forecast_unicode() +{ + weather_condition=$1 + + if [[ $weather_condition =~ 'Snow' ]]; then + echo '❄ ' + elif [[ (($weather_condition =~ 'Rain') || ($weather_condition =~ 'Shower')) ]]; then + echo '☂ ' + elif [[ (($weather_condition =~ 'Overcast') || ($weather_condition =~ 'Cloud')) ]]; then + echo '☁ ' + elif [[ $weather_condition = 'NA' ]]; then + echo '' + else + echo '☀ ' + fi } main() { + load_request_params # process should be cancelled when session is killed if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then - echo "$(display_weather)" + echo "$(display_weather) $city, $region" else echo "Location Unavailable" fi From 4c5cd4a9e35484476525538e5a7c303e038f69f8 Mon Sep 17 00:00:00 2001 From: Hugo Licon Date: Tue, 4 Aug 2020 19:50:02 -0600 Subject: [PATCH 3/3] replace awk by cut --- scripts/weather.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/weather.sh b/scripts/weather.sh index 95274fb..b5bf371 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -27,8 +27,8 @@ display_weather() fi weather_information=$(fetch_weather_information $display_weather) - weather_condition=$(echo $weather_information | awk '{print $1;}') # Sunny, Snow, etc - temperature=$(echo $weather_information | awk '{print $2;}') # +31°C, -3°F, etc + 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 @@ -36,13 +36,13 @@ display_weather() forecast_unicode() { - weather_condition=$1 + weather_condition=$(echo $weather_condition | awk '{print tolower($0)}') - if [[ $weather_condition =~ 'Snow' ]]; then + if [[ $weather_condition =~ 'snow' ]]; then echo '❄ ' - elif [[ (($weather_condition =~ 'Rain') || ($weather_condition =~ 'Shower')) ]]; then + elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then echo '☂ ' - elif [[ (($weather_condition =~ 'Overcast') || ($weather_condition =~ 'Cloud')) ]]; then + elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then echo '☁ ' elif [[ $weather_condition = 'NA' ]]; then echo ''