a perl script scraping xml data, imputing it into database, printing out the result then,matching it with a key in another database, before printing out the result with a key.


#!/usr/bin/perl


#########################################################
# This script connects to an xml weather feed from nyc, gets wind speed and direction, 
#inserts the values along with the timestamp into a database. The it looks to another database to
#find a matching number representing the direction and prints out the result.
#This information is then used by another program written in processing to create a dynamic animation.
# written by Marta Lwin with the help of Shawn Van Every
#########################################################

use CGI::Carp qw(fatalsToBrowser);
use DBI;
use Net::HTTP;

#########################################################
# Connect to the xml feed		
#########################################################

use Net::HTTP;
my $s = Net::HTTP->new(Host => "www.weather.gov") || die $@;
$s->write_request(GET => "/data/current_obs/KNYC.xml", 'User-Agent' => "Mozilla/5.0");
my($code, $mess, %h) = $s->read_response_headers;
my $a;
while (1) {
my $buf;
my $n = $s->read_entity_body($buf, 1024);
$all .= $buf;
die "read failed: $!" unless defined $n;
last unless $n;
#print $buf;
}


#########################################################
# Define our DB info
#########################################################

if    ($0=~m#^(.*)\\#){ $execDir = "$1"; }  # Win/DOS
elsif ($0=~m#^(.*)/# ){ $execDir = "$1"; }  # Unix
else  {`pwd` =~ /(.*)/; $execDir = "$1"; }  # Unix

# Get Login Info
require "$execDir/info.pl";

$database = $mySqlUsername;
$data_source = "dbi:mysql:$database";
$username = $mySqlUsername;
$password = $mySqlPassword;

#########################################################
# Connect to the database.
#########################################################

my $dbh = DBI->connect($data_source, $username, $password,
{'RaiseError' => 1, 'PrintError' => 1});


#########################################################
# user regular expressions to grab xml
######################################################### 
$direction = "wind_dir";
$mph = "wind_mph";
$_ = $all; #"<wind_dir>West</wind_dir>";

	if ( ~m/<wind_dir>([^<]+)</i ) { $direction = $1;}
	if ( ~m/<wind_mph>([^<]+)</i ) { $mph = $1;}      
print "wind direction is $direction and speed is $mph\n";


#########################################################
# insert into database
######################################################### 

$SqlStatement .= "INSERT INTO nycWind (wind_dir,wind_mph,time_change ) ";
$SqlStatement .= "VALUES ('$direction','$mph', NOW()) ";
$affected_rows = $dbh->do($SqlStatement);



#########################################################
#select from both databases 
#########################################################
$direction_id = 0;
$SqlStatement = "SELECT id FROM wind_dirID ";
$SqlStatement .= "WHERE direction = '$direction' ";
$sth = $dbh->prepare($SqlStatement);
$sth->execute();

#while (@row_array = $sth->fetchrow_array())
        #{       # Compute id
        #        $id .= ($direction);
         #       $direction .= $row_array[0];
        #}

print "wind_dir id is $direction_id and speed is $mph\n";

$sth->finish();

#########################################################
# Disconnect from the database.
#########################################################   
$dbh->disconnect ();
exit(0);