<?php
require "sc-connect.php";
require "connect.php";
$api_token="U6oGnfqmu25PwH7k";
$_SESSION["playwire_api_token"] = $api_token;
require_once('../testcommon.php');
require_once('../class.playwire.php');
rconnect(supercheats);


$query = "SELECT * FROM videos WHERE playwire>'1' and status='ACTIVE' and filename!='' and autoplay='0' and type LIKE '%Trailers%' ORDER BY clicks DESC LIMIT 1";
$result = mysql_query($query) or die("$query<BR>error:". mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
	$videos[]=$row;
}
foreach ($videos as $row) {
#	$row=$videos[$vid];
echo $row["id"]."\n";
#$platforms=array();
#$query = "SELECT platform FROM products WHERE name='".addslashes($row["gamename"])."'";
#$result = mysql_query($query) or die("$query<BR>error:". mysql_error());
#while($prow = mysql_fetch_array($result, MYSQL_ASSOC)){
#        $platforms[]=$prow["platform"];
#}
		#if ($row["min_age"]>="18") { $agegate="1"; } else { $agegate="0"; }
		#if ($row["filename"]) {
			$source_url="http://video.supercheats.com/videos/".vlchyph($row["gamename"])."/".$row["folder"]."/".$row["filename"];
			$tag_list=$row["gamename"].", ".preg_replace("/:/",", ",trim($row["type"],":"));
			if (count($platforms)) { 
				$tag_list.=", ".implode(", ",$platforms);
			}
			$playwireid=$row["playwire"];
echo $tag_list."\n";
			$playwire = new Playwire($api_token);

			// This is a POST, call the API
			$video = new Video();
			$video->id = "$playwireid";
			$video->auto_start = "1";
			$video->public = "1";
			try {
				$new_video = $playwire->uploadVideo($video);
				echo "Successfully updated video, new id is " . $new_video->id."|".$new_video->auto_start."\n";
rconnect(supercheats);
				#$query="UPDATE videos SET autoplay='1' WHERE id='".$row["id"]."'";
				#$result = mysql_query($query) or die("$query<BR>error:". mysql_error());

			} catch (PlaywireException $exception) {
				echo "Had an exception: " . $exception->getMessage()."\n";
			}
		#}
sleep(1);
	}


function vlchyph($text) {
        $text=strtolower(preg_replace("/\W/i"," ",$text));
        $text=preg_replace("/ +/i","-",$text);
        $text=trim($text,"-");
        return($text);
}

?>
