Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask


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]
[chacham]: I'm sure this is old, but i just came across it: If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl programs.
[LanX]: lol
[Corion]: Woo hoo! A new Auralnauts episode!
LanX mean unfair and funny
[LanX]: but most java code looks like from one million monkey using eclipse. ..

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2018-04-23 16:03 GMT
Find Nodes?
    Voting Booth?