Someone brought to my attention that I had failed to define a
couple of variables in the sample code I posted and they were
quite right.  I don't mind sharing my work but the entire
application I wrote to get a brief local weather summary is
242 lines and I was trying to stay close to the topic, here, so I
had shortened it and shortened it a bit too much.  Here is sample
code that will actually run.  If you want to experiment with it,
it should work fron anywhere in the world but practically, I
doubt it works outside the united states though people in other
countries can look up weather in US cities if you first get on the
noaa.gov web site

  http://noaa.gov

and enter the city name you are interested in.  If you have a US
postal zip code, that will work.  What you want is an xml file
containing the local weather conditions for that location.  You
will also get latitude and longitude which can help you get
sunrise and sunset data from another web site that needs those
data to give you the right table.

In other words, you must set $wxfile to the name of the file for
your city of interest.

        This code downloads the file for Stillwater, Oklahoma and
the information in the xml file says that the recommend pickup
time is 15 minutes past the hour so it modifies the time stamp to
show 15 minutes past your current hour.  When you look at the
code, you will see that it compares the current number of
localtime seconds to the mtime value of the present file.  It
will not get it again until 3600 seconds or 1 hour has passed
since 15 minutes past the hour in which you downloaded it.

Here is working code.  I did run it through perltidy but no
telling what the mailing process will do so you will need to
probably run perltidy again after you save the code.

        I am sorry for any confusion I caused.

Cut here.

#!/usr/bin/perl -w
use strict;
use warnings::unused;
use Data::Dumper;
use XML::Simple;
use File::Basename;
use Cwd;
use File::stat;
use Time::Local;
my $homedir = "/tmp";
my @t       = localtime(time);
my $utcsec  = timelocal(@t);
my $wxfile  = 'display.php?stid=KSWO';

#I want that file to end up in a specific directory so:
my $wxpath                = $homedir . "/" . $wxfile;
my $day                   = 86400;                           #seconds
my $hour                  = 3600;
my $gmt_offset_in_seconds = timegm(@t) - timelocal(@t);
my $tzoffset              = $gmt_offset_in_seconds / 3600;

#many thanks to whoever wrote the quick way to determine TZ
#offset from utc

my $wxlast_update_time;

#Grab conditions from NOAA if the file is stale or missing.
if ( !stat($wxpath) ) {    #The file is not there.
    system(
        "curl -s -o $wxpath 'http://w1.weather.gov/xml/current_obs/$wxfile'");

    #Change the mtime to a quarter past last hour.
    my $when = timelocal( 0, 15, $t[2], $t[3], $t[4], ( $t[5] - 100 ) );
    utime $when, $when, "$wxpath";
}         #The file is not there.
else {    #what normally happens
    my $wxstatRef = stat($wxpath);
    $wxlast_update_time = $wxstatRef->mtime();
    my $wxage = ( $utcsec - $wxlast_update_time );
    if ( $wxage >= $hour ) {    #File needs to be refreshed.

        #The file should not be more than 3600 seconds old.
        system(
            "curl -s -o $wxpath 'http://w1.weather.gov/xml/current_obs/$wxfile'"
        );

        #Change the mtime to a quarter past.
        my $when = timelocal( 0, 15, $t[2], $t[3], $t[4], ( $t[5] - 100 ) );
        utime $when, $when, "$wxpath";
    }    #File needs to be refreshed.
}    #what normally happens

# create object
my $xml = new XML::Simple;

# read XML file
my $data = $xml->XMLin("$wxpath");

print Dumper($data);

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to