🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chi.internet
Article #119

Parse wunderground.com

#119
From: Mark Anderson
Date: Mon, 26 Nov 2012 00:21
140 lines
5535 bytes
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"  ;
Technical Details
Message-ID: <MPG.2b1c803350640b6b989a65@news.speakeasy.net>
Path: arc1-ca.pugleaf.net!q13ni42036wii.0!nntp.google.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10d5fa,f007932fe2b14462,start
X-Google-NewGroupId: yes
X-Google-Attributes: gid837b0c45a0,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.180.87.170 with SMTP id az10mr2750712wib.0.1353889320147;
        Sun, 25 Nov 2012 16:22:00 -0800 (PST)
Path: arc1-ca.pugleaf.net!q13ni42036wii.0!nntp.google.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 25 Nov 2012 18:21:59 -0600
From: Mark Anderson <mea@nospambradnylion.com>
Newsgroups: chi.internet
Subject: Parse wunderground.com
Date: Sun, 25 Nov 2012 19:21:58 -0500
Message-ID: <MPG.2b1c803350640b6b989a65@news.speakeasy.net>
MIME-Version: 1.0
User-Agent: MicroPlanet-Gravity/3.0.4
Lines: 142
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 66.92.134.90
X-Trace: sv3-RV7VKVtXR80YUkJ8PC0JlyBMbDkrn6fHg6s1Jzl8CQdOExsPqXN2JhcSx7u5GsvLG3+MqzMeGoH+dOB!MTbMas6nULf6wXB5MDaqfA3g7lNyM50Ks/60sDrUh2+tVBiqnIIzFtxhnAqe5do83L6sTZ1w5I7V!zdARbf2n/Npoy4GzjFqj95i1tpVM
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 4990
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit