<?php
$ctime= time();

$yurl=$_POST['yurl'];
$period = $_POST['period'];
if(empty($period)){$period=6;}

if(!empty($_POST['yurl'])){
//$yurl="https://www.youtube.com/c/MinewShewaTube/videos"	;
$abok= explode('/',$yurl);
 $ab= $abok[4];
	
//	echo $ab;
//	exit;
	
$file = './b.txt';
if (file_exists($file) && isset($ab)) {
$myfile = fopen($file, "r") or die("Unable to open file!");
$readfile =  fread($myfile,filesize($file));
fclose($myfile);
}
$replaceok = explode('#'.$ab.'#',$readfile);	
$read = explode('|',$replaceok[1]);	// header
// header 


//if($ctime>=$read[1]  || $period!=$read[0]){
	

$ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$yurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
$buffer = curl_exec($ch);
	curl_close($ch);
//print_r($buffer);
//echo 'BBB';

//$product = explode('"watchEndpoint":{"videoId":"',$buffer);
//$productok = explode('"',$product[1]);
//$titlea = explode('"title":{"runs":[{"text":"\"',$buffer);
//$titleb = explode('"}],"a',$titlea[1]);

//echo $productok[0];
//$titleyes= str_replace( array( '\\', '"', ',' , ';', '<', '>','/' ), ' ',$titleb[0]);
//$titleyesok= substr($titleyes,0,35);
//echo $titleyesok;
//exit;


for ($x = 1; $x <= 15; $x++) {
$product = explode('"watchEndpoint":{"videoId":"',$buffer);
$productok = explode('"',$product[$x]);
$titlea = explode('"title":{"runs":[{"text":"',$buffer);
$titleb = explode('"}],"a',$titlea[$x]);
$titleyesok= str_replace( array( '\\', '"', ',' , ';', '<', '>','/' ), ' ',$titleb[0]);
//$titleyesok= substr($titleyes,0,35);

$pcount= strlen($productok[0]);
	if($pcount==11){
  $ads.= "'".$productok[0]."':".$titleyesok.",";
	$pp.=$productok[0];
	}
} 
	
	

if(!empty($pp) && !empty($replaceok[1]) && !empty($ab)){

if(empty($period)){$period=$read[0];}
	
	
if($ctime>=$read[1]){$ctimeok = $ctime +($period*3600);}else{$ctimeok=$read[1];}	
$adsok= $period.'|'.$ctimeok.'|'.$ads;	
	
//echo $adsok;
	//exit;
$allsave= str_replace($replaceok[1], $adsok, $readfile);	

$myfile = fopen("b.txt", "w") or die("Unable to open file!");
fwrite($myfile, $allsave);
fclose($myfile);
$nday = round((time()/300),0);	
copy('b.txt', './bk/'.$nday.'b.txt');

$msg= 'Update ok '.$ab.'<br>';	
}

elseif(!empty($pp) && !empty($ab)){	

//if(empty($period)){$period=6;}		
$ctimeok = 	$ctime +($period*3600);
$periodtime= $period.'|'.$ctimeok.'|';	
	
$myfile = fopen("b.txt", "a") or die("Unable to open file!");
fwrite($myfile, '#'.$ab.'#'.$periodtime.$ads.'#'.$ab.'#
');
fclose($myfile);
$nday = round((time()/300),0);	
copy('b.txt', './bk/'.$nday.'b.txt');

$msg= 'New ok '.$ab.'<br>';
	
}


}else{
$msg= 'No need Update '.$ab.'<br>';
}
//$resp = json_decode($buffer, true);
 // echo $response[0]["country_code"];
//print_r($response);

//}
?>

<form id="form1" name="form1" method="post">
  <p style="text-align: center"> 
	  <?=$msg;?>
    <label for="yurl">Youtube Channel:</label>
    <input name="yurl" type="text" id="yurl" size="50" list="rr">
<datalist id="rr">
<option>https://www.youtube.com/c/ebstvWorldwide/videos</option>
<option>https://www.youtube.com/c/fanabroadcastingcorporate/videos</option>
<option>https://www.youtube.com/c/KanaTelevision/videos</option>
<option>https://www.youtube.com/c/EBCworld/videos</option>
<option>https://www.youtube.com/c/ArtsTvWorld/videos</option>
<option>https://www.youtube.com/c/YeEthiopiaLijochTV/videos</option>
<option>https://www.youtube.com/c/UCvr6jA3WYOhXFUD2LKpqhQw/videos</option>
<option>https://www.youtube.com/c/ESATtvEthiopia/videos</option>
<option>https://www.youtube.com/c/NetsebraqProductions/videos</option>

</datalist>	  
    <br><br>
    <label for="period">Check Every:</label>
    <select name="period" id="period">
      <option value="">Select</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>8</option>
      <option>10</option>
      <option>12</option>
      <option>14</option>
      <option>16</option>
      <option value="24">1 days</option>
      <option>36</option>
      <option value="48">2 days</option>
      <option value="72">3 days</option>
      <option value="120">5 days</option>
      <option value="168">1 week </option>
      <option value="336">2 week</option>
      <option value="720">1 month</option>
    </select>
<br><br>
    <input type="submit" name="submit" id="submit" value="Submit">
  </p>
</form>



