commit 1298243dd2b3043aa4d5f57f9d5e7104a31c34f2
parent 708a34a385cc64350a0a7fb9b3b8d4bd713f2d58
Author: Mike P <webmech@gmail.com>
Date: Thu, 31 Oct 2013 18:32:14 -0400
refactored a bit, merged in new changes
Diffstat:
4 files changed, 180 insertions(+), 33 deletions(-)
diff --git a/AUTHORS b/AUTHORS
@@ -1,4 +1,4 @@
-Telize is developed by :
+AnsiWeather is developed by :
Frederic Cambus <fcambus AT users DOT sourceforge DOT net>
diff --git a/README.md b/README.md
@@ -52,6 +52,18 @@ Toggle Unicode symbols display. Value can be either `true` or `false` (requires
symbols:true
+### Display forecast
+
+Show upcoming forecast for the next `N` days (for 0 <= N <= 7). `0` will show standard output.
+
+ forecast:5
+
+## Command Line Options
+
+Any configuration options may also be passed in as command line options.
+
+ ./ansiweather -l Moscow -u metric -s true -f 5
+
## License
AnsiWeather is released under the BSD 3-Clause license. See `LICENSE` file
diff --git a/ansiweather b/ansiweather
@@ -6,7 +6,7 @@
# https://github.com/fcambus/ansiweather #
# #
# Created: 2013/08/29 #
-# Last Updated: 2013/10/19 #
+# Last Updated: 2013/10/30 #
# #
# AnsiWeather is released under the BSD 3-Clause license. #
# See LICENSE file for details. #
@@ -17,19 +17,15 @@
###[ Configuration options ]###################################################
-config_file=~/.ansiweatherrc
-
-weather_api=http://api.openweathermap.org/data/2.5/weather
-
-geo_locate_api="http://freegeoip.net/json/%s" #geo location service
+LC_NUMERIC=C
-ip_service="ifconfig.me" #ip-determination service
+config_file=~/.ansiweatherrc
function get_config {
ret=""
if [ -f $config_file ]
then
- ret=$(grep $1 $config_file | awk -F\: '{print $2}')
+ ret=$(grep "^$1" $config_file | awk -F: '{print $2}')
fi
if [ "X$ret" = "X" ]
@@ -42,24 +38,48 @@ function get_config {
fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s")
+
+
+###[ Check if jq is installed ]###############################################
+jqpath="`which jq`"
+if [ "$jqpath" == "" ]
+then
+ echo -e "\njq binary is not found, please download it from http://stedolan.github.io/jq/ and put it in your PATH"
+ exit 255
+fi
+
+###[ Auto-Location Logic ]###################################################
+geo_locate_api="http://freegeoip.net/json/%s" #geo location service
+
+ip_service="ifconfig.me" #ip-determination service
+
function get_ip_address {
- echo $(curl -s $ip_service)
+ echo $($fetch_cmd $ip_service)
}
function auto_locate {
- ip=$(get_ip_address)
+ ret=""
+ 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"
+ ret=$city,$country
+
+ if [ "$ret" == "," ]
+ then
+ return 1
+ else
+ echo $ret
+ fi
}
# Location : example "Moscow,RU"
-location=$(get_config "location" || auto_locate)
+location=$(get_config "location" || auto_locate || echo "Moscow,RU")
#check and replace spaces for curl requests
location=$( printf "%s\n" "$location" | sed 's/ /%20/g' )
@@ -70,6 +90,22 @@ units=$(get_config "units" || echo "metric")
# Display symbols : "true" or "false" (requires an Unicode capable display)
symbols=$(get_config "symbols" || echo true)
+# Show forecast : How many days, example "5". "0" is standard output
+forecast=$(get_config "forecast" || echo 0)
+
+# Or get config options from command line flags
+while getopts l:u:s:f: option
+do
+ case "${option}"
+ in
+ l) location=${OPTARG};;
+ u) units=${OPTARG};;
+ s) symbols=${OPTARG};;
+ f) forecast=${OPTARG};;
+ esac
+done
+
+
#### [ Colors and characters ]#################################################
@@ -81,30 +117,85 @@ dashes=$(get_config "dashes" || echo "\033[34m-")
+###[ Text Labels ]#############################################################
+
+greeting_text=$(get_config "greeting" || echo "Current weather in")
+wind_text=$(get_config "wind" || echo "Wind")
+humidity_text=$(get_config "humidity" || echo "Humidity")
+pressure_text=$(get_config "pressure" || echo "Pressure")
+
+
+
###[ Unicode Symbols for icons ]###############################################
sun=$(get_config "sun" || echo "\033[33;1m\xe2\x98\x80")
moon=$(get_config "moon" || echo "\033[36m\xe2\x98\xbd")
clouds=$(get_config "clouds" || echo "\033[37;1m\xe2\x98\x81")
rain=$(get_config "rain" || echo "\xe2\x98\x94")
+fog=$(get_config "fog" || echo "\033[37;1m\xe2\x96\x92")
+snow=$(get_config "snow" || echo "\033[37;1m\xe2\x9d\x84")
+thunderstorm=$(get_config "thunderstorm" || echo "\xe2\x9a\xa1")
###[ Fetch Weather data ]######################################################
-weather=$($fetch_cmd $weather_api?q=$location\&units=$units&type=like)
+fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s")
+api_cmd=$([[ $forecast != 0 ]] && echo "forecast/daily" || echo "weather")
+weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/$api_cmd?q=$location\&units=$units")
+###[ Process Weather data ]####################################################
+function epoch_to_date {
+ ret=$(date -j -r $1 +"%a %b %d")
+ echo $ret
+}
-###[ Process Weather data ]####################################################
+if [ $forecast != 0 ]
+then
+ city=$(echo $weather | jq -r '.city.name')
+ flength=$(echo $weather | jq '.list | length')
+ forecast=$([[ $forecast -gt $flength ]] && echo $flength || echo $forecast)
+ days=()
+ dates=()
+ lows=()
+ highs=()
+ humidity=()
+ pressure=()
+ sky=()
+ for i in $(seq 0 $(($forecast-1)))
+ do
+ days+=("$(echo $weather | jq ".list[$i]")")
+ dates+=("$(epoch_to_date $(echo ${days[$i]} | jq -r '.dt'))")
+ lows+=("$(printf "%0.0f" $(echo ${days[$i]} | jq -r '.temp.min'))")
+ highs+=("$(printf "%0.0f" $(echo ${days[$i]} | jq -r '.temp.max'))")
+ humidity+=("$(echo ${days[$i]} | jq -r '.humidity')")
+ pressure+=("$(echo ${days[$i]} | jq -r '.pressure')")
+ sky+=("$(echo ${days[$i]} | jq -r '.weather[0].main')")
+ done
+else
+ city=$(echo $weather | jq -r '.name')
+ temperature=$(printf '%.0f' $(echo $weather | jq '.main.temp'))
+ humidity=$(echo $weather | jq '.main.humidity')
+ pressure=$(echo $weather | jq '.main.pressure')
+ sky=$(echo $weather | jq -r '.weather[0].main')
+ sunrise=$(echo $weather | jq '.sys.sunrise')
+ sunset=$(echo $weather | jq '.sys.sunset')
+ wind=$(echo $weather | jq '.wind.speed')
+ azimuth=$(echo $weather | jq '.wind.deg')
+fi
-city=$(echo $weather | jq -r '.name')
-temperature=$(echo $weather | jq '.main.temp')
-humidity=$(echo $weather | jq '.main.humidity')
-pressure=$(echo $weather | jq '.main.pressure')
-sky=$(echo $weather | jq -r '.weather[0].main')
-sunrise=$(echo $weather | jq '.sys.sunrise')
-sunset=$(echo $weather | jq '.sys.sunset')
+
+
+###[ Process Wind data ]#######################################################
+
+declare -a directions
+directions=(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW)
+
+if [ $forecast = 0 ]
+then
+ direction=${directions[$(echo "($azimuth + 11.25)/22.5 % 16" | bc)]}
+fi
@@ -112,11 +203,16 @@ sunset=$(echo $weather | jq '.sys.sunset')
now=$(date +%s)
-if [ $now -ge $sunset ] || [ $now -le $sunrise ]
+if [ $forecast != 0 ]
then
- period="night"
+ period="none"
else
- period="day"
+ if [ $now -ge $sunset ] || [ $now -le $sunrise ]
+ then
+ period="night"
+ else
+ period="day"
+ fi
fi
@@ -126,9 +222,15 @@ fi
case $units in
metric)
scale="°C"
+ speed_unit="m/s"
+ pressure_unit="hPa"
+ pressure=$(printf '%.0f' $pressure)
;;
imperial)
scale="°F"
+ speed_unit="mph"
+ pressure_unit="inHg"
+ pressure=$(printf '%.2f' $(echo "$pressure*0.0295" | bc))
;;
esac
@@ -136,28 +238,60 @@ esac
###[ Set icons ]###############################################################
-if [ $symbols = true ]
-then
- case $sky in
+function get_icon {
+ case $1 in
Clear)
if [ $period = "night" ]
then
- icon="$moon "
+ echo "$moon "
else
- icon="$sun "
+ echo "$sun "
fi
;;
Clouds)
- icon="$clouds "
+ echo "$clouds "
;;
Rain)
- icon="$rain "
+ echo "$rain "
+ ;;
+ Fog)
+ echo "$fog "
+ ;;
+ Snow)
+ echo "$snow "
+ ;;
+ Thunderstorm)
+ echo "$thunderstorm "
;;
esac
+}
+
+if [ $symbols = true ]
+then
+ if [ $forecast = 0 ]
+ then
+ icon="$(get_icon $sky)"
+ fi
fi
###[ Display current Weather ]#################################################
-echo -e "$background$text Current weather in $city $delimiter$data $temperature $scale $icon$dashes$text Humidity $delimiter$data $humidity % $dashes$text Pressure $delimiter$data $pressure hPa \033[0m"
+if [ $forecast != 0 ]
+then
+ output="$background$text $city forecast $text$delimiter "
+ for i in $(seq 0 $(($forecast-1)))
+ do
+ icon="$(get_icon ${sky[$i]})"
+ output="$output$text${dates[$i]}: $data${highs[$i]}$text/$data${lows[$i]} $scale $icon"
+ if [ $i -lt $(($forecast-1)) ]
+ then
+ output="$output$dashes "
+ fi
+ done
+ output="$output\033[0m"
+ echo -e "$output"
+else
+ echo -e "$background$text $greeting_text $city $delimiter$data $temperature $scale $icon$dashes$text $wind_text $delimiter$data $wind $speed_unit $direction $dashes$text $humidity_text $delimiter$data $humidity % $dashes$text $pressure_text $delimiter$data $pressure $pressure_unit \033[0m"
+fi
diff --git a/ansiweather.plugin.zsh b/ansiweather.plugin.zsh
@@ -0,0 +1 @@
+export PATH=${PATH}:$(dirname $0)