Skip to content

Commit 8760b54

Browse files
Switched out deprecated yahoo API with met.no
1 parent 33a104b commit 8760b54

File tree

1 file changed

+71
-67
lines changed

1 file changed

+71
-67
lines changed

segments/weather.sh

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# The update period in seconds.
44
update_period=600
55

6-
TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yahoo"
6+
TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yrno"
7+
TMUX_POWERLINE_SEG_WEATHER_JSON_DEFAULT="jq"
78
TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT="c"
89
TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"
910

@@ -30,21 +31,22 @@ export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAUL
3031
export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
3132
# Name of GNU grep binary if in PATH, or path to it.
3233
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
33-
# Your location. Find a code that works for you:
34-
# 1. Go to Yahoo weather http://weather.yahoo.com/
35-
# 2. Find the weather for you location
36-
# 3. Copy the last numbers in that URL. e.g. "http://weather.yahoo.com/united-states/california/newport-beach-12796587/" has the numbers "12796587"
37-
TMUX_POWERLINE_SEG_WEATHER_LOCATION=""
34+
# Location of the JSON parser, jq
35+
export TMUX_POWERLINE_SEG_WEATHER_JSON="${TMUX_POWERLINE_SEG_WEATHER_JSON_DEFAULT}"
36+
# Your location
37+
# Latitude and Longtitude for use with yr.no
38+
TMUX_POWERLINE_SEG_WEATHER_LAT""
39+
TMUX_POWERLINE_SEG_WEATHER_LON=""
3840
EORC
3941
echo "$rccontents"
4042
}
4143

4244
run_segment() {
4345
__process_settings
44-
local tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_yahoo.txt"
46+
local tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/temp_weather_file.txt"
4547
local weather
4648
case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
47-
"yahoo") weather=$(__yahoo_weather) ;;
49+
"yrno") weather=$(__yrno) ;;
4850
*)
4951
echo "Unknown weather provider [${$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER}]";
5052
return 1
@@ -67,13 +69,16 @@ __process_settings() {
6769
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_GREP" ]; then
6870
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
6971
fi
70-
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LOCATION" ]; then
71-
echo "No weather location specified.";
72+
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_JSON" ]; then
73+
export TMUX_POWERLINE_SEG_WEATHER_JSON="${TMUX_POWERLINE_SEG_WEATHER_JSON_DEFAULT}"
74+
fi
75+
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LON" ] && [ -z "$TMUX_POWERLINE_SEG_WEATHER_LAT" ]; then
76+
echo "No location defined.";
7277
exit 8
7378
fi
7479
}
7580

76-
__yahoo_weather() {
81+
__yrno() {
7782
degree=""
7883
if [ -f "$tmp_file" ]; then
7984
if shell_is_osx || shell_is_bsd; then
@@ -90,32 +95,33 @@ __yahoo_weather() {
9095
fi
9196

9297
if [ -z "$degree" ]; then
93-
weather_data=$(curl --max-time 4 -s "https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27${TMUX_POWERLINE_SEG_WEATHER_UNIT}%27%20AND%20woeid%20=%20%27${TMUX_POWERLINE_SEG_WEATHER_LOCATION}%27")
98+
weather_data=$(curl --max-time 4 -s "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${TMUX_POWERLINE_SEG_WEATHER_LAT}&lon=${TMUX_POWERLINE_SEG_WEATHER_LON}")
9499
if [ "$?" -eq "0" ]; then
95-
error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
100+
grep=$TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT
101+
error=$(echo "$weather_data" | $grep -i "error");
96102
if [ -n "$error" ]; then
97103
echo "error"
98104
exit 1
99105
fi
100106

101-
# Assume latest grep is in PATH
102-
gnugrep="${TMUX_POWERLINE_SEG_WEATHER_GREP}"
103-
104-
# <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
105-
unit=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:units [^<>]*/>" | sed 's/.*temperature="\([^"]*\)".*/\1/')
106-
condition=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:condition [^<>]*/>")
107-
# <yweather:condition text="Clear" code="31" temp="66" date="Mon, 01 Oct 2012 8:00 pm CST" />
108-
degree=$(echo "$condition" | sed 's/.*temp="\([^"]*\)".*/\1/')
109-
condition=$(echo "$condition" | sed 's/.*text="\([^"]*\)".*/\1/')
107+
jsonparser="${TMUX_POWERLINE_SEG_WEATHER_JSON}"
108+
unit=$(echo "$weather_data" | $jsonparser -r .properties.meta.units.air_temperature)
109+
degree=$(echo "$weather_data" | $jsonparser -r .properties.timeseries[0].data.instant.details.air_temperature)
110+
condition=$(echo "$weather_data" | $jsonparser -r .properties.timeseries[0].data.next_1_hours.summary.symbol_code)
110111
# Pull the times for sunrise and sunset so we know when to change the day/night indicator
111112
# <yweather:astronomy sunrise="6:56 am" sunset="6:21 pm"/>
112113
if shell_is_osx || shell_is_bsd; then
113114
date_arg='-j -f "%H:%M %p "'
114115
else
115116
date_arg='-d'
116117
fi
117-
sunrise=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^\(.*\)sunset.*/\1/' | sed 's/^.*sunrise="\(.*m\)".*/\1/')" +%H%M)
118-
sunset=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^.*sunset="\(.*m\)".*/\1/')" +%H%M)
118+
119+
# # https://api.sunrise-sunset.org/json?lat=$TMUX_POWERLINE_SEG_WEATHER_LAT&lng=$TMUX_POWERLINE_SEG_WEATHER_LON&date=today
120+
# suntimes=$(curl --max-time 4 -s "https://api.sunrise-sunset.org/json?lat=${TMUX_POWERLINE_SEG_WEATHER_LAT}&lng=${TMUX_POWERLINE_SEG_WEATHER_LON}&date=today")
121+
# sunrise=$(echo $suntimes | $jsonparser -r .results.sunrise | cut -d " " -f1)
122+
# sunrise=$(date %H%M -d $sunrise)
123+
# sunset=$(echo $suntimes | $jsonparser -r .results.sunset | cut -d " " -f1)
124+
# sunset=$(date %H%M -d $sunset)
119125
elif [ -f "${tmp_file}" ]; then
120126
__read_tmp_file
121127
fi
@@ -125,62 +131,60 @@ __yahoo_weather() {
125131
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
126132
degree=$(echo "${degree} + 273.15" | bc)
127133
fi
128-
condition_symbol=$(__get_condition_symbol "$condition" "$sunrise" "$sunset")
129-
echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')" | tee "${tmp_file}"
134+
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "f" ]; then
135+
degree=$(echo "${degree} * 9 / 5 + 32" | bc)
136+
fi
137+
# condition_symbol=$(__get_yrno_condition_symbol "$condition" "$sunrise" "$sunset")
138+
condition_symbol=$(__get_yrno_condition_symbol "$condition")
139+
echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')" | tee "${tmp_file}"
130140
fi
131141
}
132142

133-
# Get symbol for condition. Available conditions: http://developer.yahoo.com/weather/#codes
134-
__get_condition_symbol() {
135-
local condition=$(echo "$1" | tr '[:upper:]' '[:lower:]')
136-
local sunrise="$2"
137-
local sunset="$3"
143+
# Get symbol for condition. Available symbol names: https://api.met.no/weatherapi/weathericon/2.0/documentation#List_of_symbols
144+
__get_yrno_condition_symbol() {
145+
# local condition=$(echo "$1" | tr '[:upper:]' '[:lower:]')
146+
# local sunrise="$2"
147+
# local sunset="$3"
148+
local condition=$1
138149
case "$condition" in
139-
"sunny" | "hot")
140-
hourmin=$(date +%H%M)
141-
if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
142-
#echo "☽"
143-
echo ""
144-
else
145-
#echo "☀"
146-
echo ""
147-
fi
150+
"clearsky_day")
151+
echo "🌣"
148152
;;
149-
"rain" | "mixed rain and snow" | "mixed rain and sleet" | "freezing drizzle" | "drizzle" | "light drizzle" | "freezing rain" | "showers" | "mixed rain and hail" | "scattered showers" | "isolated thundershowers" | "thundershowers" | "light rain with thunder" | "light rain" | "rain and snow")
150-
#echo "☂"
151-
echo ""
153+
"clearsky_night")
154+
echo ""
152155
;;
153-
"snow" | "mixed snow and sleet" | "snow flurries" | "light snow showers" | "blowing snow" | "sleet" | "hail" | "heavy snow" | "scattered snow showers" | "snow showers" | "light snow" | "snow/windy" | "snow grains" | "snow/fog")
154-
#echo "☃"
156+
"rain" | "lightrain" | "heavyrain" | "sleet" | "lightsleet" | "heavysleet")
157+
echo "🌧"
158+
;;
159+
"lightrainshowers" | "rainshowers" | "heavyrainshowers")
160+
echo "🌦"
161+
;;
162+
"snow" | "lightsnow" | "heavysnow")
155163
echo ""
156164
;;
157-
"cloudy" | "mostly cloudy" | "partly cloudy" | "partly cloudy/windy")
158-
echo ""
165+
"lightsnowshowers" | "heavysnowshowers" | "snowshowers")
166+
echo "🌨"
159167
;;
160-
"tornado" | "tropical storm" | "hurricane" | "severe thunderstorms" | "thunderstorms" | "isolated thunderstorms" | "scattered thunderstorms")
161-
#echo "⚡"
162-
echo ""
168+
"cloudy")
169+
echo "☁️"
163170
;;
164-
"dust" | "foggy" | "fog" | "haze" | "smoky" | "blustery" | "mist")
165-
#echo "♨"
166-
#echo "﹌"
167-
echo ""
171+
"partlycloudy_day")
172+
echo "🌤"
168173
;;
169-
"breezy")
170-
#echo "🌬"
171-
echo "🍃"
174+
"partlycloudy_night")
175+
echo "☾☁"
172176
;;
173-
"windy" | "fair/windy")
174-
#echo "⚐"
175-
echo ""
177+
"lightrainshowersandthunder" | "rainshowersandthunder" | "heavyrainshowersandthunder" | "lightsleetshowersandthunder" | "sleetshowersandthunder" | "heavysleetshowersandthunder" | "lightsnowshowersandthunder" | "snowshowersandthunder" | "heavysnowshowersandthunder" | "lightrainandthunder" | "rainandthunder" | "heavyrainandthunder" | "lightsleetandthunder" | "sleetandthunder" | "heavysleetandthunder" | "lightsnowandthunder" | "snowandthunder" | "heavysnowandthunder")
178+
echo "🌩"
176179
;;
177-
"clear" | "fair" | "cold")
178-
hourmin=$(date +%H%M)
179-
if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
180-
echo ""
181-
else
182-
echo ""
183-
fi
180+
"fog")
181+
echo "🌫"
182+
;;
183+
"fair_day")
184+
echo ""
185+
;;
186+
"fair_night")
187+
echo ""
184188
;;
185189
*)
186190
echo "?"

0 commit comments

Comments
 (0)