<?php

//include "googlecrawler.php";
$timer1 = 50;

$con = mysql_connect("localhost","root","sofieke001");
 
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("Seo", $con);



//$query = "SELECT * FROM `Keywords` WHERE  `State` = 1 ";
$query = "SELECT ads.Image_file, ads.url FROM ads WHERE (((ads.Published)=1)) GROUP BY ads.Image_file ";

$comments = mysql_query($query);
$valxyz = 1;

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
  $SearchKey = $row['Image_file'];
  $Url = $row['url'];
  $x= fileexist($SearchKey);
  if ($x == ("not exist")) {
  // Nu gaan we Google Pagespeed uitvoeren want ons bestand bestaat nog niet
  echo " the file does not exist so we gonna create it $SearchKey <br>";
  get_thumbnail($Url,$SearchKey);
  $valxyz = $valxyz +1;
  if ($valxyz > 10) {
  	return ;
  }

  }else {
  	echo "the file exist do nothing $SearchKey<br>";
  }



$timer1 = rand(60, 120);
sleep($timer1);

}

mysql_close($con);



function fileexist ($file_to_check){
//	$file_to_check = "googleup/".$file_to_check;
	if (file_exists($file_to_check)) {
   		// echo "The file $file_to_check exists";
    	return "exist";
	} else {
  		//  echo "The file $file_to_check does not exist";
    	return "not exist";
	}
}

function get_thumbnail ($site_url, $site_filename){
	if ( !$image = apc_fetch( "thumbnail:".$site_url ) ) 
	{ 
		$image = file_get_contents("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=$site_url&screenshot=true");
		$image = json_decode($image, true); 
		
		//echo "<pre>"; print_r($image); die; 
		
		$image = $image['screenshot']['data'];
		apc_add("thumbnail:".$site_url, $image, 2400); 
	}

	
	$image = str_replace(array('_','-'),array('/','+'),$image); 
	
	echo "<img src=\"data:image/jpeg;base64,".$image."\" border='1' />";



$image = base64_decode($image);
echo "<br>".$site_filename."<br>" ;
file_put_contents($site_filename, $image);	
}
?>