<?php
require '../fly2/fly3.php';
require '../config.php';
require '../youtube.php';

class VideoUploader extends YoutubeV3{
	function execute(){
		$tracks = query('select playurl, no from track where snippet = "null"');
		for($i=0;$i<count($tracks);$i++){
			parse_str(parse_url($tracks[$i][playurl])[query], $params);
			if($params[v]){
				$a = $this->getVideo($params[v]);
				if($a[result] == 'success'){
					$a[no] = $tracks[$i][no];
					$a[snippet]= json_encode($a[snippet], JSON_UNESCAPED_UNICODE);
					$a[contentDetails]= json_encode($a[contentDetails], JSON_UNESCAPED_UNICODE);
					$a[statistic]= json_encode($a[statistic], JSON_UNESCAPED_UNICODE);
					// 제목, 설명, 트랙의 썸네일, 상세설명, 동영상분석정보 DB에 넣기 
					$result = query("
						update track set
							title=:title,
							snippet=:snippet,
							contentDetails=:contentDetails, 
							statistic=:statistic
						where no=:no", $a);	
					if($result[numAffected]==1){
						echo $a[title]."\n";
						echo '-> update done'."\n";
					}else{
						echo $a[title]."\n";
						echo '-> The current data is the latest '."\n";
					}
				}else{
					print_r($a[result]);
				}
			}
		}
	}
}
$app = new VideoUploader(); 
$app -> execute();
	
?>