commit e290ec1a21caaf791fc0fff8f15708622702cbe2
parent e5e3eee2ea2f7c57859867bfbbbb5f40e43bd7aa
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date: Thu, 31 Oct 2013 09:33:02 -0700
Merge pull request #11 from mreinhardt/master
Add ability to display upcoming weekly forecast
Diffstat:
M | README.md | | | 12 | ++++++++++++ |
M | ansiweather | | | 142 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- |
2 files changed, 116 insertions(+), 38 deletions(-)
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
@@ -37,24 +37,30 @@ function get_config {
}
# Location : example "Moscow,RU"
-if [ ! -z "$1" ]
-then
- location=$1;
-else
- location=$(get_config "location" || echo "Moscow,RU")
-fi
+location=$(get_config "location" || echo "Moscow,RU")
# System of Units : "metric" or "imperial"
-if [ ! -z "$2" ]
-then
- units=$2
-else
- units=$(get_config "units" || echo "metric")
-fi
+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 ]#################################################
@@ -100,22 +106,51 @@ fi
###[ 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")
+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 ]####################################################
-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')
+function epoch_to_date {
+ ret=$(date -j -r $1 +"%a %b %d")
+ echo $ret
+}
+
+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
@@ -124,7 +159,10 @@ azimuth=$(echo $weather | jq '.wind.deg')
declare -a directions
directions=(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW)
-direction=${directions[$(echo "($azimuth + 11.25)/22.5 % 16" | bc)]}
+if [ $forecast = 0 ]
+then
+ direction=${directions[$(echo "($azimuth + 11.25)/22.5 % 16" | bc)]}
+fi
@@ -132,11 +170,16 @@ direction=${directions[$(echo "($azimuth + 11.25)/22.5 % 16" | bc)]}
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
@@ -162,37 +205,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)
- icon="$fog "
+ echo "$fog "
;;
Snow)
- icon="$snow "
+ echo "$snow "
;;
Thunderstorm)
- icon="$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 $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"
+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