Hi,
Here in Holland I use the nice template from carterlake.
Zofar almost every thing is working. Only the forcast I cannot get it working here.
The nws is not so detailed forcast for Holland. But the weatherunderground looks like OK for here.
So i tried the perl script on my webserver, it looks like it is working.
I changed the location where the info from WU with the query HILVERUM, (got a nice page on weatheruntherground)
It produce the forcasts files, but is looks still old, and not the info from WU.
attached is the Dutch version of the WU-forecast.pl file – it creates a 5-day and 7-day ( 5-daagse verwachting, 7-daagse verwachting) html files for you to include in your site.
Best regards,
Ken
Here’s the Perl code (also in the .zip file)
#!/usr/bin/perl
# This little hack is to pull webpages, parse the content and put in
# a file for posting on the saratoga-weather.org website
#
# Ken True
#
$version = 'WU-forecast.pl V1.00 - 27-Feb-2007';
$doCSS = 0; # set to 1 to print CSS and page head
require LWP::UserAgent;
require HTTP::Headers;
$outfilenameHTML = './forecast-5day.htm'; #XHTML 1.0 compliant (hopefully)
$outfilenameHTMLFULL = './forecast-7day.htm'; # Full WU 7-day forecast
$quiet = 0;
$|=1;
$ua = new LWP::UserAgent;
print "$version\n";
open (HTML, ">$outfilenameHTML") || die "..unable to open $outfilenameHTML: $!\n";
open (HTMLFULL, ">$outfilenameHTMLFULL") || die "..unable to open $outfilenameHTMLFULL: $!\n";
#open (LOG,">>WU-forecast.log");
&get_weather_WU;
close LOG;
close HTML;
close HTMLFULL;
print STDERR "..wrote to $outfilenameHTML and $outfilenameHTMLFULL.\n";
exit;
#----------------------------------------------------------------------------
sub get_weather_WU {
my($req) = new HTTP::Request('GET', 'http://dutch.wunderground.com/cgi-bin/findweather/getForecast?query=Hilversum');
$req->header('Cache-control' => 'no-cache');
$req->header('Pragma' => 'no-cache');
my($content) = $ua->request($req)->content();
my($hdrs) = $ua->request($req)->headers;
my($dt) = scalar localtime(time);
print "..$dt X-Cache: ",$hdrs->header('X-Cache'),"\n";
my(@lines) = split(/\n/,$content);
# open (IN,"Greek-getForecast.htm");
# while (<IN>) {
# s/\n//;
# push(@lines,$_) if $_;
# }
my($i) = 0;
my($line) = "";
my(@newlines);
while ($i<=$#lines) {
if ($lines[$i] =~ /5-daagse verwachting/i) {
# $i = $i -2;
last;
}
$i++;
}
return unless $i;
if ($doCSS) { print HTML <<EOH1;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>
<body>
<!-- generated by $version -->
<style>
BODY { font-size: 12px; font-family: Arial, sans-serif; background-color: #FFF; color: #000; margin-top: 0px; margin-left: 0px; margin-right: 0px; }
H3 { padding-left: 5px; padding-top: 2px; padding-bottom: 2px; margin: 0px; font-size: 13px; font-weight: bold; background-color: #008; color: #FFF; border-top: 1px solid #4245AD; border-left: 1px solid #4245AD; border-right: 1px solid #333; border-bottom: 1px solid #333; }
H4 { padding: 2px; margin: 0px; font-size: 12px; font-weight: bold; background-color: #CCC; border: 1px solid #EEE; }
H5 { padding: 2px; margin: 0px; font-size: 12px; font-weight: normal; background-color: #E5E5E5; border-top: 1px dotted #CCC; text-align: center; }
DIV { margin: 0px; padding: 0px; }
TD.sortH { padding: 5px; background-color: #CCC; color: #000; font-size: 11px; font-weight: bold; font-family: Verdana; border-top: 1px solid #EEE; border-left: 1px solid #EEE; border-right: 1px solid #999; border-bottom: 1px solid #999; cursor: pointer; }
TD.sortC { color: #333; font-family: Verdana; font-size: 11px; padding: 1px; padding-left: 5px; padding: 5px; border-bottom: 1px solid #CCC; }
#message3 { text-align: center; background-color: #F5F5F5; color: #000; border: 1px solid #CCC; padding: 2px; }
#p5 { padding: 5px; }
#b { font-weight: bold; }
.taC { text-align: center; }
.boxB { border: 1px solid #008; background-color: #FFF; vertical-align: top; }
</style>
EOH1
;
}
print HTML <<EOH1A;
<!-- $version -->
<!-- Wunderground Forecast -->
<div class="WU">
<table>
<tr>
EOH1A
;
my @days = ();
my @conds = ();
my @temps = ();
while ($i<=$#lines) {
$line = $lines[$i];
$line =~ s!href="/cgi-bin/findweather!href="http://www.wunderground.com/cgi-bin/findweather!;
$line =~ s!'!''!g;
$line =~ s!5[- ]Day Forecast!<td><h3>Weather Underground 5-Day Forecast!;
$line =~ s!</h3>!</h3></td></tr><TR><TD>!;
$line =~ s!</table>!</table></TD></TR></TABLE>!;
$line =~ s!
!
!gi;
$line =~ s!id="b"!!;
$line =~ s!<img ([^>]+)!<img $1 /!;
$line =~ s!title="([^"]+)"!alt="$1" title="$1"!;
$line =~ s!TR>!tr>!g;
$line =~ s!TD>!td>!g;
$line =~ s!TABLE>!table>!g;
$line =~ s!bgcolor="#f6f6f6" style="!style="background-color: #F6F6F6; !;
if ($line =~ m/href\="/ && $line =~ /\&/) {
$line =~ s!\&!\&!g;
}
print HTML "$line\n" unless $line =~ /DisplayMOS.asp|^\s+for$|ZIP|Today''s|DisplayPollen/;
last if $line =~ m|</table>|;
$i++;
}
#print STDERR join(":",@days), "\n";
#print STDERR join(":",@conds), "\n";
#print STDERR join(":",@temps), "\n";
$line = join("\t",join(":",@days),join(":",@conds),join(":",@temps));
#print LOG "$dt\tWUG\t$line\n";
EOH2
;
print HTML <<EOH2A;
</div>
<!-- end of Wunderground forecast -->
EOH2A
;
# now extract the 7-day text forecast with icons
while ($i<=$#lines) {
if ($lines[$i] =~ /<table CELLPADDING=2 BORDER=0/i) {
# $i = $i -2;
last;
}
$i++;
}
print HTMLFULL <<EOH3A;
<!-- $version -->
<!-- Wunderground Forecast -->
<div class="WU">
EOH3A
;
while ($i<=$#lines) {
$line = $lines[$i];
$line =~ s!href="/cgi-bin/findweather!href="http://www.wunderground.com/cgi-bin/findweather!;
$line =~ s!'!''!g;
print HTMLFULL "$line\n";
last if $line =~ m|</table>|;
$i++;
}
print HTMLFULL <<EOH3B;
</div>
<!-- end of Wunderground forecast -->
EOH3B
;
} # end --- get_weather
#----------------------------------------------------------------------------
Thanks for the quick response and solution. I have it working now without the javascript part. And let the cron update the pers script once every 6 hours.
thanks again.
You’re welcome!
I should have mentioned that the modified script doesn’t create JavaScript any more – it creates two html pages so folks with Server-Side Includes enabled can include the contents directly in their pages. If you need the JavaScript version of the output, let me know… it’s easy to add that code back into the script.
Hi Ken
An other problem is the forecast, at the radar image on the right site is a forecast of only the today, and tomorrow text only.
Can that be solved with a java script stripping out only the today and the tomorrow text forecast?
All the info is allready in the forcast-7dayhtm file…
That would be nice.
I have grabbed your Wunderground perl script (many thanks) and played with it for a London forecast no problem but can you tell me how to change the background colour for the 5 day weather forecast as it is driving me mad. White is OK but would rather have it fit in with the rest of my colours.
In Firefox forecast shows as transparent so background is correct colour but in IE it shows as a white background.
Steve,
You’re welcome for the script – it’s an old warhorse that served me well (in several incarnations) when my site was a one-pager and no other scripting was available (no SSI, no PHP etc).
The Perl script doesn’t add any styling to what is on the Wunderground page itself, and leaves the class= and id= attributes as it found them on the WU page. That said, it’s probably best to put a bit of CSS in the calling page to perform the styling you’d like. The overall WU text is surrounded by a
, the <table class=“boxB” …> surrounds the inner Icon display, and each row has around it.
Putting styling for WU, boxB or taC should let you get rid of the non-transparent background
I took a closer look at the script and found that you can add ccs style to the html page “$doCSS = 0; # set to 1 to print CSS and page head”
so set to 1 added the style changes and bobs your uncle…cool.
Now If I can just get the perl script to run under cron #-o (works OK in the command window) it will be smiles all round.
error log host
[Thu Aug 30 21:43:46 2007] [error] [client 84.80.167.162] malformed header from script. Bad header=WU-forecast.pl V1.00 - 27-Feb-: WU-forecast.pl
[Thu Aug 30 21:43:46 2007] [warn] /WU-forecast.pl did not send an HTTP header
[Thu Aug 30 21:43:46 2007] [error] …unable to open ./forecast-5day.htm: Permission denied\n
malformed header ?
#!/usr/bin/perl
wrong ?
I suppose WU-forecast.pl creates the html forecast files, what’s wrong with header ?
I only used NotePad to open the script
Is the Path To Perl right? I have a cgi-bin
The script was transferred using FTP and ASCII mode
The script and directory has CHMOD 755
Part of my script
#!/usr/bin/perl
# This little hack is to pull webpages, parse the content and put in
# a file for posting on the saratoga-weather.org website
#
# Ken True
#
$version = 'WU-forecast.pl V1.00 - 27-Feb-2007';
$doCSS = 0; # set to 1 to print CSS and page head
require LWP::UserAgent;
require HTTP::Headers;
$outfilenameHTML = 'www.weersverwachtingen.eu/forecast-5day.htm'; #XHTML 1.0 compliant (hopefully)
$outfilenameHTMLFULL = 'www.weersverwachtingen.eu/forecast-7day.htm'; # Full WU 7-day forecast
$quiet = 0;
$|=1;
$ua = new LWP::UserAgent;
print "$version\n";
open (HTML, ">$outfilenameHTML") || die "..unable to open $outfilenameHTML: $!\n";
open (HTMLFULL, ">$outfilenameHTMLFULL") || die "..unable to open $outfilenameHTMLFULL: $!\n";
#open (LOG,">>WU-forecast.log");
&get_weather_WU;
close LOG;
close HTML;
close HTMLFULL;
print STDERR "..wrote to $outfilenameHTML and $outfilenameHTMLFULL.\n";
exit;
#----------------------------------------------------------------------------
sub get_weather_WU {
my($req) = new HTTP::Request('GET', 'http://dutch.wunderground.com/cgi-bin/findweather/getForecast?query=amsterdam, netherlands');
etc etc
what is a cron setting ? do I have to add it somewhere ?
malformed header from script. Bad header=WU-forecast.pl V1.00 - 27-Feb-: WU-forecast.pl
Is possibly caused by an invalid path to perl in the
#!/usr/bin/perl
Line. are you sure that you have access to perl via that path?
The second:
…unable to open ./forecast-5day.htm: Permission denied\n
Indicates a permissions issue opening a file which is displayed. This might be actually related to the first, but it is hard to tell. It acts as if Perl output the error.
It is sometimes hard to diagnose Perl issues without shell access to the account. Also, webservers sometimes use different default paths and users to execute external scripts. Which is why PHP is better if it can be used instead.
hi, edje
i did try that script…work ok here, i can have dutch or english just need for Greek…lol
i run script in a directory and make same directory the files.(i use windows xp)
i did not change the lines…
$outfilenameHTML = ‘./forecast-5day.htm’; #XHTML 1.0 compliant (hopefully)
$outfilenameHTMLFULL = ‘./forecast-7day.htm’; # Full WU 7-day forecast
i did change only the name of town…
my($req) = new HTTP::Request(‘GET’, ‘http://dutch.wunderground.com/cgi-bin/findweather/getForecast?query=amsterdam’);
The script was originally intended to be run on my weather station (WinXP) PC, so I’d not necessarily put in the extra stuff to run it well as a cron function on Linux/Unix … the perl -w is a good add Pinto!
To adapt the script for other countries/languages, you need to find the local-language presentation on Wunderground for your forecast, then copy that URL to HTTP:Request argument.
You may also need to change another line to detect the start of the 5 day forecast … the Dutch version looks for
if ($lines[$i] =~ /5-daagse verwachting/i) {
in the header of the 5 day forecast. the ‘5-daagse verwachting’ would have to be changed to the exact characters in the WU page for your language (look at the page with view source and copy that phrase into the program).
Since there seems to be a general interest in having the WU forecast (particularly for international folks who may have no other forecast available), I’ll see what I can do to create a ‘carterlake - style’ forecast PHP for WU. It might be easier for folks to use than that raw Perl script
the -w shows any errors if you run the script in a command window
I have this script running for London weather no problem this is on a linux server. There where a couple of minor errors but it would still work the errors have been resolved so now get a clean run on the script.
Since there seems to be a general interest in having the WU forecast (particularly for international folks who may have no other forecast available), I'll see what I can do to create a 'carterlake - style' forecast PHP for WU. It might be easier for folks to use than that raw Perl script
Best regards,
Ken
[
that’s a very nice and gentle idea, Ken !
(and a big change Panos gets his greek version )