#!/usr/bin/perl
#######################
# By: Ventz Petkov    #
# Date: 6-14-12       #
# Scrape Wunderground #
#######################

use warnings;
use strict;

use URI::file;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();

# Note - cron the file so that they don't ban us!
# 		Cron every hour for example, something like this:
#		#!/bin/sh
#		wget "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=02123" -O /tmp/weather.html
#		chmod 755 /tmp/weather.html

my $file = URI->new("file:/tmp/weather.html");
#my $url = 'http://www.wunderground.com/cgi-bin/findweather/getForecast?query=02123';

$mech->agent_alias( 'Mac Safari' );
$mech->get( $file );
my $page = $mech->content;

my ($condition, $temp, $feel, $wind, $wind_speed) = '';

if($page =~ /.*<div id="curCond">(.*)<\/div>.*/) {
	$condition = $1;
}
if($page =~ /.*<div id="tempActual"><span id="rapidtemp" class="pwsrt" pwsid="KMACAMBR14" pwsunit="english" pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="(\d?\d?\d?\.?\d?\d?)">.*/) {
	$temp = $1;
}
if($page =~ /.*<div id="tempFeel">Feels Like(\s*)<span class="nobr"><span class="b">(\d?\d?\d?\.?\d?\d?)<\/span>.*/s) {
	$feel = $2;
}
if($page =~ /.*<span id="windCompassSpeed" class="pwsrt" pwsid="KMACAMBR14" pwsunit="english" pwsvariable="windspeedmph" english="" metric="">(\d?\d?\.?\d?\d?)<\/span>.*/) {
	$wind = $1;
}
if($page =~ /.*<div class="dataCol1">Speed \/ Dir<\/div>(\s*)<div class="dataCol4">(\s*)(.*?)(\s*)<\/div>.*/s) {
	$wind_speed = "$3"; chomp($wind_speed);
}
print "\n$condition\n";
print "\tTemp: $temp | Feels Like: $feel | Wind: $wind ($wind_speed)\n\n";
