#!/usr/bin/perl
## Emacs: -*- tab-width: 4; -*-
sub Usage
{
return << "END";
Usage:
$0 ZipCode [StartHourFromNow] [StartDayFromNow]
getweather is a utility that uses LWP::Simple and Data::CTable to
convert a local weather report from weather.com into a text-based
tabular report.
The URL it gets its data from is something like this:
http://www.weather.com/weather/hourbyhour/94062?dayNumber=1&hour=0
ZipCode is the required US zip code to ask about (e.g. 94062)
The other parameters don't seem to be working with anything but their
default values for now, so you might not be able to use them:
StartHourFromNow is how many hours in future to begin table (default 0).
StartDayFromNow is how many days in future to begin table (default 1).
Program output should look something like this:
+-----+--------+-------+---------------+------+-------+------+-----+-----------+--------+
| Day | Date | Hour | Forecast | Temp | Feels | Dew | Hum | Wind | Precip |
+-----+--------+-------+---------------+------+-------+------+-----+-----------+--------+
| Wed | Apr 24 | 5 AM | Foggy | 46 F | 45 F | 41 F | 84% | W 3 mph | 0% |
| Wed | Apr 24 | 6 AM | Foggy | 47 F | 47 F | 42 F | 83% | WSW 3 mph | 0% |
| Wed | Apr 24 | 7 AM | Foggy | 48 F | 48 F | 42 F | 80% | WSW 3 mph | 0% |
| Wed | Apr 24 | 8 AM | Foggy | 51 F | 51 F | 43 F | 74% | SW 3 mph | 0% |
| Wed | Apr 24 | 9 AM | Partly Cloudy | 54 F | 54 F | 43 F | 67% | SW 3 mph | 0% |
| Wed | Apr 24 | 10 AM | Partly Cloudy | 58 F | 58 F | 44 F | 60% | WSW 4 mph | 0% |
| Wed | Apr 24 | 11 AM | Partly Cloudy | 61 F | N/A | 44 F | 53% | W 5 mph | 0% |
| Wed | Apr 24 | 12 PM | Partly Cloudy | 64 F | N/A | 44 F | 48% | W 6 mph | 0% |
+-----+--------+-------+---------------+------+-------+------+-----+-----------+--------+
This program makes a few assumptions about the HTML code generated by
weather.com. Changes in the output there could make the program stop working.
This tool is part of the Data::CTable distribution.
Copyright (c) 1995-2002 Chris Thorman. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
See Data::CTable home page for further info:
http://christhorman.com/projects/perl/Data-CTable/
END
;
}
use strict;
use LWP::Simple qw(get $ua);
use Data::CTable qw(min max);
## Data::CTable does not require Data::ShowTable, but its out() method
## does, so we opt to fail here rather than just get a warning from
## $t->out().
use Data::ShowTable;
## Get command-line args and defaults.
my ($Zip, $Hours, $Days) = @ARGV;
int($Zip) > 0 or die &Usage();
$Zip = sprintf("%05d", $Zip);
$Hours = int($Hours); $Hours ||= 0;
$Days = int($Days); $Days ||= 1;
## Be IE.
$ua->{agent} = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
## Get the data.
my $URL = "http://www.weather.com/weather/hourbyhour/$Zip?dayNumber=$Days&hour=$Hours";
my $HTML = get($URL) or die "Failed to get $URL (no network connection?)\n";
## Create a trivial text-only version of the page to display in case
## we can not extract the tabular weather data we want.
my $Clean = $HTML;
$Clean =~ s// /gs;
$Clean =~ s/
/\n/gis;
$Clean =~ s/<.*?>/ /gs;
$Clean =~ s/ / /gis;
$Clean =~ s/(\s*\n\s*)+/\n/g;
$Clean =~ s/[ \t]+/ /g;
$Clean =~ s/^[ \t]+//gm;
## Find a table on the page that mentions "dewpoint"
my $Tables = [$HTML =~ m(