public function updateSchedule() { $countries = $this->pdo->query("\n\t\t\t\t\t\tSELECT DISTINCT(country) AS country\n\t\t\t\t\t\tFROM tvrage\n\t\t\t\t\t\tWHERE country != ''"); $showsindb = $this->pdo->query("\n\t\t\t\t\t\tSELECT DISTINCT(rageid) AS rageid\n\t\t\t\t\t\tFROM tvrage"); $showarray = []; foreach ($showsindb as $show) { $showarray[] = $show['rageid']; } foreach ($countries as $country) { if ($this->echooutput) { echo $this->pdo->log->headerOver('Updating schedule for: ') . $this->pdo->log->primary($country['country']); } $sched = Utility::getURL(['url' => $this->xmlFullScheduleUrl . $country['country']]); if ($sched !== false && ($xml = @simplexml_load_string($sched))) { $tzOffset = 60 * 60 * 6; $yesterday = strtotime("-1 day") - $tzOffset; $xmlSchedule = []; foreach ($xml->DAY as $sDay) { $currDay = strtotime($sDay['attr']); foreach ($sDay as $sTime) { $currTime = (string) $sTime['attr']; foreach ($sTime as $sShow) { $currShowName = (string) $sShow['name']; $currShowId = (string) $sShow->sid; $day_time = strtotime($sDay['attr'] . ' ' . $currTime); $tag = $currDay < $yesterday ? 'prev' : 'next'; if ($tag == 'prev' || $tag == 'next' && !isset($xmlSchedule[$currShowId]['next'])) { $xmlSchedule[$currShowId][$tag] = ['name' => $currShowName, 'day' => $currDay, 'time' => $currTime, 'day_time' => $day_time, 'day_date' => date("Y-m-d H:i:s", $day_time), 'title' => html_entity_decode((string) $sShow->title, ENT_QUOTES, 'UTF-8'), 'episode' => html_entity_decode((string) $sShow->ep, ENT_QUOTES, 'UTF-8')]; $xmlSchedule[$currShowId]['showname'] = $currShowName; } // Only add it here, no point adding it to tvrage aswell that will automatically happen when an ep gets posted. if ($sShow->ep == "01x01") { $showarray[] = $sShow->sid; } // Only stick current shows and new shows in there. if (in_array($currShowId, $showarray)) { $this->pdo->queryExec(sprintf("\n\t\t\t\t\t\t\t\t\t\t\tINSERT INTO episodeinfo (rageid, showtitle, fullep, airdate, link, eptitle)\n\t\t\t\t\t\t\t\t\t\t\tVALUES (%d, %s, %s, %s, %s, %s)\n\t\t\t\t\t\t\t\t\t\t\tON DUPLICATE KEY UPDATE\n\t\t\t\t\t\t\t\t\t\t\t\tshowtitle = %2\$s, airdate = %4\$s, link = %5\$s ,eptitle = %6\$s", $sShow->sid, $this->pdo->escapeString($currShowName), $this->pdo->escapeString($sShow->ep), $this->pdo->escapeString(date("Y-m-d H:i:s", $day_time)), $this->pdo->escapeString($sShow->link), $this->pdo->escapeString($sShow->title), $this->pdo->escapeString(date("Y-m-d H:i:s", $day_time)))); } } } } // Update series info. foreach ($xmlSchedule as $showId => $epInfo) { $res = $this->pdo->query(sprintf("SELECT * FROM tvrage WHERE rageid = %d", $showId)); if (sizeof($res) > 0) { foreach ($res as $arr) { $prev_ep = $next_ep = ""; $query = []; // Previous episode. if (isset($epInfo['prev']) && $epInfo['prev']['episode'] != '') { $prev_ep = $epInfo['prev']['episode'] . ', "' . $epInfo['prev']['title'] . '"'; $query[] = sprintf("prevdate = %s, previnfo = %s", $this->pdo->from_unixtime($epInfo['prev']['day_time']), $this->pdo->escapeString($prev_ep)); } // Next episode. if (isset($epInfo['next']) && $epInfo['next']['episode'] != '') { if ($prev_ep == "" && $arr['nextinfo'] != '' && $epInfo['next']['day_time'] > strtotime($arr["nextdate"]) && strtotime(date('Y-m-d', strtotime($arr["nextdate"]))) < $yesterday) { $this->pdo->queryExec(sprintf("\n\t\t\t\t\t\t\t\t\t\t\t\tUPDATE tvrage\n\t\t\t\t\t\t\t\t\t\t\t\tSET prevdate = nextdate, previnfo = nextinfo\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE id = %d", $arr['id'])); $prev_ep = "SWAPPED with: " . $arr['nextinfo'] . " - " . date("r", strtotime($arr["nextdate"])); } $next_ep = $epInfo['next']['episode'] . ', "' . $epInfo['next']['title'] . '"'; $query[] = sprintf("nextdate = %s, nextinfo = %s", $this->pdo->from_unixtime($epInfo['next']['day_time']), $this->pdo->escapeString($next_ep)); } else { $query[] = "nextdate = NULL, nextinfo = NULL"; } // Output. if ($this->echooutput) { echo $this->pdo->log->primary($epInfo['showname'] . " (" . $showId . "):"); if (isset($epInfo['prev']['day_time'])) { echo $this->pdo->log->headerOver("Prev EP: ") . $this->pdo->log->primary("{$prev_ep} - " . date("m/d/Y H:i T", $epInfo['prev']['day_time'])); } if (isset($epInfo['next']['day_time'])) { echo $this->pdo->log->headerOver("Next EP: ") . $this->pdo->log->primary("{$next_ep} - " . date("m/d/Y H:i T", $epInfo['next']['day_time'])); } echo "\n"; } // Update info. if (count($query) > 0) { $sqlQry = join(", ", $query); $this->pdo->queryExec(sprintf("\n\t\t\t\t\t\t\t\t\t\t\tUPDATE tvrage\n\t\t\t\t\t\t\t\t\t\t\tSET %s\n\t\t\t\t\t\t\t\t\t\t\tWHERE id = %d", $sqlQry, $arr['id'])); } } } } } else { // No response from tvrage. if ($this->echooutput) { echo $this->pdo->log->info("Schedule not found."); } } } if ($this->echooutput) { echo $this->pdo->log->primary("Updated the TVRage schedule succesfully."); } }
public function updateSchedule() { $countries = $this->pdo->query("select distinct(country) as country from tvrage where country != ''"); $showsindb = $this->pdo->query("select distinct(rageid) as rageid from tvrage"); $showarray = array(); foreach ($showsindb as $show) { $showarray[] = $show['rageid']; } if ($this->echooutput) { echo 'TVRage : Updating schedule...'; } foreach ($countries as $country) { if ($this->echooutput) { echo '..' . strtoupper($country['country']) . ".."; } try { $raw = Utility::getURL($this->xmlFullScheduleUrl . $country['country']); $xml = new SimpleXMLElement($raw); $sched = $xml->xpath('/schedule/DAY'); } catch (Exception $e) { # Simply no data or unparseable continue; } if ($sched !== false) { $tzOffset = 60 * 60 * 6; $yesterday = strtotime("-1 day") - $tzOffset; $xmlSchedule = array(); foreach ($sched as $dayObj) { $currDay = (string) $dayObj['attr']; foreach ($dayObj->time as $sTime) { $currTime = (string) $sTime['attr']; foreach ($sTime as $sShow) { $currShowName = (string) $sShow['name']; $currShowId = (string) $sShow->sid; $day_time = strtotime($currDay . ' ' . $currTime); $tag = $currDay < $yesterday ? 'prev' : 'next'; if ($tag == 'prev' || $tag == 'next' && !isset($xmlSchedule[$currShowId]['next'])) { $xmlSchedule[$currShowId][$tag] = array('name' => $currShowName, 'day' => $currDay, 'time' => $currTime, 'day_time' => $day_time, 'day_date' => date("Y-m-d H:i:s", $day_time), 'title' => html_entity_decode((string) $sShow->title, ENT_QUOTES, 'UTF-8'), 'episode' => html_entity_decode((string) $sShow->ep, ENT_QUOTES, 'UTF-8')); $xmlSchedule[$currShowId]['showname'] = $currShowName; } if ($sShow->ep == "01x01") { // Only add it here, no point adding it to tvrage aswell // that will automatically happen when an ep gets posted $showarray[] = $sShow->sid; } if (in_array($currShowId, $showarray)) { $showname = $this->pdo->escapeString($currShowName); $title = $this->pdo->escapeString($sShow->title); $fullep = $this->pdo->escapeString($sShow->ep); $link = $this->pdo->escapeString($sShow->link); $airdate = $this->pdo->escapeString(date("Y-m-d H:i:s", $day_time)); $sql = sprintf('INSERT into episodeinfo (rageid,showtitle,fullep,airdate,link,eptitle) VALUES (%d,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE rageid = %1$d, airdate = %4$s, link = %5$s, eptitle = %6$s, showtitle = %2$s', $sShow->sid, $showname, $fullep, $airdate, $link, $title); $this->pdo->queryInsert($sql); } } } } // update series info foreach ($xmlSchedule as $showId => $epInfo) { $res = $this->pdo->query(sprintf("select *, UNIX_TIMESTAMP(nextdate) as nextDateU, UNIX_TIMESTAMP(DATE(nextdate)) as nextDateDay from tvrage where rageid = %d", $showId)); if (sizeof($res) > 0) { foreach ($res as $arr) { $prev_ep = $next_ep = ""; $query = array(); // previous episode if (isset($epInfo['prev']) && $epInfo['prev']['episode'] != '') { $prev_ep = $epInfo['prev']['episode'] . ', "' . $epInfo['prev']['title'] . '"'; $query[] = sprintf("prevdate = FROM_UNIXTIME(%s), previnfo = %s", $epInfo['prev']['day_time'], $this->pdo->escapeString($prev_ep)); } // next episode if (isset($epInfo['next']) && $epInfo['next']['episode'] != '') { if ($prev_ep == "" && $arr['nextinfo'] != '' && $epInfo['next']['day_time'] > $arr['nextDateU'] && $arr['nextDateDay'] < $yesterday) { $this->pdo->queryExec(sprintf("update tvrage set prevdate = nextdate, previnfo = nextinfo where id = %d", $arr['id'])); $prev_ep = "SWAPPED with: " . $arr['nextinfo'] . " - " . date("r", $arr['nextDateU']); } $next_ep = $epInfo['next']['episode'] . ', "' . $epInfo['next']['title'] . '"'; $query[] = sprintf("nextdate = FROM_UNIXTIME(%s), nextinfo = %s", $epInfo['next']['day_time'], $this->pdo->escapeString($next_ep)); } else { $query[] = "nextdate = null, nextinfo = null"; } // output /* if ($this->echooutput) { echo $epInfo['showname']." (".$showId."):\n"; echo " -prev: {$prev_ep} - ".(isset($epInfo['prev']['day_time']) ? date("r",$epInfo['prev']['day_time']) : "")."\n"; echo " -next: {$next_ep} - ".(isset($epInfo['next']['day_time']) ? date("r",$epInfo['next']['day_time']) : "")."\n"; } */ // update info if (count($query) > 0) { $sql = str_ireplace("%", "%%", join(", ", $query)); $sql = sprintf("update tvrage set {$sql} where id = %d", $arr['id']); $this->pdo->queryExec($sql); } } } } // end update series info } } // end foreach country if ($this->echooutput) { echo "TVRage : Schedule complete...\n"; } }