Perl checks for rain and issues umbrella warning

Umbrella or Bust?

To determine whether it will rain on any given day on your way to and from work and, thus, whether you need an umbrella, you can launch the script in Listing 2 at dawn as a cronjob. In line 12, it defines the critical hours – 8 and 9am for commuting to work and 5, 6, or 7pm for commuting back home after work.

Listing 2

umbrella

 

In line 15, you need to define the weather location in the location parameter and again the access key in api_key. The auto_api option ensures that the CPAN module calls the Wunderground server's REST interface behind the scenes and returns the desired JSON array using the hourly() method invoked in line 20.

When called without any parameters, the script only prints a message if the probability of rain in the critical hours is more than 50 percent. Then the printf statement in line 33 prints a string such as

Take your Umbrella! (52% rain at 19:00:00)

to standard output, and the subsequent email script in Listing 3 sends the message to the eagerly waiting users.

Listing 3

mandrill-email

 

If there is no danger of rainfall drenching the employee, the script does not output anything, and the subsequent emailer cancels without sending a message.

If you want to know exactly what the script is getting up to, call it with the -v option, as shown in Figure 5, and you will receive rain forecasts for the critical hours in the form of Log4perl messages. However, the cronjob needs to run the script without -v – like this, for example, at seven o'clock in the morning:

 

to prevent the mailer from sending a message even if it does not look like rain. Note also that either the scripts must be in a directory that the cronjob can find or the cron entry must explicitly specify the absolute path to the scripts.

Figure 5: In verbose mode, the script tells you which messages from the weather service it is checking.

Looking for a Mail Provider

In this age of spam, it isn't easy to automate the process of sending email. At the office, or at home, you can do this because the ISP can verify who is doing what. To be flexible, it is a good idea to turn to a third party. For example, Mandrill allows you to use their email servers for a fee, but test accounts can also send a few email messages for free.

Email with REST

Interested users can register with a valid email address [4] and then receive a 22-digit API key in Base64 format in turn, which the script can later use to send mail via Mandrill's REST server. However, note at this point that the From address when sending email must always be the valid email address you specified when registering. If this is not the case, then Mandrill refuses to provide the service, at least for the free trial accounts.

Line 7 in Listing 3 [5] grabs the text data arriving at the standard input and stores it in $text. If this string is empty because the previous script did not output anything to the Unix pipeline, line 10 terminates without an error message. In this case, nothing would be sent. In line 13, replace $api_key with your Mandrill test key and the sender address in $from (line 14) with the email you used to register. You can specify the receiving address to suit your needs.

The script uses the to_json function from the JSON module on CPAN to encode the email data and then sends it to the Mandrill server via a POST request. If line 32 does not detect any errors, the email was sent, and you can check your smartphone before you rush off to work to see whether the weather service advises taking the risk or taking an umbrella just in case.

Infos

  1. Weather Underground: http://wunderground.com
  2. Weather Underground, developer API: http://www.wunderground.com/weather/api
  3. Probability of precipitation: http://en.wikipedia.org/wiki/Probability_of_precipitation
  4. Mandrill email service http://mandrillapp.com
  5. Listings for this article: ftp://ftp.linux-magazin.com/pub/listings/magazine/172

The Author

Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News