<?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 status='ACTIVE' and filename!=''";
        $stuff = mysql_query($query) or die("$query<BR>error:". mysql_error());
        while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)) {
                $videos[$row["id"]]=$row;
        }

        echo count($videos)."\n";
connect(supercheats_videos);
        $query = "SELECT videoid,cutoffs FROM video_stats WHERE cutoffs>'0' and day>='20111001'";
        $stuff = mysql_query($query) or die("$query<BR>error:". mysql_error());
        while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)) {
                if ($videos[$row["videoid"]]) {
                $plays[$row["videoid"]]=$plays[$row["videoid"]]+$row["cutoffs"];
                }
        }
        echo count($plays)."\n";

arsort($plays);
$plays=array_slice($plays,0,2000,TRUE);
foreach ($plays as $vid=>$c) {
#        echo "$c -> $vid - ".$videos[$vid]["playwire"]."\n";
        if ($videos[$vid]["playwire"]<=1) {
                $uploads[]=$vid;
        }
}
echo count($uploads);
$uploads=array_slice($uploads,0,50);
echo count($uploads);

rconnect(supercheats);
#$query = "SELECT * FROM videos WHERE playwire='0' and status='ACTIVE' and filename!='' ORDER BY id DESC LIMIT 100";
#$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) {
foreach ($uploads as $vid) {
	$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);
			}
echo $tag_list."\n";
			$playwire = new Playwire($api_token);

			// This is a POST, call the API
			$video = new Video();
			$video->name = $row["gamename"]." ".$row["title"];
			$video->source_url = $source_url;
			
			$video->description = $row["description"];
			$video->category_id = "16";
			$video->width = "640";
			$video->height = "480";
			$video->tag_list = $tag_list;
			$video->show_video_watermark = "0";
			$video->use_age_gate = $agegate;
			$video->auto_start = "0";
			$video->public = "1";
			try {
				$new_video = $playwire->uploadVideo($video);
				echo "Successfully uploaded video, new id is " . $new_video->id."\n";
rconnect(supercheats);
				$query="UPDATE videos SET playwire='".$new_video->id."' 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);
}

?>
