banner

For a full list of BASHing data blog posts see the index page.  RSS


The third and final version of A Data Cleaner's Cookbook is now online! The Cookbook has been corrected, expanded and slightly reorganised. If you use the Cookbook regularly be sure to refresh your browser to ensure you go to the latest versions of the pages you visit.

Scripting a temperature notifier

My wife and I go for an early morning walk by a nearby river every day. I like to know in advance how cold it's been overnight, so I can wear suitably warm clothing. There's no local weather station recording the riverside temperature, but a fair approximation is the minimum overnight temperature at Devonport Airport here in northwest Tasmania. The airport reports its temperature data to the Bureau of Meterorology (BOM; Australia).

Previously, to get the minimum and current temperature I would open a browser, go to the BOM website page with Tasmanian observations, then look for the Devonport Airport figures: see screenshot.

temp1

This seemed like too much work to get a few numbers, so I scripted a desktop notifier:

#!/bin/bash
code=$(links -source http://www.bom.gov.au/tas/observations/tasall.shtml?ref=hdr)
temps=$(grep -A1 "lowtmp tNWC-station-devonport-airport" <<<"$code" | awk -F"[<>]" 'NR==1 {print "Low: "$3" at "$7} NR==2 {print "High: "$3" at "$7}')
printf "Devonport Airport\n$temps" | yad --text-info --geometry="250x75+1600+600" --timeout="5" --no-buttons --undecorated --justify="center"
exit 0

The script uses the links command-line browser to go to the relevant BOM page and grab its source code with the links -source option. If I use curl the BOM will tell me firmly and politely:

Your access is blocked due to the detection of a potential automated access request. The Bureau of Meteorology website does not support web scraping: if you are trying to access Bureau data through automated means, you should stop.

The relevant lines in the page code are shown here:

temp2

I retrieve the lines with grep -A1, which grabs both the "lowtmp" line and the "hightmp" line after it. I process each line with AWK to print the temperature, " at " and the recording time, plus an appropriate introduction (e.g. "Low: "); the AWK output is stored in a shell variable. The day's high temperature since midnight will approximate the current riverside temperature before our early morning walk.

Finally, I send the AWK-output variable to a YAD text-info dialog, with options to

The script is launched with a keyboard shortcut. Here's the notification window before a recent walk:

temp3

Last update: 2022-01-26
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License