Created
March 17, 2010 22:17
-
-
Save raytiley/335798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('America/New_York'); | |
//Setup | |
$channelID = 2; | |
$startInDays = 4; | |
$scheduleForDays = 7; | |
$monthsToLookBack = 3; | |
require_once('nusoap.php'); | |
//End Setup | |
//Variables | |
$programming = array(); | |
$schedule = array(); | |
//End Varaibles | |
$client = new nusoap_client("http://192.168.0.136/CablecastWS/CablecastWS.asmx?WSDL", 'wsdl'); | |
class ScheduleEvent | |
{ | |
// property declaration | |
var $start, $end; | |
function TestObj($name) | |
{ | |
$this->start = $start; | |
} | |
static function cmp_obj($a, $b) | |
{ | |
$a1 = $a->start; | |
$b1 = $b->start; | |
if ($a1 == $b1) | |
{ | |
return 0; | |
} | |
return ($a1 > $b1) ? +1 : -1; | |
} | |
} | |
function isSomethingScheduled($time, $schedule) | |
{ | |
$return = false; | |
foreach($schedule as $s) | |
{ | |
if ($time == $s->start) | |
{ | |
$return = true; | |
} | |
if($time > $s->start && $time < $s->end) | |
{ | |
$return = true; | |
} | |
} | |
return $return; | |
} | |
function maxProgramLength($time, $schedule, $endDay) | |
{ | |
usort($schedule, array("ScheduleEvent", "cmp_obj")); | |
$endTime = NULL; | |
foreach($schedule as $s) | |
{ | |
if($s->start > $time && $s->start < $endDay) | |
{ | |
$endTime = $s->start; | |
break; | |
} | |
if ($endTime == NULL) | |
{ | |
$endTime = $endDay; | |
} | |
} | |
return $endTime - $time; | |
} | |
function randomShow($maxLength, $programming) | |
{ | |
$tmpArray = array(); | |
foreach($programming as $p) | |
{ | |
if ($p["TotalSeconds"] < $maxLength) | |
{ | |
$tmpArray[] = $p; | |
} | |
} | |
return $tmpArray[array_rand($tmpArray, 1)]; | |
} | |
//Load Shows | |
$shows = $client->call('AdvancedShowSearch', array( | |
'ChannelID' => $channelID, | |
'searchString' => "", | |
'eventDate' => date("Y-m-d",mktime(0, 0, 0, date("m")-$monthsToLookBack, date("d"), date("Y"))), | |
'dateComparator' => '>', | |
'restrictToCategoryID' => 0, | |
'restrictToProducerID' => 0, | |
'restrictToProjectID' => 0, | |
'displayStreamingShowsOnly' => 'false', | |
'searchOtherSites' => 'true'), '', '', false, true); | |
foreach ($shows["AdvancedShowSearchResult"]["SiteSearchResult"]["Shows"]["ShowInfo"] as $show) { | |
if($show['CustomFields']['CustomField'] != "") | |
{ | |
foreach ($show["CustomFields"]["CustomField"] as $field) | |
{ | |
if($field['Name'] == "AutoSchedule" && $field['Value'] == 'x') | |
{ | |
$programming[] = array( | |
"showID" => $show["ShowID"], | |
"length" => $show["TotalSeconds"] | |
); | |
} | |
} | |
} | |
} | |
$CCschedule = $client->call('GetCGExemptScheduleInformation', array('ChannelID' => $channelID, | |
'FromDate' => date("Y-m-d",time() + (($startInDays) * 24 * 3600))."T12:00:00", | |
'ToDate' => date("Y-m-d",time() + (($scheduleForDays + $startInDays) * 24 * 3600))."T12:00:00", | |
'restrictToShowID' => 0), '', '', false, true); | |
foreach ($CCschedule["GetCGExemptScheduleInformationResult"]["ScheduleInfo"] as $run) | |
{ | |
$tempEvent = new ScheduleEvent(); | |
$tempEvent->start = strtotime($run["StartTime"]); | |
$tempEvent->end = strtotime($run["EndTime"]); | |
//echo $run["EndTime"]."\n"; | |
$schedule[] = $tempEvent; | |
} | |
$i=0; | |
while($i < $scheduleForDays) | |
{ | |
$dayStart = strtotime(date("Y-m-d", time()+(3600 * 24 * ($i +$startInDays)))."T00:00:00"); | |
$currentTime = $dayStart; | |
$dayEnd = strtotime(date("Y-m-d", time()+(3600 * 24 * ($i + $startInDays)))."T23:59:59"); | |
while($currentTime < $dayEnd) | |
{ | |
if(!isSomethingScheduled($currentTime, $schedule)) | |
{ | |
$maxLength = maxProgramLength($currentTime, $schedule, $dayEnd); | |
$newShow = randomShow($maxLength, $programming); | |
echo "Scheduling ShowID: ".$newShow["showID"]. " at ".date("Y-m-d", $currentTime)." ".date("H:i:s", $currentTime)."\n"; | |
$tempEvent = new ScheduleEvent(); | |
$tempEvent->start = $currentTime; | |
$tempEvent->end = $currentTime + $newShow["length"]; | |
$schedule[] = $tempEvent; | |
$result = $client->call('AddScheduleEvent', array( | |
'ChannelID' => $channelID, | |
'ShowID' => $newShow["showID"], | |
'RunDateTime' => date("Y-m-d", $currentTime)."T".date("H:i:s", $currentTime), | |
'CGExempt' => 'false', | |
'RunLock' => 'false', | |
'RunBump' => 0, | |
'BugText' => '', | |
'CrawlText' => '', | |
'CrawlLength' => 0, | |
'username' => "admin", | |
'password' => "password"), '', '', false, true); | |
} | |
$currentTime += 3600; | |
} | |
$i++; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment