use strict;
use LWP::Simple;
use CGI;
use DBI;
my $master_dsn = 'DBI:mysql:Distributed_ClusterDB:localhost';
#my $master_dsn = 'DBI:mysql:Distributed_ClusterDB:172.18.16.35';
my $master_db_user = 'root';
my $master_db_paswd = 'admin~414';
sub trim($);

open (PROCESSCHECK, ("ps -ef | grep \"/usr/bin/perl /home/web18/entity/entityGame_watch.pl\" | wc -l |"));
my @lines = <PROCESSCHECK>;
chomp (@lines);
foreach my $temp (@lines)
{
        print $temp,"\n";
        if ($temp > 3)
        {
                print "There is a watch entity process already running\n";
                exit;
        }
}
close(PROCESSCHECK);


my $master_dbh = DBI->connect ($master_dsn, $master_db_user, $master_db_paswd);

my $master_sth = $master_dbh->prepare ("SELECT DATE_FORMAT(timestamp - INTERVAL 2 HOUR, '%Y%m%d%H%i%S') as timestamp from entity_fetch_time_watch");

$master_sth->execute ();

my $time = $master_sth->fetchrow_array();

my $master_sth_update = $master_dbh->prepare ("UPDATE entity_fetch_time_watch set timestamp=now()");

$master_sth_update->execute();
$master_sth_update->finish;

print $time , "\n";

#my $time = "20090323000000";
my $dir="/var/www/cron/mc/172_30_11_23";

#open (FILENAME, "/home/web18/entity/entities_watch.txt") or die "could not open file";
open (FILENAME, "$dir/entities_watch.txt") or die "could not open file";
my $entList = '';



#$time = "20110922000000";


my $search_url = "http://api.in.com/active18/solr/solr_search_watch.php?query=crawl_time:[$time+TO+*]&sortfield=crawl_time&sortorder=1";
#my $search_url = "http://api.in.com/active18/solr/solr_search_watch.php?query=id:95256824239a27253012d698d74ea6de5478da19&sortfield=crawl_time&sortorder=1";
print $search_url;
my $con = get $search_url;
my $count = 0;
if ($con =~ m/<length>(.*?)<\/length>/i)
{
        $count = $1;
}
print $count, "\n";

my $index = 0;
my $limit = 200;
my $i = 0;
my $temp = 0;

foreach my $temp (<FILENAME>)
{
	chomp($temp);
        $entList = $temp . "|" . $entList;
}

#chomp($entList);
#print $entList;
#exit;
my @values = split(/\|/, $entList);

#my $compareText = "srk ,ak,Saurabh king khan, Shah Rukh Khan";


do
{
    #my $query_url = "http://api.in.com/active18/solr/solr_search_read.php?query=*:*+AND+crawl_time:[20090310000000+TO+*]&sortfield=crawl_time&sortorder=0&start=" . $index . "&limit=" . $limit;
	 my $query_url = "http://api.in.com/active18/solr/solr_search_watch.php?query=crawl_time:[$time+TO+*]&sortfield=crawl_time&sortorder=1&start=" . $index . "&limit=" . $limit;

	#my $query_url = "http://api.in.com/active18/solr/solr_search_watch.php?query=id:95256824239a27253012d698d74ea6de5478da19&sortfield=crawl_time&sortorder=0&start=" . $index . "&limit=" . $limit;
        $con = get $query_url;
	#print $con;	
	#while ($con =~ m/<item>(.*?)<\/item>/s)
	
	while ($con =~ s/<item>(.*?)<\/item>//ism)
	{

		print "Count:" . $i . "\n";
		#print $1;
		#exit;
		$i++;
                my $text = $1;
                my $id = '';
                my $url = '';
                my $crawl_time = '';
                my $source = '';
                my $title = '';	
		my $entTitle = '';
		my $entTags = '';
		my $tags = '';
				
                
		if ($text =~ m/<id><!\[CDATA\[(.*?)\]\]><\/id>/)
                {
                        $id = $1;
                }
                if ($text =~ m/<url><!\[CDATA\[(.*?)\]\]><\/url>/)
                {
                        $url = CGI::escape($1);
                }
                if ($text =~ m/<crawl_time><!\[CDATA\[(.*?)\]\]><\/crawl_time>/)
                {
                        $crawl_time = CGI::escape($1);
                }
                if ($text =~ m/<source><!\[CDATA\[(.*?)\]\]><\/source>/)
                {
                        $source = CGI::escape($1);
                }
                if ($text =~ m/<title><!\[CDATA\[(.*?)\]\]><\/title>/)
                {
                        $title = CGI::escape($1);
						$entTitle = $1;
                }                                
		if ($text =~ m/<tags><!\[CDATA\[(.*?)\]\]><\/tags>/)
                {
                        $tags = CGI::escape($1);
						$entTags = $1;
                }
				#print $id."\n";
				#exit;
				#my $compareText = $entTitle . " " . $entTags;
				my $compareText = $entTitle;
				#print $compareText;
				my $finalEntity = '';
				my $final_pr_id = '';
				foreach my $v (@values)
				{
				        my @aValues = split(/,/, $v);
					
					#print @values;
					#print "\n\n";
					chomp(@aValues);
					my $i = 0;
					my $mainName = '';
					my %temp = ();
					my $flag = 1;
					my $size = @aValues;
					#print $size;
					#exit;
					#print @aValues[0] . ":" . @aValues[1];
					#exit;
					
					my $a = trim(@aValues[0]);
					my $pr_id = trim(@aValues[1]);
					next if ($a eq "");	
					$a =~ s/\(/\\(/;
				        $a =~ s/\)/\\)/;
				        if($flag && $compareText =~m/\b$a\b/is)
				        {
							 $flag = 0;
		            				  print "\n\nFound Match:" , $a , " Alias:" . $a . ":" . $pr_id . " \n";				
							 $finalEntity = $a . "," . $finalEntity;	 		  		                	     		$final_pr_id = $pr_id . "," . $final_pr_id;
					}
				}
				chop($finalEntity);
				chop($final_pr_id);				

				if($finalEntity ne '')
				{
#-------------------------------------------- For Title ------------------------------------------------------------------------------					
				
					

					$finalEntity = CGI::escape($finalEntity);
					print $finalEntity . "\n";
					print $final_pr_id . "\n";
					
					
					my $query_url = "http://api.in.com/active18/solr/solr_add_watch_entity_81.php?url=$url&crawl_time=$crawl_time&source=$source&title=$title&entity=$finalEntity&pr_id=$final_pr_id";
										
					print $query_url;
					print "\n".$id . ":";
					my $con = get $query_url;
					#print $con . "\n";	
					print "\nFinalEntity " . $finalEntity;		
					#exit;
				}else{
# ---------------------------------------------------------- For Tags -------------------------------------------------------------------
					
					 my $compareText = $entTags;
                                	#print $compareText;
                                	my $finalEntity = '';
                                	my $final_pr_id = '';
                                	foreach my $v (@values)
                                	{
                                        	my @aValues = split(/,/, $v);

                                        	#print @values;
                                        	#print "\n\n";
                                        	chomp(@aValues);
                                        	my $i = 0;
                                        	my $mainName = '';
                                        	my %temp = ();
                                        	my $flag = 1;
                                        	my $size = @aValues;
                                        	#print $size;
                                        	#exit;
                                        	#print @aValues[0] . ":" . @aValues[1];
                                        	#exit;

                                        	my $a = trim(@aValues[0]);
                                        	my $pr_id = trim(@aValues[1]);
                                        	next if ($a eq "");
                                        	$a =~ s/\(/\\(/;
                                        	$a =~ s/\)/\\)/;
                                        	if($flag && $compareText =~m/\b$a\b/is)
                                        	{
                                                         $flag = 0;
                                                          print "\n\nFound Match:" , $a , " Alias:" . $a . ":" . $pr_id . " \n";
                                                         $finalEntity = $a . "," . $finalEntity;                                                                                
							 $final_pr_id = $pr_id . "," . $final_pr_id;
                                        	}
                                	}
                                	chop($finalEntity);
                                	chop($final_pr_id);

					$finalEntity = CGI::escape($finalEntity);
                                        print $finalEntity . "\n";
                                        print $final_pr_id . "\n";


                                        my $query_url = "http://api.in.com/active18/solr/solr_add_watch_entity_81.php?url=$url&crawl_time=$crawl_time&source=$source&title=$title&entity=$finalEntity&pr_id=$final_pr_id";

                                        print $query_url;
                                        print "\n".$id . ":";
                                        my $con = get $query_url;
                                        #print $con . "\n";
                                        print "\nFinalEntity " . $finalEntity;
				}
				
	}
		$index = $index + $limit;
#        $i++;
}while ($index <= $count);	

sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}

