Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Checking the Weather

by rand0mmm (Acolyte)
on Mar 21, 2006 at 00:38 UTC ( [id://538093]=perlquestion: print w/replies, xml ) Need Help??

rand0mmm has asked for the wisdom of the Perl Monks concerning the following question:

I am an Artist, so of course I want to open different files based on the weather. ;-)

While I am learning Perl (one project a time) I have no idea how to fit my information into a whole working thing and pull off what conceptually seems like the right tool for the right job. So far I have been learning and using perl on my website server apps, (running a wiki) called by http. This is all backwards for me now.

I want to use Perl on my Mac OS X machine to create a script that periodically checks an url and then opens and runs different interactive patches I've made in one of my local applications; suffice to call them movies. My hope is the machine will perpetually check a webpage, find the weather, and open movies with the corresponding filename, every 7 minutes.

I want to make the changing weather info at page at http://www.weather.com/weather/local/12986? open "Partly Sunny.mov" when it's "Partly Sunny" on the page, or "Windy.mov" when it's "Windy", and "Thunderstorm.mov" when it's "Thunderstorms" on the page. etc.

Since I also need to distinguish between Sunny, Calm, Windy, Raining, Thunder+Lightning and also the time of day. I figure i will be making a set of different weather files for morning, day, dusk, and night. So it figures I will need some kind of MORNINGmap, DAYmap, DUSKmap, NIGHTmap, hash-array-tablething to map the time onto the weather onto the files to play.

I notice this weather page has a nice <B CLASS=obsTextA>Cloudy</B> structure surrounding my target info. I looked for more plaintext weather data and couldn't locate anything like an http: sms server.

I have explored some related ideas a bit, and there's a mac osX tool called Automator, that can hand a webpage as input to PERL. I couldn't make it do much yet, but maybe this will be useful on my path.

This seems like a big challenge to me right now, but I am hoping someone who understands PERL can show me how simple it is and put me on a clearer path.
Any suggestions are welcome. --- --just

Replies are listed 'Best First'.
Re: Checking the Weather
by Thelonius (Priest) on Mar 21, 2006 at 01:11 UTC
    If you already have LWP::Simple installed:
    use strict; use LWP::Simple; my $content = get("http://www.weather.com/weather/local/12986?"); die "Couldn't get it!" unless defined $content; my $weather; if ($content =~ m!<B CLASS=obsTextA>([^<]*)</B>!) { $weather = $1; } else { die "Can't find weather on page!\n"; } print "weather = $weather\n";
Re: Checking the Weather
by brian_d_foy (Abbot) on Mar 21, 2006 at 03:55 UTC

    CPAN has several Weather related modules which can fetch from various places. Since you want weather for Tupper Lake, New York, I figure that US weather services will be fine, so things such as Geo::WeatherNOAA will work, although if not there are several alternatives using different sources.

    To figure things like sunrise and sunset (or any of the twilights), I've found Astro::Sunrise quite helpful. Once you have the current time, compare it to the times you get from that module to see where you are in the day.

    Now you just have to put the pieces together. Good luck :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Checking the Weather
by inman (Curate) on Mar 21, 2006 at 09:18 UTC
    Weather.com provides an XML feed service which removes the need to take apart an HTML page. You should be able to read the XML using modules such as XML::Simple.
Re: Checking the Weather
by zentara (Archbishop) on Mar 21, 2006 at 11:26 UTC
    Any suggestions are welcome.

    This is a Tk program, but you might find some artistic insiration from it. :-) ztk-weather-radar


    I'm not really a human, but I play one on earth. flash japh
Re: Checking the Weather
by bfdi533 (Friar) on Mar 21, 2006 at 17:32 UTC

    Another alternative to get text-based info is to use Weather::Simple. Here is an example that uses Weather::Simple for Tupper Lake, NY. It returns a hash that has all the info you need.

    #!/usr/bin/perl use Data::Dumper; use Weather::Simple; # define parameters for weather search my %params = ( 'cache' => '/tmp/weathercache', 'partner_id' => 'somepartnerid', 'license' => '12345678', 'place' => 'Tupper Lake, NY', ); # instantiate a new weather.com object my $simple_weather = Weather::Simple->new(%params); my $weather = $simple_weather->get_weather(); print Dumper($weather);

    Update: Fixed a comment in the code above that would prevent it from retrieving the Tupper Lake weather.

Re: Checking the Weather
by bfdi533 (Friar) on Mar 21, 2006 at 17:31 UTC

    You can also use Net::Telnet to check the weather from wunderground.com (they have a telnet interface which is all text). Here is the example from the Net::Telnet documentation which does just that:

    my ($forecast, $t); use Net::Telnet (); $t = new Net::Telnet; $t->open("rainmaker.wunderground.com"); ## Wait for first prompt and "hit return". $t->waitfor('/continue:.*$/'); $t->print(""); ## Wait for second prompt and respond with city code. $t->waitfor('/city code.*$/'); $t->print("BRD"); ## Read and print the first page of forecast. ($forecast) = $t->waitfor('/[ \t]+press return to continue/i'); print $forecast; exit;

    The only problem with this telnet interface is that they do not have Tupper Lake, NY as a location you can get info on. They have several location throughout NY that may or may not be nearby.

    Update: Fixed a copy/paste error in the code segment above.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538093]
Approved by GrandFather
Front-paged by brian_d_foy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found