Weather-Underground-StationHistory

 view release on metacpan or  search on metacpan

lib/Weather/Underground/StationHistory.pm  view on Meta::CPAN

package Weather::Underground::StationHistory;

use 5.006000;

use strict;
use warnings;

use version; our $VERSION = qv('v1.0.5');

use Exporter;
use base 'Exporter';

use Regexp::Common;

our @EXPORT_OK =
    qw{
        &generate_single_day_station_history_url
        &strip_garbage_from_station_history
    };
our %EXPORT_TAGS = (
    all => [@EXPORT_OK],
);

my $EMPTY_STRING = q<>;

sub generate_single_day_station_history_url {
    my ($station_id, $year, $month_number, $day_of_month) = @_;

    return
        sprintf
            'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=%s&year=%d&month=%d&day=%d&graphspan=day&format=1',
            $station_id,
            $year,
            $month_number,
            $day_of_month,
        ;
} # end generate_single_day_station_history_url()

sub strip_garbage_from_station_history {
    my $original_contents   = shift;
    my @original_lines      = split m/ [\r\n]+ /xms, $original_contents;
    my $resulting_contents  = $EMPTY_STRING;

    foreach my $original_line (@original_lines) {
        $original_line =~ s/ $RE{balanced}{-parens => '<>'} //xmsg;
        $original_line =~ s/ $RE{ws}{crop}                  //xmsg;

        if ($original_line ne $EMPTY_STRING) {
            $resulting_contents .= "$original_line\n";
        } # end if
    } # end foreach

    return $resulting_contents;
} # end strip_garbage_from_station_history()


1; # Magic true value required at end of module
__END__

=for stopwords CSV

=head1 NAME

Weather::Underground::StationHistory - Utility functions for dealing with weather station historical data from L<http://wunderground.com>.


=head1 VERSION

This document describes Weather::Underground::StationHistory version 1.0.5.


=head1 SYNOPSIS

    use Weather::Underground::StationHistory qw{ :all };

    use LWP::Simple;

    print
        strip_garbage_from_station_history(
            get(
                generate_single_day_station_history_url(
                    'KILCHICA52',
                    2006,
                    10,
                    27,
                )
            )
        );


=head1 DESCRIPTION

This module provides a URL generator function for retrieving historical data
for weather stations from Weather Underground (L<http://wunderground.com>).

Additionally, a function to clean up the data retrieved from said URLs is
provided.  Nominally, the content retrieved from the URLs is in CSV (Comma
Separated Values) format.  If you enter these URLs into a web browser, the data
does appear to be in that format.  However, the MIME type given for the data by
the web server is C<text/html> and the data contains C<< <br> >> tags and HTML
comments (though no C<< <html> >>, C<< <head> >>, or C<< <body> >> tags that



( run in 1.062 second using v1.01-cache-2.11-cpan-71847e10f99 )