john

john / Fish Fetch Tempest Weather

Last active 5 months ago

Like 0

Hasty fish script to fetch basic weather data (conditions, temperature, low/high temperature forecast) from a Weatherflow Tempest station

john revised this gist 2 years ago · cbf04ad

1 file changed, 5 insertions, 1 deletion

fish_fetch_tempest_weather.fish
@@ -4,9 +4,13 @@
4 4 # set vars
5 5 set -l station 000000 # replace with your station ID
6 6 set -l token 00000000-0000-0000-0000-000000000000 # replace with API token
7 + set -l cache_max_age 3600 # 1 hour in seconds
8 +
9 + if not set -q XDG_CACHE_HOME
10 + set -x XDG_CACHE_HOME "$HOME/.cache"
11 + end
7 12 set -l cache_dir "$XDG_CACHE_HOME/fish"
8 13 set -l cache_file "$cache_dir/wx"
9 - set -l cache_max_age 3600 # 1 hour in seconds
10 14
11 15 function wx_print
12 16 # print weather

john revised this gist 2 years ago · cfea575

1 file changed, 3 insertions, 3 deletions

fish_fetch_tempest_weather.fish
@@ -8,7 +8,7 @@ set -l cache_dir "$XDG_CACHE_HOME/fish"
8 8 set -l cache_file "$cache_dir/wx"
9 9 set -l cache_max_age 3600 # 1 hour in seconds
10 10
11 - function print_weather
11 + function wx_print
12 12 # print weather
13 13 set -l wxdata $argv
14 14 if test (count $wxdata) -ne 4
@@ -40,7 +40,7 @@ if test -f $cache_file
40 40 set -l file_age (math $current_time - (stat -c %Z $cache_file))
41 41 if test $file_age -lt $cache_max_age
42 42 set -l wxdata (cat $cache_file | string split ',')
43 - print_weather $wxdata
43 + wx_print $wxdata
44 44 return 0
45 45 end
46 46 end
@@ -58,6 +58,6 @@ if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
58 58 ] | @csv' | tr -d '"' | string split ',')
59 59 if test (count $wxdata -eq 4)
60 60 string join ',' $wxdata >$cache_file
61 - print_weather $wxdata
61 + wx_print $wxdata
62 62 end
63 63 end

john revised this gist 2 years ago · 519e48f

1 file changed, 3 insertions, 3 deletions

fish_fetch_tempest_weather.fish
@@ -29,7 +29,6 @@ function print_weather
29 29 set_color white
30 30 echo ")"
31 31 set_color normal
32 - return 0
33 32 end
34 33
35 34 # ensure cache_dir exists
@@ -41,7 +40,8 @@ if test -f $cache_file
41 40 set -l file_age (math $current_time - (stat -c %Z $cache_file))
42 41 if test $file_age -lt $cache_max_age
43 42 set -l wxdata (cat $cache_file | string split ',')
44 - print_weather $wxdata; and return 0
43 + print_weather $wxdata
44 + return 0
45 45 end
46 46 end
47 47
@@ -58,6 +58,6 @@ if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
58 58 ] | @csv' | tr -d '"' | string split ',')
59 59 if test (count $wxdata -eq 4)
60 60 string join ',' $wxdata >$cache_file
61 - print_weather $wxdata; and return 0
61 + print_weather $wxdata
62 62 end
63 63 end

john revised this gist 2 years ago · d885f36

1 file changed, 3 insertions, 2 deletions

fish_fetch_tempest_weather.fish
@@ -29,6 +29,7 @@ function print_weather
29 29 set_color white
30 30 echo ")"
31 31 set_color normal
32 + return 0
32 33 end
33 34
34 35 # ensure cache_dir exists
@@ -40,7 +41,7 @@ if test -f $cache_file
40 41 set -l file_age (math $current_time - (stat -c %Z $cache_file))
41 42 if test $file_age -lt $cache_max_age
42 43 set -l wxdata (cat $cache_file | string split ',')
43 - print_weather $wxdata; and exit 0
44 + print_weather $wxdata; and return 0
44 45 end
45 46 end
46 47
@@ -57,6 +58,6 @@ if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
57 58 ] | @csv' | tr -d '"' | string split ',')
58 59 if test (count $wxdata -eq 4)
59 60 string join ',' $wxdata >$cache_file
60 - print_weather $wxdata; and exit 0
61 + print_weather $wxdata; and return 0
61 62 end
62 63 end

john revised this gist 2 years ago · 617d00f

1 file changed

fish_fetch_tempest_weather.sh renamed to fish_fetch_tempest_weather.fish

File renamed without changes

john revised this gist 2 years ago · 3a7ae48

1 file changed, 62 insertions

fish_fetch_tempest_weather.sh (file created)
@@ -0,0 +1,62 @@
1 + #!/usr/bin/env fish
2 + # vim: ft=fish
3 +
4 + # set vars
5 + set -l station 000000 # replace with your station ID
6 + set -l token 00000000-0000-0000-0000-000000000000 # replace with API token
7 + set -l cache_dir "$XDG_CACHE_HOME/fish"
8 + set -l cache_file "$cache_dir/wx"
9 + set -l cache_max_age 3600 # 1 hour in seconds
10 +
11 + function print_weather
12 + # print weather
13 + set -l wxdata $argv
14 + if test (count $wxdata) -ne 4
15 + return 1
16 + end
17 + set_color -d white
18 + echo -ne "\nwx@home: "
19 + set_color green
20 + echo -n "$wxdata[1] and $wxdata[2]°f "
21 + set_color white
22 + echo -n "("
23 + set_color blue
24 + echo -n "$wxdata[3]°"
25 + set_color white
26 + echo -n "|"
27 + set_color red
28 + echo -n "$wxdata[4]°"
29 + set_color white
30 + echo ")"
31 + set_color normal
32 + end
33 +
34 + # ensure cache_dir exists
35 + mkdir -p $cache_dir
36 +
37 + # use cached data if less than $cache_max_age old
38 + set -l current_time (date +%s)
39 + if test -f $cache_file
40 + set -l file_age (math $current_time - (stat -c %Z $cache_file))
41 + if test $file_age -lt $cache_max_age
42 + set -l wxdata (cat $cache_file | string split ',')
43 + print_weather $wxdata; and exit 0
44 + end
45 + end
46 +
47 + # fetch fresh data
48 + set -l wxjson (curl --max-time 2 -sL --compressed \
49 + "https://swd.weatherflow.com/swd/rest/better_forecast?station_id=$station&token=$token&units_temp=f")
50 +
51 + if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
52 + set -l wxdata (echo $wxjson | jq -r '[
53 + (.current_conditions.conditions | ascii_downcase),
54 + (.current_conditions.air_temperature | round),
55 + (.forecast.daily[0].air_temp_low | round),
56 + (.forecast.daily[0].air_temp_high | round)
57 + ] | @csv' | tr -d '"' | string split ',')
58 + if test (count $wxdata -eq 4)
59 + string join ',' $wxdata >$cache_file
60 + print_weather $wxdata; and exit 0
61 + end
62 + end