#!/usr/bin/perl -w
use strict;
 
use XML::RSS::Parser;
use LWP::Simple;
use DBI;

my $DB_host = "";
my $DB_login = "";
my $DB_passwd = "";
my $DB_name = "ece452";
my $DB_table = "Wunderground_RSS_weather";
my $DB_link = DBI->connect("dbi:mysql:database=$DB_name;host=$DB_host", $DB_login, $DB_passwd)
	or die "Couldn't connect to database: ".DBI->errstr;

# Wunderground global RSS configuration
my $RSS_units = "english";
my @RSS_urls;

my @RSS_zips = ("08601",	# Trenton
		"07724",	# Eatontown
		"08901",	# New Brunswick
		"07960",	# Morristown
                "07013",	# Clifton
		"07677",	# Woodcliff Lake
		"10022");	# Central Park

my %ZipCode_Latitude = ("08601", "40.2",
			"07724", "40.3",
			"08901", "40.5",
			"07960", "40.8",
			"07013", "40.9",
			"07677", "41.0",
			"10022", "40.8");

my %ZipCode_Longitude = ("08601", "-74.8",
			"07724", "-74.1",
			"08901", "-74.4",
			"07960", "-74.5",
			"07013", "-74.2",
			"07677", "-74.1",
			"10022", "-74.0");

foreach my $RSS_zip (@RSS_zips) {
        push (@RSS_urls, "http://rss.wunderground.com/auto/rss_full/NJ/$RSS_zip.xml?units=$RSS_units");
}

foreach my $RSS_url (@RSS_urls) {
        my $RSS_data = get $RSS_url;
        die "Couldn't get $RSS_url" unless defined $RSS_data;

	my $RSS_feed = XML::RSS::Parser->new;
	my $RSS = $RSS_feed->parse_string($RSS_data);

	my @_ZipCode = split(/\?/, $RSS_url);
	my $ZipCode = substr($_ZipCode[0], length($_ZipCode[0])-9, 5);

	my $Latitude = $ZipCode_Latitude{$ZipCode};
	my $Longitude = $ZipCode_Longitude{$ZipCode};

	my @Description = split(/ \| /, $RSS->query('//item/description')->text_content);

	my @_Temperature_F = split(/ /, $Description[0]);
	@_Temperature_F = split(/\&/, $_Temperature_F[1]);
	my $Temperature_F = $_Temperature_F[0];

	my @_Humidity_PERCENT = split(/ /, $Description[1]);
	@_Humidity_PERCENT = split(/\%/, $_Humidity_PERCENT[1]);
	my $Humidity_PERCENT = $_Humidity_PERCENT[0];

	my @_Pressure_INCH_HG = split(/ /, $Description[2]);
	@_Pressure_INCH_HG = split(/in/, $_Pressure_INCH_HG[1]);
	my $Pressure_INCH_HG = $_Pressure_INCH_HG[0];

	my $Conditions = substr($Description[3], 12, length($Description[3])-12);

	my $WindDirection = substr($Description[4], 16, length($Description[4])-16);

	my @_WindSpeed_MPH = split(/mph/, $Description[5]);
	my $WindSpeed_MPH = substr($_WindSpeed_MPH[0], 12, length($_WindSpeed_MPH[0])-12);

	my $pubDate = $RSS->query('//pubDate')->text_content;

	my $insert_query = $DB_link->do(qq{
		INSERT INTO `$DB_table`
			(`_Created`, `ZipCode`, `Latitude`, `Longitude`, `Temperature_F`, `Humidity_PERCENT`, 
			`Pressure_INCH_HG`, `Conditions`, `WindDirection`, `WindSpeed_MPH`, `pubDate`)
		VALUES (NOW(), "$ZipCode", "$Latitude", "$Longitude", "$Temperature_F", "$Humidity_PERCENT", 
			"$Pressure_INCH_HG", "$Conditions", "$WindDirection", "$WindSpeed_MPH", 
			STR_TO_DATE("$pubDate", "%a, %e %b %Y %T"));
	})
		or die "Couldn't run insert_query: ".DBI->errstr;

	print "Completed for zip code: $ZipCode\n";
	print "[sleeping for 2 seconds]\n\n";
	sleep(2);

}

$DB_link->disconnect();
