in reply to weather fetching script

I'm in a generous mood today, so here is a re-write of your script that should work a bit better than the original:
#!/usr/bin/perl -w use strict; use HTML::TokeParser; use LWP::UserAgent; my %cities = ( "City" => { var => 'adelmax' }, "Elizabeth" => { var => 'elizmax' }, "Noarlunga" => { var => 'noarmax' }, "Mount Barker" => { var => 'mtbkmax' } ); my $ua = LWP::UserAgent->new(); $ua->timeout(60); my $request = HTTP::Request->new('GET', 'http://www.bom.gov.au/cgi-bin +/wrap_fwo.pl?IDS10034.txt'); my $result = $ua->request($request); my $content = $result->content; my $p = HTML::TokeParser->new(\$content); my @lines; while (my $tag = $p->get_tag("pre")) { my $text = $p->get_text("/pre"); @lines = split /\n/, $text; } my $adeldesc; my $cnt; for my $line (@lines) { if ($line =~ /^Forecast for/ && !$adeldesc) { $adeldesc = $lines[$cnt+1]; } if ($line =~ /^([\w\s]+):\s+Max\s(\d+)/) { $cities{$1}{max} = $2; } $cnt++; } my $out = "var adeldesc = $adeldesc\n"; for (keys %cities) { $out .= qq(var $cities{$_}{var} = "$cities{$_}{max}"\n); } open W, ">", "weather.js" or die "Could not open weather.js for writing:$!\n"; print W $out; close W; print <<EOF; Content-Type: text/x-javascript Pragma: no-cache Cache-Control: no-cache Expires: -1 $out EOF
Output:
Content-Type: text/x-javascript Pragma: no-cache Cache-Control: no-cache Expires: -1 var adeldesc = Fine and cool day with increasing high cloud. Moderate + east to northeast winds. var mtbkmax = "11" var noarmax = "13" var adelmax = "13" var elizmax = "13"

As cwry pointed out, the reason it probably broke in the first place is because our good friends (and my ex-collegues) at the BOM decided to change "Adelaide City" to just "City". However, there were several other bad™ practices exhibited in your code, such as:

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^2: weather fetching script
by pip (Novice) on Jul 13, 2006 at 11:46 UTC
    Hi Darren, Thanks for the quick reply. The script fetches the description ok, but unfortunately not the actual temperatures. I can't get it to display the description in html, not sure if that is because of the missing enclosing tags "abcdef"; or the page itself. I'll do a mock js results page to fix that side of things. thanks again, pip
      Okay, well that's because the content on layout of the page actually changes according to the time of day. Later in the afternoon they start including the forecast overnight minimums as well. Given that I used to work for them, I should have known this - but hey, it was a few years ago ;)

      Anyway, this is easy to get around. Given that you aren't interested in the minimums, you can simply alter the pattern match on line 40 to read as follows:

      if ($line =~ /^([\w\s]+):.*?Max\s+(\d+)/) {

      The change is that we include a non-greedy .* to skip over anything until it reaches "Max".

      With that change (and the current content as at 9:30PM CST), I get:

      var adeldesc = Fine and cool with increasing high cloud. Moderate eas +t to northeast winds. var mtbkmax = "9" var noarmax = "11" var adelmax = "10" var elizmax = "11"

      Cheers,
      Darren :)

        Hi Darren, Thanks for the mod for the script, temps now show up. Sorry for being a pain...but how can I firstly: get the description being shown in "quotation marks" ? (html page shows errors if it doesn't have them)and secondarily: how do I get the script to write to /home/aml33704/public_html/ ? (Can't get the results to show unless outside cgi-bin) I am very grateful for your help! cheers, pip
A reply falls below the community's threshold of quality. You may see it by logging in.