Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer


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.

use strict;
use warnings;

my $main_url = "";
my $entry_point = "";
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",
                                # cc=>'',
            my $fh = $msg->open;
            print $fh "$forecast";

Replies are listed 'Best First'.
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://197453]
[shmem]: ...not as an attachment, it's probably better to c&p that into the mail body

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2017-04-24 12:53 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (439 votes). Check out past polls.