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

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