<?php include "../../config.php"; ?>
<?php include "../../assets/var.php"; ?>
<?php include "../../assets/function.php"; ?>

<?php $phone_code = [ '+6221', '+6222', '+6223', '+6224', '+6225', '+6226', '+6227', '+6228', '+6229', '+6230', '+6231', '+6232', '+6233', '+6234', '+6235', '+6236', '+6237', '+6238', '+6239', '+6240', '+6241', '+6242', '+6243', '+6244', '+6245', '+6246', '+6247', '+6248', '+6249', '+6250', '+6251', '+6252', '+6253', '+6254', '+6255', '+6256', '+6257', '+6258', '+6259', '+6260', '+6261', '+6262', '+6263', '+6264', '+6265', '+6266', '+6267', '+6268', '+6269', '+6270', '+6271', '+6272', '+6273', '+6274', '+6275', '+6276', '+6277', '+6278', '+6279', '+6290', '+6291', '+6292', '+6293', '+6294', '+6295', '+6296', '+6297', '+6298', '+6299' ]; ?>

<?php
$sql  = $pdo->query("SELECT * FROM tb_keyword_scrapping_maps");
$data = $sql->fetch(PDO::FETCH_ASSOC);

$keyword = $data['keyword'];
$ll      = $data['ll'];
$api_key = $data['api_key'];

$sql_    = $pdo->query("SELECT * FROM tb_listing_maps where keyword = '".$keyword."'");

$start = $sql_->rowCount()+1;

$field = json_encode([
  	"country" => "ID",
  	"domain"  => "com",
  	"keyword" => $keyword,
  	"ll"      => $ll,
  	"start"   => $start
  	]);
?>

<?php
$curl = curl_init();
  
curl_setopt_array($curl, array(
	//CURLOPT_URL => 'https://api.scrape-it.cloud/scrape/google/locals',
	CURLOPT_URL => "http://api.scrape.do?token=cf33f3b5e5bc463097e5c698907ac0d2463e9635782&url=https://httpbin.org/ip?json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $field,
    CURLOPT_HTTPHEADER => array(
        'x-api-key: ' . $api_key . '',
        'Content-Type: application/json'
    ),
));
  
$response = curl_exec($curl);
curl_close($curl);

$data                = json_decode($response, true);
$scrapingResultCount = count($data['scrapingResult']['locals']);
    
if($scrapingResultCount == 0 OR $data['status'] == 'Sorry, you don\'t have enough credits to perform this request')
{
    session_start();
    unset($_SESSION['extract']);
    $extract = "Selesai";
}
else
{
    if($data['status'] == 'ok')
    {
        $extract = "Sedang berproses ..";
    }
}
    
echo "Status: " . $data['status'] . "<br>";
echo "Pesan: " . $data['message'] . "<br>";
echo "Ektrak: " . $extract . "";
	
for ( $z = 0; $z < $scrapingResultCount; $z++ ) 
{
    
	$phone_old  = $data['scrapingResult']['locals'][$z]['phone'];
    $phone      = str_replace(str_split('\\/:*?"<>|- '), '', $phone_old);
	    
	if($phone_old != null && !in_array(substr($phone,0,5),$phone_code))
	{
	    $sql_exist  = $pdo->query("SELECT * FROM tb_listing_maps where phone_old = '".$phone_old."'");
        
        if($sql_exist->rowCount() == 0 )
        {

	    $pdo->query ("insert into tb_listing_maps
        (
        title,
        phone,
        phone_old,
        keyword,
        ll,
        posted
        )
        values 
            (
            '".$data['scrapingResult']['locals'][$z]['title']."',
            '".$phone."',
            '".$phone_old."',
            '".$keyword."',
            '".$ll."',
            '".date('Y-m-d H:i:s')."'
            )
             ");
             
        }
             
	}
             
}
    
$pdo->query("UPDATE tb_keyword_scrapping_maps SET 
        scraping_result_count   ='".$scrapingResultCount."'
        ")or die(print_r($pdo->errorInfo(), true));
        
?>