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
01 #!/usr/local/bin/perl -w 02 use strict; 03 use WWW::Wunderground::API; 04 use DateTime; 05 use Log::Log4perl qw(:easy); 06 use Getopt::Std; 07 08 getopts "v", \my %opts; 09 Log::Log4perl->easy_init( 10 $opts{ v } ? $DEBUG : $ERROR); 11 12 my @critical_hours = qw(8 9 17 18 19); 13 14 my $w = WWW::Wunderground::API->new( 15 location => "San Francisco, CA", 16 api_key => "0123456789abcdef", 17 auto_api => 1, 18 ); 19 20 for my $e (@{ $w->hourly }) { 21 if( !scalar @critical_hours ) { 22 last; 23 } 24 25 my $dt = DateTime->from_epoch( 26 epoch => $e->{ FCTTIME }->{ epoch } ); 27 28 if( $dt->hour() == $critical_hours[0] ) { 29 DEBUG "Checking hour ", $dt->hour(), 30 " ($e->{ pop }%)"; 31 32 if( $e->{ pop } > 50 ) { 33 printf "Take your Umbrella! " . 34 "(%d%% rain at %s)\n", 35 $e->{ pop }, $dt->hms(); 36 } 37 38 shift @critical_hours; 39 } 40 }
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
01 #!/usr/local/bin/perl -w 02 use strict; 03 use warnings; 04 use LWP::UserAgent; 05 use JSON qw(to_json); 06 07 my $text = join "", <>; 08 09 if( !length $text ) { 10 exit 0; 11 } 12 13 my $api_key = "abcdefghi0123456789"; 14 my $from = 'from@email-address.com'; 15 my $to = 'to@email-address.com'; 16 17 my $ua = LWP::UserAgent->new(); 18 19 my $resp = $ua->post( 20 "https://mandrillapp.com/api/1.0/" . 21 "messages/send.json", 22 Content => to_json( { 23 key => $api_key, 24 message => { 25 text => $text, 26 subject => $text, 27 from_email => $from, 28 to => [ { email => $to } ], 29 } } ), 30 ); 31 32 if( $resp->is_error() ) { 33 die "Sending mail failed: ", 34 $resp->message(); 35 }
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:
0 7 * * * umbrella | mandrill-email
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.
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
- Weather Underground: http://wunderground.com
- Weather Underground, developer API: http://www.wunderground.com/weather/api
- Probability of precipitation: http://en.wikipedia.org/wiki/Probability_of_precipitation
- Mandrill email service http://mandrillapp.com
- Listings for this article: ftp://ftp.linux-magazin.com/pub/listings/magazine/172
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.