Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2012/08/10

Code I Am Not Proud Of

I have a shell script that uses espeak to tell me the time and temperature, which I crontab to run at the top of every hour. I had been using the Google Weather shadow API, the one that's connected to the doomed iGoogle, but it stopped working on me this week.

This broke my code.

So, I've moved to using XML files from NOAA, which I probably should have used from the first.

sub temperature {
    my $parser = XML::DOM::Parser->new() ;
    my $file = 'http://w1.weather.gov/xml/current_obs/KLAF.xml' ;
    my $doc = $parser->parsefile( $file ) ;
    my $nodes = $doc->getElementsByTagName( "temp_f" ) ;
    my $n = $nodes->getLength ;
    my $temp ;

    for ( my $i = 0 ; $i < $n ; $i++ ) {
        my $node = $nodes->item ($i) ;
        my $str  = $node->toString;
        $temp = sprintf '%d' , ( split /[<>]/ , $str )[2] ;
        }
    return $temp ;
    }

I am fine with most of this. I get a parser. I use it to download the file, and get a document. I get an object full of nodes from that document, and I go through each one. Going through each one is where I hit the goofy.

I can get a node that gives me what I want. What I cannot seem to find is the way to get the value from the object. So, I go to regular expressions. There must be a way to get XML::DOM to do the right thing, but I couldn't find it. From there on, that's OK.

It makes me wish, though, that NOAA exported JSON.