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; } }

In reply to Meteorology::Australia by jens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.