Following a recent community member query about using wttr.in weather data in fastfetch I decided to explore ways to improve the retrieval & use of the results.
Contributions, corrections, modifications & adaptations welcome.
Requirements:
- bash & basic shell commands (echo, cat, chmod)
- curl
- cron/crontab
- fastfetch (if you wish to use it)
Testing access before querying wttr.in
To prevent fastfetch potentially hanging or crashing if wttr is unreachable (eg: no internet etc) you could test whether a fast & highly available DNS server is available & conditionally retrieve weather data or echo a message instead:
[[ $(echo -n >/dev/tcp/1.1.1.1/53; echo $?) == 0 ]] &>/dev/null && <COMMANDS> || echo service unreachable
Above example will open a TCP connection to a specified host and port, Cloudflare 1.1.1.1:53 (use whatever you prefer), if successful it runs <COMMANDS> else it echoes message. We use this method instead of ping -c 1 host
because the latter will get fooled by proxys. Would prefer to test if wttr itself is reachable but their server has slow response times, combined with the curl -s wttr.in
command this would double the delay for fastfetch output.
Fastfetch module construct for conditional wttr output:
{
"type": "command",
"key": "Weather",
"text": "[[ $(echo -n >/dev/tcp/1.1.1.1/53; echo $?) == 0 ]] &>/dev/null && echo $(curl -s 'https://wttr.in/San%20Salvo?lang=it&format=%l,+%t+%c+%C,+%p') || echo wttr service unreachable"
}
Try it without network.
Fetch & cache
A solution to speed up wttr data access is periodically retrieving wttr data to the local system then reading the data from there, this can also be automated via a crontab entry.
The crontab prefetch & conditional output methods can be combined to produce a fast & somewhat fault aware wttr weather data lookup for fastfetch (or anything else), in which case you would test if wttr itself is available (wttr.in/80
) instead of x.x.x.x/53
:
1: Create the shell script & make it executable then open user crontab
- Change location /San%20Salvo
, language lang=it
, & formatting format=%l,+%t+%c+%C,+%p
according to your needs, wttr.in/:help
cat << 'EOF' > $HOME/.config/wttrcache.sh
#!/bin/bash
o="/tmp/wttr"
[[ $(echo -n >/dev/tcp/wttr.in/80; echo $?) == 0 ]] &>/dev/null && echo $(curl -s 'https://wttr.in/San%20Salvo?lang=it&format=%l,+%t+%c+%C,+%p') > "$o" || echo wttr service unreachable > "$o"
EOF
chmod +x $HOME/.config/wttrcache.sh
crontab -e
2: Put this in your crontab then press ctrl+x to save & exit
- This is set to run every 30 minutes (*/30
), change according to your needs but be considerate of wttr.in server traffic/load
- If you’re having trouble with or don’t like environmental variables in cron then replace $HOME with /home/yours or add HOME=/home/yours
beneath PATH
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/30 * * * * $HOME/.config/wttrcache.sh
3. Update your ~/.config/fastfetch/config.json weather module source:
{
"type": "command",
"key": "Weather",
"text": "[[ -f /tmp/wttr ]] && cat /tmp/wttr || echo no wttr cache..."
}
- /tmp contents don’t survive reboots, in case there is no wttr data in /tmp yet we test for existence & if not found then echo message
Now there should be no delay when running fastfetch or whatever you have referencing the cached wttr weather data.
Using cached weather data elsewhere
The cached wtrr.in weather data can be sourced by other programs or scripts, like conky or Zenity calendar or Bash PS1 for example, just be aware of any applicable rendering limitations like for emoji (in which case remove +%c
from the wttr URL) and that the data doesn’t persist across reboots thus won’t be available until the first cron run so consider changing cache location from /tmp/wttr
to $HOME/.cache/wttr
:
PS1='$( [[ -f $HOME/.cache/wttr ]] && cat $HOME/.cache/wttr || echo no wttr cache...), \u@\h: \W \$'
Produces: