Skip to content

Commit 433e875

Browse files
author
Andy Nguyen
committed
changes
1 parent 2a13d51 commit 433e875

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

segments/weather.sh

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,36 @@ EORC
3131
run_segment() {
3232
local cache_weather_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_cache_data.txt"
3333
local cache_location_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_cache_location.txt"
34-
local weather
34+
local weather=""
3535

3636
# Check if the weather data is still a valid cache hit
3737
if [ -f "$cache_weather_file" ]; then
3838
last_update=$(__read_file_last_update "$cache_weather_file")
3939
time_now=$(date +%s)
40-
4140
up_to_date=$(echo "(${time_now}-${last_update}) < ${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD}" | bc)
4241
if [ "$up_to_date" -eq 1 ]; then
43-
__read_file_content "$cache_weather_file"
44-
return
42+
weather=$(__read_file_content "$cache_weather_file")
4543
fi
4644
fi
47-
48-
__process_settings
49-
case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
50-
"yrno") weather=$(__yrno) ;;
51-
*)
52-
echo "Unknown weather provider [$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER]"
53-
return 1
54-
;;
55-
esac
56-
if [ -n "$weather" ]; then
57-
echo "$weather"
45+
46+
# Fetch from provider if empty
47+
# If a new provider is implemented, please set the $weather variable!
48+
if [ -z "$weather" ]; then
49+
__process_settings
50+
case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
51+
"yrno")
52+
weather=$(__yrno)
53+
;;
54+
*)
55+
# Just read the stale cache, default to empty/blank
56+
# Better not overwriting the previous healthy content
57+
weather=$(__read_file_content "$cache_weather_file")
58+
;;
59+
esac
60+
echo "$weather" > "$cache_weather_file"
5861
fi
62+
63+
echo "$weather"
5964
}
6065

6166
__process_settings() {
@@ -71,57 +76,49 @@ __process_settings() {
7176
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD" ]; then
7277
export TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD_DEFAULT}"
7378
fi
74-
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_GREP" ]; then
75-
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
76-
fi
7779
if [ "$TMUX_POWERLINE_SEG_WEATHER_LAT" = "auto" ] || [ "$TMUX_POWERLINE_SEG_WEATHER_LON" = "auto" ] || [ -z "$TMUX_POWERLINE_SEG_WEATHER_LON" ] || [ -z "$TMUX_POWERLINE_SEG_WEATHER_LAT" ]; then
7880
if ! get_auto_location; then
7981
exit 8
8082
fi
8183
fi
8284
}
8385

86+
# An implementation of a weather provider, just need to echo the result, run_segment() will take care of the rest
8487
__yrno() {
85-
degree=""
88+
local degree=""
8689

87-
if [ -z "$degree" ]; then
88-
# There's a chance that you will get rate limited or both location APIs are not working
89-
# Then long and lat will be "null", as literal string
90-
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LAT" ] || [ -z "$TMUX_POWERLINE_SEG_WEATHER_LON" ] || [ "$TMUX_POWERLINE_SEG_WEATHER_LAT" == null ] || [ "$TMUX_POWERLINE_SEG_WEATHER_LON" == null ]; then
91-
__read_file_content "$cache_weather_file"
92-
return
93-
fi
94-
if 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}"); then
95-
error=$(echo "$weather_data" | grep -i "error")
96-
if [ -n "$error" ]; then
97-
echo "error"
98-
return 1
99-
fi
100-
degree=$(echo "$weather_data" | jq -r '.properties.timeseries | .[0].data.instant.details.air_temperature')
101-
condition=$(echo "$weather_data" | jq -r '.properties.timeseries | .[0].data.next_1_hours.summary.symbol_code')
102-
elif [ -f "${cache_weather_file}" ]; then
103-
__read_file_content "$cache_weather_file"
104-
return
105-
fi
90+
# There's a chance that you will get rate limited or both location APIs are not working
91+
# Then long and lat will be "null", as literal string
92+
if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LAT" ] || [ -z "$TMUX_POWERLINE_SEG_WEATHER_LON" ]; then
93+
echo "Err: Unable to auto-detect your location"
94+
return 1
10695
fi
10796

108-
if [ -n "$degree" ]; then
109-
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
110-
degree=$(echo "${degree} + 273.15" | bc)
111-
fi
112-
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "f" ]; then
113-
degree=$(echo "${degree} * 9 / 5 + 32" | bc)
97+
if 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}"); then
98+
error=$(echo "$weather_data" | grep -i "error")
99+
if [ -n "$error" ]; then
100+
echo "error"
101+
return 1
114102
fi
115-
# condition_symbol=$(__get_yrno_condition_symbol "$condition" "$sunrise" "$sunset")
116-
condition_symbol=$(__get_yrno_condition_symbol "$condition")
117-
# Write the <content @ date>, separated by 2 spaces and @, so we can fetch it later on without having to call 'stat'
118-
echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')@$(date +%s)" > "$cache_weather_file"
119-
__read_file_content "$cache_weather_file"
120-
return
103+
degree=$(echo "$weather_data" | jq -r '.properties.timeseries | .[0].data.instant.details.air_temperature')
104+
condition=$(echo "$weather_data" | jq -r '.properties.timeseries | .[0].data.next_1_hours.summary.symbol_code')
105+
fi
106+
107+
if [ -z "$degree" ]; then
108+
echo "yr.no err: unable to fetch weather data"
109+
return 1
121110
fi
122111

123-
__read_file_content "$cache_weather_file"
124-
return
112+
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
113+
degree=$(echo "${degree} + 273.15" | bc)
114+
fi
115+
if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "f" ]; then
116+
degree=$(echo "${degree} * 9 / 5 + 32" | bc)
117+
fi
118+
# condition_symbol=$(__get_yrno_condition_symbol "$condition" "$sunrise" "$sunset")
119+
condition_symbol=$(__get_yrno_condition_symbol "$condition")
120+
# Write the <content@date>, separated by a @ character, so we can fetch it later on without having to call 'stat'
121+
echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')@$(date +%s)"
125122
}
126123

127124
# Get symbol for condition. Available symbol names: https://api.met.no/weatherapi/weathericon/2.0/documentation#List_of_symbols
@@ -180,23 +177,28 @@ __get_yrno_condition_symbol() {
180177
}
181178

182179
__read_file_split() {
183-
if [ ! -f "$1" ]; then
184-
echo "$3"
180+
file_to_read="$1"
181+
lookup_index="$2"
182+
fallback_value="$3"
183+
if [ ! -f "$file_to_read" ]; then
184+
echo "$fallback_value"
185185
return
186186
fi
187187
local -a file_arr
188-
IFS='@' read -ra file_arr <<< "$(cat "$1")"
189-
if [ -z "${file_arr[$2]}" ]; then
190-
echo "$3"
188+
IFS='@' read -ra file_arr <<< "$(cat "$file_to_read")"
189+
if [ -z "${file_arr[$lookup_index]}" ]; then
190+
echo "$fallback_value"
191191
return
192192
fi
193-
echo "${file_arr[$2]}"
193+
echo "${file_arr[$lookup_index]}"
194194
}
195195

196+
# Default to empty/blank
196197
__read_file_content() {
197-
__read_file_split "$1" 0 "N/A"
198+
__read_file_split "$1" 0 ""
198199
}
199200

201+
# Default to 0
200202
__read_file_last_update() {
201203
__read_file_split "$1" 1 0
202204
}

0 commit comments

Comments
 (0)