ansiweather

Weather in terminal, with ANSI colors and Unicode symbols
Log | Files | Refs | README | LICENSE

commit 708a34a385cc64350a0a7fb9b3b8d4bd713f2d58
parent 98ffc43417505353be8a0d56c88db623915a7e46
Author: Mike P <webmech@gmail.com>
Date:   Mon, 21 Oct 2013 22:45:09 -0400

automatic location detection added for even more awesomeness

Diffstat:
Mansiweather | 33+++++++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/ansiweather b/ansiweather @@ -19,6 +19,12 @@ config_file=~/.ansiweatherrc +weather_api=http://api.openweathermap.org/data/2.5/weather + +geo_locate_api="http://freegeoip.net/json/%s" #geo location service + +ip_service="ifconfig.me" #ip-determination service + function get_config { ret="" if [ -f $config_file ] @@ -34,8 +40,29 @@ function get_config { fi } +fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s") + +function get_ip_address { + echo $(curl -s $ip_service) +} + +function auto_locate { + ip=$(get_ip_address) + + geo_data=$($fetch_cmd $(printf $geo_locate_api $ip)) + + city=$(echo $geo_data | jq -r '.city') + + country=$(echo $geo_data | jq -r '.country_code') + + echo "$city,$country" +} + # Location : example "Moscow,RU" -location=$(get_config "location" || echo "Moscow,RU") +location=$(get_config "location" || auto_locate) + +#check and replace spaces for curl requests +location=$( printf "%s\n" "$location" | sed 's/ /%20/g' ) # System of Units : "metric" or "imperial" units=$(get_config "units" || echo "metric") @@ -65,9 +92,7 @@ rain=$(get_config "rain" || echo "\xe2\x98\x94") ###[ Fetch Weather data ]###################################################### -fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s") - -weather=$($fetch_cmd http://api.openweathermap.org/data/2.5/weather?q=$location\&units=$units) +weather=$($fetch_cmd $weather_api?q=$location\&units=$units&type=like)