<html>
	<head>
		<title>Playwire Video API Test - Video Upload</title>
	</head>
<body>
<?php
	require_once('../testcommon.php');
	if (empty($api_token)) {
		showAPITokenForm();
	} else {
		require_once('../class.playwire.php');

		$playwire = new Playwire($api_token);


require "sc-connect.php";
rconnect(supercheats);

$query = "SELECT * FROM videos WHERE playwire='0' and status='ACTIVE' and filename!='' ORDER BY id DESC LIMIT 1";
$stuff = mysql_query($query) or die("$query<BR>error:". mysql_error());
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)) {
                if ($row["min_age"]>="18") { $agegate="1"; } else { $agegate="0"; }
                        $source_url="http://video.supercheats.com/videos/".vlchyph($row["gamename"])."/".$row["folder"]."/".$row["filename"];
                        $tag_list=$row["gamename"].", ".preg_replace("/:/",", ",trim($row["type"],":"));
                        $playwire = new Playwire($api_token);

                        // This is a POST, call the API

		if ($row["id"]) {
			echo "<pre>";
			// 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;
				$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();
			}
			echo "</pre>";
		}
		else {
			echo "<pre>";
			$defaults = $playwire->getVideoDefaults();
			$categories = $playwire->getVideoCategories();
			echo "</pre>";
		}
	}
}
?>
</body>
</html>
<?php

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