MLUG Bash Scripting Workshop 25/04/08 | ||
---|---|---|
Prev | Index | Next |
The Mornington Penninsula Weather is part of the Central District Forecast. The Victorian weather by District is listed at: http://www.bom.gov.au/weather/vic/forecasts.shtml.
The Central District Forecast is found at: http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV17102.txt.
We can use lynx, a CLI web browser, to dump the Central District Forecast as a text file with this command:lynx -dump http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV17102.txt > dump-weather.txt
I have included a copy of dump-weather.txt here. Once we can see all the text from the BOM's page in this text file we can determine what we will use in our script to grab just the information relevant to the Mornington Penninsula.
The first thing I want to display is the date and time this report was issued. It is found in this line of text:
Issued at 1531 on Thursday the 24th of April 2008Grep can be used to grab this line and print it to stdout when the web page is piped from lynx with this command:
lynx -dump www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV17102.txt | grep IssuedThat was fairly easy but well have to uses sed for the rest of the work and we'll be grabbing about 16 lines of text out of the middle of this page. The section we'll be grabbing looks like this:
[snipped] Mornington Peninsula Tonight and Friday Fine until a isolated showers develop late in the afternoon or evening. Cloud increasing during the day with moderate to fresh north to northwest winds tending westerly later. Min 12 Max 22 Precis: Isolated showers later. UV Index: 3 [Moderate] UV Alert from 11:40 to 13:00 Saturday Scattered showers, chiefly afternoon. Min 11 Max 18 Sunday Showers. Local hail possible. Min 8 Max 14 Monday Showers. Min 8 Max 14 [26]Home | [27]About Us | [28]Learn about Meteorology | [29]Contacts | [snipped]
We want everything between and including the line beginning with "Mornington" and the line beginning with Monday. However, the line of beginning with "Monday" will never be the same from one day to the next so we have to find another way to grab that line.
The next line, beginning with "[26]Home", never changes because it and the rest of that line are hyperlinks on the web page.
My solution is to use sed to grab every line from "Mornington" to "[26]" and then delete the line begining with "[26]". There may be another way, but I do it like this:sed -n '/Mornington/,/\[26\]/ p' | sed -e '/\[26\]/ d'
You might be thinking that the above looks a bit too cryptic so I'd better do a bit of explaining:
Don't get too hung up on syntax. There are more sed one liners on the web than you can shake a stick at. Understand the basic usage and if you have a need and don't have a matching snippet lying around then google it.
You should be able to follow this script without comments by now with the exception of maybe the final command which runs BSD Games' (Phases of the Moon) pom. If you don't have it on board, don't include it in your script. Just use echo and a couple newlines to distance your weather report from the command prompt when it returns.
#!/bin/bash clear echo -e "\n`date`\n" lynx -dump www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV17102.txt | grep Issued echo lynx -dump www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV17102.txt | \ sed -n '/Mornington/,/\[26\]/ p' | sed -e '/\[26\]/ d' echo "\n\n`pom`\n\n"
Well there it is, a bit simpler once you understand what's going on. It'll also look a bit different to the previous incarnations of this script because I change things as I learn more. My script for saving dvb-t broadcasts has undegone three re-writes in 3 1/2 years.
I have had a bit of fun with this script since first writing it. I have adapted it to make two other scripts.
The first script, weather-mail, was a bit of a goof used to send out friendly spam a couple vintages ago to some folks I know in the wine and grape industry in the Yarra Valley. It ran as a cron job. You can see what the email looked like here.
The second script, Mornington-weather.cgi generates a Mornington weather webpage on my website. You can see the webpage here. It is a bit slow to come up but keep in mind my website is hosted in the U.S. so it takes awhile to go from the server to Australia then back and then deliver to your browser.