Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Meteorology::Australia

by jens (Pilgrim)
on Sep 13, 2002 at 06:31 UTC ( #197453=snippet: print w/ replies, xml ) Need Help??

Description: As promised in my previous post Weather goes thou, Spider?, the following code lets you automate checking the weather in Australia from the Bureau of Meteorology.

If you live in Australia (or just have a perverse fascination with foreign climes), you can set up the following program as a cron job to email you the weather at the time of your choosing. (Note that this requires manual configuration of your own cron/anacron settings.)

Note: You need to change the appropriate URL to indicate the state/region information you want, and the email address(es) you want it to go to.
#!/usr/bin/perl

use strict;
use warnings;

my $main_url = "http://www.bom.gov.au";
my $entry_point = "http://www.bom.gov.au/weather/vic/forecasts.shtml";
my $webpage = undef;

use HTML::TokeParser;
use LWP 5.65;
use Mail::Send;

    my $browser = LWP::UserAgent->new;
    my $response = $browser->get( $entry_point );
    if ($response->is_success)
    {
        my $text = $response->content;
        my $link = HTML::TokeParser->new(\$text);

#look for the link to the actual Melbourne Precis Forecast
        while (my $token = $link->get_tag("a"))
        {
            my $url = $token->[1]{href} || "-";
            my $text = $link->get_trimmed_text("/a");
            $webpage = $url
                if ($text =~ m/Melbourne Precis Forecast/);
        }
  my $response = $browser->get($main_url . $webpage);

            die "Can't get $webpage -- ", $response->status_line
        unless $response->is_success;

         die "Hey, I was expecting HTML, not ",$response->content_type
        unless $response->content_type eq 'text/html';

        my $webpage_contents = $response->content;

        my $pre = HTML::TokeParser->new( \$webpage_contents );

        if ($pre->get_tag( "pre" )) {
            my $forecast = $pre->get_text;

            my $msg = new Mail::Send Subject=>"Today's Forecast",
                                 To=>'YOUREMAIL@YOURADDRESS.com.au',
                                # cc=>'',
                                ;
            my $fh = $msg->open;
            print $fh "$forecast";
            $fh->close;
        }
    }

Comment on Meteorology::Australia
Download Code
Re: Meteorology::Australia
by zentara (Archbishop) on Sep 13, 2002 at 15:28 UTC
    Worked for me with LWP 5.64.
Re: Meteorology::Australia
by quinkan (Monk) on Sep 20, 2002 at 05:24 UTC
    Hey, this only gets Melbourne weather -- and that's a one-liner once you've got all the Mail:Send stuff going:
    print "Morning showers, settling to rain. Rain tomorrow.\n";
    Sydney, on the other hand, takes: ...<stuff>..../nsw/forecasts.shtml .... <stuff> and needs to look for /Metropolitan Forecast/
      For Perth it is more like this-
      if (rand(100) > 5 ){ print "It's going to be sunny!\n"; } else { print "Slight chance of showers today.\n"; }
      :-)

      --
      Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://197453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-08-29 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls