I’ve recently been messing around with Arch Linux and have installed it on a spare laptop (via ArcoLinux) that I had lying around. I installed xMonad and Polybar for a cleaner, simpler look.
One of the things that I looked around for was a way to have the current weather temperature appear in polybar. Most of everything that I found was either custom python scripts that you had to modify to implement or APIs that you had to sign-up (and/or pay) for.
Having already developed a way to get the current temperature in my NuGet assembly (Felsökning.Ireland), though, I knew that my government’s weather service produced an API that I could use (for free) and, given my previous experience with it, I figured it would be easy enough to develop against it, again (only, this time, no .NET).
First things, first, though: I had to figure out what the URL of the API was. Given that the point of reusable code is that you write it, once, and re-use it everywhere else, I hadn’t bothered (or needed) to remember what the URL was. I just went and got it from the (open) source.
With the URL in-hand, I opened terminal and started playing with how to get the specific value that I wanted (in this case, temperature) out of the return from the API.
After much testing (and lot more swiss cows’ing), I arrived at this one-liner to do just that:
curl -s https://prodapi.metweb.ie/observations/Claremorris/today | jq [last] | jq '.[0]' | jq -r '.temperature'
Now that I had a means to get the data value that I wanted/needed for my end-goal, I needed to implement it.
Enter custom modules for polybar. With these 9 lines of code (and the one line change referencing the module on the bar), I was able to achieve what I would have – otherwise – had to implement in a myriad of much more difficult ways.
[module/meteirannweather]
type = custom/script
exec = curl -s https://prodapi.metweb.ie/observations/Claremorris/today | jq [last] | jq '.[0]' | jq -r '.temperature'
interval = 500
interval-fail = 1000
format-prefix = ""
format-suffic = "°C"
format-background = ${colors.background}
format-foreground = ${colors.foreground}
NOTE: exec can be a script. I chose direct command because it was highly unlikely that I would need to reuse this anywhere else, given the value is always in polybar.
Ctrl + S in Sublime to save and, after about a second (the API may not be fast – but it’s free), I got 10°C
in polybar.
Success!