Weather Forecast

Jens Porup jens at cyber.com.au
Sun Sep 15 20:16:02 CDT 2002


Hi all.

Given the fickle, strange weather we've had over the last several
days, the following script might be of some use to you. 

With not enough work to keep me busy at the moment, I've been playing
around with LWP and HTML::TokeParser, and I've come up with a 
utility script you can run as a cron job to deliver the Bureau
of Meteorology weather forecast to you each day. 

If you're really mad about the weather, you could probably modify
the script to check every hour for the time stamp so it updates you 
when the Bureau puts out a new bulletin, but frankly I'm not that 
keen on keeping up to date with BOM.

Anyway, here it is. Any suggestions on coding style are, as always,
most welcome.

Cheers,

Jens

-----

#!/usr/bin/perl

use strict;
use warnings;

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

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

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");
		$destination_webpage = $url
			if ($text =~ m/Melbourne Precis Forecast/);
	}

	my $response = $browser->get( $main_url . $destination_webpage );

		die "Can't get $destination_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 $weather_report = HTML::TokeParser->new( \$webpage_contents );

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

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





More information about the Melbourne-pm mailing list