Far too often I get so stuck into the work week that I forget to monitor the weather for the weekend when I should be going off to play on my dive kayaks — an activity which is somewhat weather dependent.
Luckily, help is at hand in the form of the ScraperWiki email alert system.
As you may have noticed, when you do any work on ScraperWiki, you start to receive daily emails that go:
Dear Julian_Todd, Welcome to your personal ScraperWiki email update. Of the 320 scrapers you own, and 157 scrapers you have edited, we have the following news since 2011-12-01T14:51:34: Histparl MP list - https://scraperwiki.com/scrapers/histparl_mp_list : * ran 1 times producing 0 records from 2 pages * with 1 exceptions, (XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '<!DOCTYP') ...Lots more of the same This concludes your ScraperWiki email update till next time. Please follow this link to change how often you get these emails, or to unsubscribe: https://scraperwiki.com/profiles/edit/#alerts
The idea behind this is to attract your attention to matters you may be interested in — such as fixing those poor dear scrapers you have worked on in the past and are now neglecting.
As with all good features, this was implemented as a quick hack.
I thought: why design a whole email alert system, with special options for daily and weekly emails, when we already have a scraper scheduling system which can do just that?
With the addition of a single flag to designate a scraper as an emailer (plus a further 20 lines of code), a new fully fledged extensible feature was born.
Of course, this is not counting the code that is in the Wiki part of ScraperWiki.
The default code in your emailer looks roughly like so:
import scraperwiki emaillibrary = scraperwiki.utils.swimport("general-emails-on-scrapers") subjectline, headerlines, bodylines, footerlines = emaillibrary.EmailMessageParts("onlyexceptions") if bodylines: print "\n".join([subjectline] + headerlines + bodylines + footerlines)
As you can see, it imports the 138 lines of Python from general-emails-on-scrapers, which I am not here to talk about right now.
Using ScraperWiki emails to watch the weather
Instead, what I want to explain is how I inserted my Good Weather Weekend Watcher by polling the weather forecast for Holyhead.
My extra code goes like this:
weatherlines = [ ] if datetime.date.today().weekday() == 2: # Wednesday url = "http://www.metoffice.gov.uk/weather/uk/wl/holyhead_forecast_weather.html" html = urllib.urlopen(url).read() root = lxml.html.fromstring(html) rows = root.cssselect("div.tableWrapper table tr") for row in rows: #print lxml.html.tostring(row) metweatherline = row.text_content().strip() if metweatherline[:3] == "Sat": subjectline += " With added weather" weatherlines.append("*** Weather warning for the weekend:") weatherlines.append(" " + metweatherline) weatherlines.append("")
What this does is check if today is Wednesday (day of the week #2 in Python land), then it parses through the Met Office Weather Report table for my chosen location, and pulls out the row for Saturday.
Finally we have to handle producing the combined email message, the one which can contain either a set of broken scraper alerts, or the weather forecast, or both.
if bodylines or weatherlines: if not bodylines: headerlines, footerlines = [ ], [ ] # kill off cruft surrounding no message print "\n".join([subjectline] + weatherlines + headerlines + bodylines + footerlines)
The current state of the result is:
*** Weather warning for the weekend: Mon 5Dec Day 7 °C W 33 mph 47 mph Very Good
This was a very quick low-level implementation of the idea with no formatting and no filtering yet.
Email alerts can quickly become sophisticated and complex. Maybe I should only send a message out if the wind is below a certain speed. Should I monitor previous days’ weather to predict whether the sea will be calm? Or I could check the wave heights on the off-shore buoys? Perhaps my calendar should be consulted for prior engagements so I don’t get frustrated by being told I am missing out on a good weekend when I had promised to go to a wedding.
The possibilities are endless and so much more interesting than if we’d implemented this email alert feature in the traditional way, rather than taking advantage of the utterly unique platform that we happened to already have in ScraperWiki.