🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chi.internet
Article #120

Re: Parse wunderground.com

#120
From: Cydrome Leader
Date: Mon, 26 Nov 2012 00:56
152 lines
7251 bytes
Mark Anderson <mea@nospambradnylion.com> wrote:
> While listening to the Bears game I wrote this little perl script to 
> parse the output of wget to wunderground.com and get the current temp, 
> wind speed, and a bunch of other stuff and put it into a comma 
> deliminated file.  I figured I'd post it here in case anyone needs to 
> extract weather info from that site.  If they change up the web site 
> this script will probably need a tweak.  Hopefully this saves someone 
> some time.  I checked out the wunderground perl CPAN module but I wanted 
> to target the station in my neighborhood which couldn't be done with 
> that module.  The temp here can be many degrees different than the temp 
> at Ohare.
> 
> Wget writes a file ddd.dat with the wunderground raw html data which the 
> perl script reads.  There may be  more elegant ways of doing this.  The 
> bash shell script calls wget, the perl script, and timestamps the comma 
> deliminated data.  I have it currently set to loop every 15 minutes but 
> will probably extend that to an hour.  The perl script is the meat and 
> potatoes since I had to decipher their html structure and attributes 
> they use to id the various data.
> 
> I use a simple bash shell script in an infinite loop to call it.
> (change /home/mea to your path)
> 
> #!/bin/bash
> 
> # Script to log temperature data from
> # wunderground.com
> 
> 
> for (( ; ; ))
> do
>        wget "paste_your_location_specific_wunderground_url_here"  -O 
> /home/mea/ddd.dat  -a /home/mea/log.dat
> 
> 
> /home/mea/parse_weather.sh
> echo "," `date +%H%M%m%d`
> 
> sleep 900
> done
> 
> 
> And then the perl script.
> 
> #!/usr/bin/perl
> use strict  ;
> use warnings  ;
> 
> # Parse weather underground for current
> # temperature.  The site will captured
> # using wget and placed into a file
> # called ddd.dat.
> #
> # This script just parses that html
> # and extracts the current temperature,
> # then timestamps and appends it to
> # weather.dat.  It is the responsibility
> # of the calling script to manage that
> # data (i.e. rotate it into a more permanent
> # location).
> 
> use IO::File ;
> use HTML::TreeBuilder ;
> 
> my @elements2 = {};
> my @stripchars = ( q[\xa0]  , q[\xb0] ,  "Feels Like " , "F")  ;
> 
>        my $document = ""  ;  #the input document
>        $document =  new IO::File;
>        $document ->  open( "/home/mea/ddd.dat" );
> 
>        my $tree = HTML::TreeBuilder->new; # empty tree
>        $tree->parse_file($document);
> 
> #---------LOOK DOWN HTML--------POPULATE @elements2-------------
> 
>        
>        @elements2 = $tree -> look_down( 
>                                sub {
>                                      $_[0] -> tag eq 'div'  or
>                                      $_[0] -> tag eq 'span' 
>                                }
>                        )  ;
> #       print "num of div elements=$#elements2\n"   ;
>        my $idx = ""  ;
>        my $temp = ""  ;
>        my $windspeed = ""  ;
>        my $windcompass = ""  ;
>        my $sunrise  = ""  ;
>        my $sunset   = ""  ;
>        my $cond = ""  ;
>        my $humidity = ""  ;
>        my $pressure = ""  ;
>        my $tempfeel = ""  ;
>        
>        foreach my $para (@elements2)   {
> 
>                if ($idx = $para -> attr("id")) {  
> 
>                        if ($idx eq "tempActual")  {
>                                $temp = $para -> as_trimmed_text  ;
>                                foreach my $strip (@stripchars) {
>                                        $temp =~ s/$strip//g  ;
>                                }
>                        }
>                        if ($idx eq "tempFeel")  {
>                                $tempfeel = $para -> as_trimmed_text  ;
>                                foreach my $strip (@stripchars) {
>                                        $tempfeel =~ s/$strip//g  ;
>                                }
>                        }
>                        if ($idx eq "windCompassContainer" )  {
>                                $windspeed = $para -> as_trimmed_text  ;
>                        }
>                        if ($idx eq "sRise")  {
>                                $sunrise = $para -> as_trimmed_text  ;
>                        }
>                        if ($idx eq "sSet")  {
>                                $sunset  = $para -> as_trimmed_text  ;
>                        }
>                        if ($idx eq "curCond")  {
>                                $cond = $para -> as_trimmed_text  ;
>                        }
>                        if ($idx eq "windCompass" )  {
>                                if($windcompass = $para -> attr("value")) {}
>                                else { $windcompass="error" ; }
>                        }
>                }
>                if ($idx = $para -> attr("pwsvariable")) {  
>                        if ($idx eq "baromin")  {
>                                if($pressure = $para -> attr("value")) {}
>                                else { $pressure="error" ; }
>                        }
>                        if ($idx eq "humidity")  {
>                                $humidity = $para -> as_trimmed_text  ;
>                        }
>                }
> 
>        }
>        print "$temp,$tempfeel,$windspeed,$windcompass,$sunrise,$sunset,
> $cond,$humidity,$pressure"  ;
> 
> 

wow. 

Why not just add 10 characters of awk to my version of whatever the hell
you're trying to do up there.

Anybody that uses perl isn't a programmer.

 wget -qO- "http://rss.wunderground.com/auto/rss_full/IL/Chicago.xml?units=english" | grep Temp
Technical Details
Message-ID: <k8uens$84b$1@reader1.panix.com>
Path: arc1-ca.pugleaf.net!q13ni112357wii.0!nntp.google.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newspeer1.nac.net!news.mi.ras.ru!spln!extra.newsguy.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news-out.readnews.com!transit3.readnews.com!panix!not-for-mail
References: <MPG.2b1c803350640b6b989a65@news.speakeasy.net>

Show Full Headers
Complete Article Headers
X-Google-Thread: 837b0c45a0,f007932fe2b14462
X-Google-Attributes: gid837b0c45a0,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.180.87.165 with SMTP id az5mr725311wib.1.1354445402074;
        Sun, 02 Dec 2012 02:50:02 -0800 (PST)
MIME-Version: 1.0
Path: arc1-ca.pugleaf.net!q13ni112357wii.0!nntp.google.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newspeer1.nac.net!news.mi.ras.ru!spln!extra.newsguy.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news-out.readnews.com!transit3.readnews.com!panix!not-for-mail
From: Cydrome Leader <presence@MUNGEpanix.com>
Newsgroups: chi.internet
Subject: Re: Parse wunderground.com
Date: Mon, 26 Nov 2012 00:56:28 +0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Lines: 153
Message-ID: <k8uens$84b$1@reader1.panix.com>
References: <MPG.2b1c803350640b6b989a65@news.speakeasy.net>
NNTP-Posting-Host: panix2.panix.com
X-Trace: reader1.panix.com 1353891388 8331 166.84.1.2 (26 Nov 2012 00:56:28 GMT)
X-Complaints-To: abuse@panix.com
NNTP-Posting-Date: Mon, 26 Nov 2012 00:56:28 +0000 (UTC)
User-Agent: tin/2.0.0-20110823 ("Ardenistiel") (UNIX) (NetBSD/5.1.2 (i386))
X-Received-Bytes: 6566