Output:#!/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
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 :)
In reply to Re: weather fetching script
by McDarren
in thread weather fetching script
by pip
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |