use DBI;
use strict;
use LWP::Simple;
use CGI;

my $master_dsn = 'DBI:mysql:connect:172.29.44.253';
my $master_db_user = 'irconnectdb';
my $master_db_paswd = '13c077ect~131';

#my $master_dsn = 'DBI:mysql:moneycontrol:172.30.20.26';
#my $master_db_user = 'moneycontrol';
#my $master_db_paswd = 'admin~414';

my $master_dbh = DBI->connect ($master_dsn, $master_db_user, $master_db_paswd, {mysql_auto_reconnect => 1});

my $index = 0;
my $index1 = 1000;

my $master_sth = $master_dbh->prepare ("SELECT count(*) from connect_master where DATE_SUB(CURDATE(),INTERVAL 30 MINUTE) <= upd_date");
#my $master_sth = $master_dbh->prepare ("SELECT count(*) from connect_master");
$master_sth->execute ();
my $cnt = 0;
while (my ($count) = $master_sth->fetchrow_array ())
{
 	$cnt =  $count; 	
}
print $cnt , " Records to be updated \n";
#exit;
my $i = 1;
#while($index <= $cnt)
#{

	#print $index , "," , $cnt , "\n";
	#my $master_sth = $master_dbh->prepare ("SELECT entity_id,entityname,vote_up,vote_down,sub_menu_cat,category from biodata where DATE_SUB(CURDATE(),INTERVAL 30 MINUTE) <= upd_date");
	#my $master_sth = $master_dbh->prepare ("SELECT pr_id,profile_name,vote_up,vote_down,cat_id,img_2 from connect_master order by upd_date desc limit ". $index . "," . $index1);
	
	my $master_sth = $master_dbh->prepare ("SELECT pr_id,profile_name,vote_up,vote_down,cat_id,img_2 from connect_master where DATE_SUB(CURDATE(),INTERVAL 30 MINUTE) <= upd_date");

	#print "SELECT entity_id,entityname,vote_up,vote_down,sub_menu_cat,category from biodata where DATE_SUB(CURDATE(),INTERVAL 30 MINUTE) <= upd_date";
	$master_sth->execute ();

	my $alias = '';
	while (my ($id, $entity, $voteup, $votedown, $category, $thumbnail) = $master_sth->fetchrow_array ())
	{
        	#print $entity . " " . $category ."\n";
		print $i , "\n";
		$i = $i + 1;	
	
		$entity = $master_dbh->quote($entity);
		my $master_sth1 = $master_dbh->prepare ("SELECT alias from wiki_full_list where entityname=$entity");
		#print "SELECT alias from wiki_full_list where entityname=$entity";
		
		#my $alias = '';
		$master_sth1->execute ();
		$alias = $master_sth1->fetchrow_array ();
		#{
			#print $alias, "\n";
		#}


		my $query_url = "http://api.in.com/active18/solr/solr_add_spell_entity_new.php?id=" . $id . "&entity=" . CGI::escape($entity) . "&voteup=" . CGI::escape($voteup) . "&votedown=" . CGI::escape($votedown). "&alias=" .  CGI::escape($alias) ."&category=" . CGI::escape($category) . "&thumbnail=" . CGI::escape($thumbnail);	
		#print $query_url;
		my $con = get $query_url;
        	#print  "id:" . $con . "\n";
		#exit;
	}
#	$index = $index + 1000;
	#$index1 = $index + 500;
#}

$master_sth->finish;

