Grab the weather forecast using weatherapi.com
I like to record the weather in my journals. For several years, I’ve used https://wttr.in via curl
. Recently, wttr has often been unreachable or would throw errors, so I took a look at https://www.weatherapi.com/
Designed for developers by developers, Weather API is the ultimate weather and geolocation API
The free account limits are generous, so I created an account. The default JSON results are very thorough. I created a little shell script that returns only the high/low temps and a text summary of the forecast:
#!/bin/sh
# Jack Baty, 2023 (https://baty.net)
jq=/opt/homebrew/bin/jq
weatherfile=`mktemp`
curl -s "https://api.weatherapi.com/v1/forecast.json?key=MYAPIKEY&q=MYZIPCODE&days=1&aqi=no&alerts=no" > $weatherfile
condition=`${jq} -r .forecast.forecastday[0].day.condition.text ${weatherfile}`
high=`${jq} -r .forecast.forecastday[0].day.maxtemp_f ${weatherfile}`
low=`${jq} -r .forecast.forecastday[0].day.mintemp_f ${weatherfile}`
echo "Low ${low}, High ${high} - ${condition}"
The output looks like this:
Low 52.1, High 72.6 - Patchy rain possible
Easy enough. I wrote a small lisp function for inserting the weather in Emacs:
(defun jab/insert-forecast ()
"Use weatherapi.com to insert the weather forecast at point"
(interactive)
(let ((w (shell-command-to-string "~/bin/getweather")))
(insert w)))