#!/usr/bin/perl

################################
#   Forex Factory Spidering    #
#   Dated: 07 July, 2010       #
# Last Updated: 8 July , 2010  #
#    Current version: 1.0      #
#  Copyright Sangranet Tech.   #
################################

### Required and used Modules ###
use CGI qw/ :standard :html3/;
use URI::Escape;
use HTML::Entities;
use HTTP::Request;
use LWP::UserAgent;
use DBI;

##### Start of Editable Variables ###
my ($dsn) = "DBI:mysql"; # DBI Module for interacting Perl with MySQL
my ($dbname) = "forex_calender"; # Name of database in which hits table will be stored
my ($host) = "localhost"; # Host name of MySQL Server
my ($user_name) = "root"; # Username for connecting to MySQL Server
my ($pass) = "Ja7371223933"; #Password for connecting to MySQL Server
$timezone = "3";
 
##### End of Editable Variables ###
$| = 1;

################# Start of Main Program ####################

### Initialize database ###
my ($dbh,$sth);
my (@c_array,$rows);
my (%attr) =
(
	PrintError => 1,
	RaiseError => 1
);
$dbh = DBI->connect("$dsn:$dbname:$host", $user_name, $pass, \%attr) or die("Can't connect to database");

$cgi = new CGI;
$id = $cgi->param("id");
$day = $cgi->param("day");
print $cgi->header;

if ($id ne "")
{
	$extra_args = "WHERE cl_id='$id'";
}
elsif ($day ne "")
{
	if ($day=~/today/i) {
		$extra_args = "WHERE DATE(DATE_ADD(show_time,INTERVAL $timezone HOUR)) = DATE(DATE_ADD(now(),INTERVAL 3 HOUR))";
	} elsif ($day=~/yesterday/i) {
		$extra_args = "WHERE DATE(DATE_ADD(show_time,INTERVAL $timezone HOUR)) = DATE(DATE_SUB(now(),INTERVAL 21 HOUR))";
	} elsif ($day=~/tomorrow/i) {
		$extra_args = "WHERE DATE(DATE_ADD(show_time,INTERVAL $timezone HOUR)) = DATE(DATE_ADD(now(),INTERVAL 27 HOUR))";
	}

}

print <<HEAD;
<html><head><title>Forex Calender</title></head>
<body>
<table width="100%" border="0">
<tr bgcolor="#C0C0C0">
	<td>Date</td>
	<td>Time(EDT)</td>
	<td>Currency</td>
	<td>Impact</td>
	<td>Details</td>
	<td>Actual</td>
	<td>Forecast</td>
	<td>Previous</td>
</tr>
HEAD

$sth = $dbh->prepare("SELECT cl_date,cl_time,cl_currency,cl_impact,cl_name,cl_id,cl_actual,cl_forecast,cl_previous,cl_details FROM forex_calender $extra_args ORDER BY show_time ASC");
$sth->execute();
while (@c_array = $sth->fetchrow_array)
{
	($cl_date,$cl_time,$cl_currency,$cl_impact,$cl_name,$cl_id,$cl_actual,$cl_forecast,$cl_previous,$cl_details) = @c_array;
	
	if ($cl_impact=~/low/i) { ## Low Impact
		$impact_image = "images/implow.gif";
	} elsif ($cl_impact=~/medium/i) { ## Low Impact
		$impact_image = "images/impmedium.gif";
	} elsif ($cl_impact=~/high/i) { ## Low Impact
		$impact_image = "images/imphigh.gif";
	} else {
		$impact_image = "images/impno.gif";
	}
	print "<tr>
			<td>$cl_date</td>
			<td>$cl_time</td>
			<td>$cl_currency</td>
			<td><img src=\"$impact_image\" alt=\"$cl_impact\" title=\"$cl_impact\" border=\"0\" /></td>
			<td><a href=\"#\" onclick=\"if(document.getElementById('$cl_id').style.display == 'none') {document.getElementById('$cl_id').style.display='';} else {document.getElementById('$cl_id').style.display='none';} return false;\">$cl_name</a></td>
			<td>$cl_actual</td>
			<td>$cl_forecast</td>
			<td>$cl_previous</td>
		   </tr>
		   <tr><td colspan=\"8\"><div id=\"$cl_id\" style=\"display:none;background-color:#CCCCCC;\">$cl_details</div></td></tr>
		  ";
}
print <<FOOTER;
</table>
</body></html>
FOOTER


		
			
