예제 #1
0
 public function updateSchedule()
 {
     $countries = $this->pdo->query("SELECT DISTINCT(country) AS country FROM tvrage_titles WHERE country != ''");
     $showsindb = $this->pdo->query("SELECT DISTINCT(rageid) AS rageid FROM tvrage_titles");
     $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 = Misc::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("INSERT INTO tvrage_episodes (rageid, showtitle, fullep, airdate, link, eptitle) VALUES (%d, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE airdate = %s, link = %s ,eptitle = %s, showtitle = %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)), $this->pdo->escapeString($sShow->link), $this->pdo->escapeString($sShow->title), $this->pdo->escapeString($currShowName)));
                         }
                     }
                 }
             }
             // Update series info.
             foreach ($xmlSchedule as $showId => $epInfo) {
                 $res = $this->pdo->query(sprintf("SELECT * FROM tvrage_titles 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("UPDATE tvrage_titles SET prevdate = nextdate, previnfo = nextinfo WHERE 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) {
                             $sql = join(", ", $query);
                             $sql = sprintf("UPDATE tvrage_titles SET {$sql} WHERE id = %d", $arr['id']);
                             $this->pdo->queryExec($sql);
                         }
                     }
                 }
             }
         } 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.");
     }
 }