Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Ok, this is a wierd one, but I thought it was worth sharing anyway.

Some of you know that I enjoy sailing. The problem is you really never know when you'll get that urge to drop everything and go down to the marina. Sometimes this will happen at times when an immediate weather forecast isn't readily available. And in sailing, weather is important.

Sure, I could drive down to the marina and turn on the weather radio, or tune in the station on the car stereo, but where's the fun in that?

This script is the answer. I have set up a procmail filter that intercepts email messages sent from my AT&T Wireless cell phone to a particular email address. If the message has in its subject: "Forecast", and in its body a city, and a state abbreviation, the filter invokes the following script.

The script then goes to the NOAA website, grabs the forecast for the city in question (including storm warnings if there are any), and sends off a series of email messages back to my cellphone.

For testing purposes, if the script is not given an email address to respond to, it will just print the output.

Here's the tricky part. AT&T Wireless truncates email messages at 120 characters. So this script does its best to try to abbreviate most of the words in the forecast. The output takes a little getting used to, but almost always fits into 120 characters per day. The script sends one email per day, and one per storm-warning. So if you run it with an email address, it'll spew out six or more 120 character messages, each representing one day's worth of forecast, plus however many messages it takes to communicate any relevant storm warnings.

The usage is as follows:

First, using the defaults:

./weather.pl __OUTPUT__ tnite: mostly clr eve, low clouds/ fog ovrngt. lows arnd 60. W wind arnd 15 mph eve. Thu: low clouds/ fog am. othrwse mostly sunny. highs mid 60s to upr 70s. W wind 15 to 20 mph pm. Thu nite: mostly clr eve, low clouds/ fog ovrngt. lows lwr 60s. W wind 15 to 20 mph eve. Fri: low clouds/ fog am. othrwse mostly sunny. highs upr 60s/ 70s. Fri nite: mostly clr eve, low clouds/ fog ovrngt. lows lwr 60s. Sat - Wed: areas of nite/ am low clouds/ fog, othrwse mostly clr. highs near 70 at beach to mid 70s to lwr 80s inlnd. lows arnd 60.
This will give you the forecast for Los Angeles, CA. You can also invoke the script like this:
./weather.pl 'Los Angeles' 'CA'

...in which case, it will give you the forecast for the city and state you've specified.

If you want to supply an email address, you may also do that:

./weather.pl 'Los Angeles' 'CA' '1234567890@mobile.att.net'

And finally, for you windows folks (myself included), you may specify a mailhost to use for a direct SMTP mailing:

perl weather.pl "Los Angeles" "CA" "1234567890@mobile.att.net" "mail.yourhost.com"

If you don't want to bother with all the work of setting up an email filter to invoke the script, you may just set it up in a cron job or a Windows Scheduler job to have the forecast sent to your mobile device once every 24 hours.

If you're one of the lucky people whos cell phone carrier doesn't truncate messages at 120 characters, you should be able to easily modify the script to send everything at once, in one single email. ...not too difficult.

This is mostly a proof of concept for myself, and something that I just wanted to play with. I wanted to try out the Geo::WeatherNOAA. I hope it sparks some ideas out there... Here's the code:

use strict; use warnings; use Geo::WeatherNOAA; use MIME::Lite; my ( $city, $state, $email, $mailhost ) = @ARGV; $city = $city || 'Los Angeles'; $state = $state || 'CA'; if ( $^O =~ /MSWin32/ and defined $mailhost ) { MIME::Lite->send( 'smtp', $mailhost, Timeout=>60 ) } { my %abbrev = map { chomp; split /\s*\|\s*/ } <DATA>; my $keywords = join '|', keys %abbrev; sub abbreviate { my ( $prefix, $text ) = @_; my $out = lc ( $prefix . ': ' . $text ); $out =~ s/\b($keywords)\b/$abbrev{$1}/g; # Abbreviate $out =~ s/\.{3}/./; # Repunctuate $out =~ s/\s\bin\b//g; # drop 'in' $out =~ s/\s\bthe\b//g; # drop 'the' $out =~ s/\s*([,.;\/]+)\s*/$1 /g; # Too much space. return $out; } } my ( $date, $warnings, $forecast, $coverage ) = process_city_zone( $city , $state, '', 'get'); foreach my $warning ( @$warnings ) { if ( $warning =~ /\w/ ) { my $out = abbreviate ( '!!!', $warning ); if ( defined $email ) { my $msg = MIME::Lite->new( 'From' => $email, 'To' => $email, 'Subject' => 'Warning', 'Data' => $out ); $msg->send; } else { print "Warning:\n$out\n"; } } } foreach my $frame ( @{ prepare_forecast( $forecast ) } ) { if ( defined $email ) { my $msg = MIME::Lite->new( 'From' => $email, 'To' => $email, 'Subject' => 'Forecast', 'Data' => $frame ); $msg->send; } else { print "$frame\n"; } } sub prepare_forecast { my ( $info ) = shift; return [ map { abbreviate( $_, $info->{$_} ) } keys %$info ]; } __DATA__ tonight| tnite patchy| ptch clear| clr evening| eve cloudy| cldy overnight| ovrngt upper| upr morning| am afternoon| pm south| S north| N east| E west| W northeast| NE southeast| SE southwest| SW northwest| NW winds| wind monday| Mon tuesday| Tue wednesday| Wed thursday| Thu friday| Fri saturday| Sat sunday| Sun tomorrow| Tomrw through| - otherwise| othrwse then| , night| nite inland| inlnd beaches| beach lower| lwr and| / around| arnd

I've found that the NOAA website is kind of finacky about its forecast cities. I had to fiddle a bit to find which cities work and which don't. For example, for some reason, Oxnard, CA doesn't work.

Comments are welcomed; I'm always interested in sharpening my skills.


Dave


In reply to Weather Forecasts emailed to cell phone by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-01-22 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (60 votes). Check out past polls.

    Notices?