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
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.