public function processTvReleases($site, $numtoProcess = 100)
 {
     $ret = 0;
     $db = new DB();
     $tvmaze = new TVMaze();
     $tvdb = new TheTVDB();
     $lookupTvMaze = $site->lookuptvmaze;
     $lookuptheTvDB = $site->lookupthetvdb;
     // get all releases without a tvinfo which are in a tv category.
     $result = $db->queryDirect(sprintf("SELECT searchname, ID from releases where tvinfoID = -1 and categoryID in ( select ID from category where parentID = %d ) order by postdate desc limit %d ", Category::CAT_PARENT_TV, $numtoProcess));
     if ($db->getNumRows($result) > 0) {
         if ($this->echooutput) {
             echo "TVInfo  : Looking up " . $db->getNumRows($result) . " releases. TvMaze:" . ($lookupTvMaze ? "on" : "off") . " theTVDB:" . ($lookuptheTvDB ? "on" : "off") . "\n";
         }
         while ($arr = $db->getAssocArray($result)) {
             $show = $this->parseNameEpSeason($arr['searchname']);
             if ($show && $show['name'] != '') {
                 // find the ID locally
                 $tvinforow = $this->getTvInfoByTitle($show['cleanname']);
                 if ($tvinforow && $this->echooutput) {
                     echo sprintf("TVInfo  : Found %s %s %s\n", $show['cleanname'], $show['seriesfull'], $tvinforow['localonly'] == 1 ? "Local Only" : "");
                 }
                 // if not found locally try maze
                 if (!$tvinforow && $lookupTvMaze) {
                     if ($this->echooutput) {
                         echo "TVInfo  : Didnt find " . $show['cleanname'] . " locally, checking TvMaze\n";
                     }
                     $tvmShow = $tvmaze->singleSearch($show['cleanname']);
                     if ($tvmShow && $this->compare($show['cleanname'], $tvmShow->name) >= TvInfo::MATCH_PROBABILITY) {
                         $this->addTvInfoFromMaze($show['cleanname'], $tvmShow);
                         $tvinforow = $this->getByMazeID($tvmShow->id);
                         //get back out on maze id, not insertid, incase it failed to insert due to mazeid already existing
                     }
                 }
                 // if not found locally or maze try tvdb
                 if (!$tvinforow && $lookuptheTvDB) {
                     $seriesinfo = $tvdb->lookupSeriesID($show['cleanname']);
                     if ($seriesinfo && $seriesinfo["id"] > 0 && $this->compare($show['cleanname'], $seriesinfo["name"]) >= TvInfo::MATCH_PROBABILITY) {
                         $tvinforow = $this->getByTvdbID($seriesinfo["id"]);
                         if (!$tvinforow) {
                             if ($this->echooutput) {
                                 echo "TVInfo  : Didnt find " . $show['cleanname'] . " locally, checking theTVDB\n";
                             }
                             $tvdbShow = $tvdb->lookupSeries($seriesinfo["id"], $show['cleanname'], false);
                             $this->addTvInfoFromTVDB($show, $tvdbShow);
                             $tvinforow = $this->getByTvdbID($tvdbShow["tvdbID"]);
                             //get back out on tvdb id, not insertid, incase it failed to insert due to tvdbid already existing
                         }
                     }
                 }
                 // if tv try and get episode info
                 if ($tvinforow) {
                     $seriesfull = isset($show['seriesfull']) && !empty($show['seriesfull']) ? $db->escapeString($show['seriesfull']) : "null";
                     $season = isset($show['season']) && !empty($show['season']) ? $db->escapeString($show['season']) : "null";
                     $episode = isset($show['episode']) && !empty($show['episode']) ? $db->escapeString($show['episode']) : "null";
                     $tvairdate = isset($show['airdate']) && !empty($show['airdate']) ? $db->escapeString($show['airdate']) : "null";
                     $tvepisodetitle = "null";
                     $shortfullep = str_replace('S', '', $show['season']) . 'x' . str_replace('E', '', $show['episode']);
                     $episodeid = "null";
                     // only bother looking up for episode data if the match wasnt to a local tvinfo row
                     if ($tvinforow["localonly"] == 0) {
                         //check local releases to see if we already have the data (release or episodeinfo)
                         $epsql = sprintf("select tvtitle as title, tvairdate as airdate from releases\n                                 where tvairdate is not null and season = %s and episode = %s and tvinfoID = %d\n                                 union select eptitle as title, airdate from episodeinfo where tvinfoID = %d and fullep = %s", $db->escapeString($show['season']), $db->escapeString($show['episode']), $tvinforow["ID"], $tvinforow["ID"], $db->escapeString($shortfullep));
                         $epinfo = $db->queryOneRow($epsql);
                         // check maze for ep if mazeID is known
                         if (!$epinfo && $lookupTvMaze && $tvinforow["mazeID"]) {
                             if (strpos($show["seriesfull"], '/') !== false) {
                                 $tvmEpInfo = $tvmaze->getDatedEpisode($tvinforow["mazeID"], str_replace("/", "-", $show["seriesfull"]));
                             } else {
                                 $tvmEpInfo = $tvmaze->getNumberedEpisode($tvinforow["mazeID"], str_replace('S', '', $show['season']), str_replace('E', '', $show['episode']));
                             }
                             if ($tvmEpInfo) {
                                 $episodeid = $this->addEpisode(null, $tvmEpInfo->id, $tvinforow["releasetitle"], $tvmEpInfo->airdate, $tvmEpInfo->season, $tvmEpInfo->number, $tvmEpInfo->name, null, null, $tvmEpInfo->summary, null, null, null, $tvinforow["ID"]);
                                 $epinfo = $this->getEpisodeInfoByID($episodeid);
                             }
                         }
                         // check tvdb for ep if tvdb is known
                         if (!$epinfo && $lookuptheTvDB && $tvinforow["tvdbID"]) {
                             $tvdbEpInfo = $tvdb->lookupEpisode($tvinforow["tvdbID"], $show);
                             if ($tvdbEpInfo) {
                                 $episodeid = $this->addEpisode($tvdbEpInfo["id"], null, $tvinforow["releasetitle"], $tvdbEpInfo["airdate"], $tvdbEpInfo["season"], $tvdbEpInfo["number"], $tvdbEpInfo["name"], $tvdbEpInfo["director"], $tvdbEpInfo["gueststars"], $tvdbEpInfo["summary"], $tvdbEpInfo["rating"], $tvdbEpInfo["writer"], $tvdbEpInfo["epabsolute"], $tvinforow["ID"]);
                                 $epinfo = $this->getEpisodeInfoByID($episodeid);
                             }
                         }
                         if ($epinfo) {
                             if (!empty($epinfo['airdate'])) {
                                 $tvairdate = $db->escapeString($epinfo['airdate']);
                             }
                             if (!empty($epinfo['eptitle'])) {
                                 $tvepisodetitle = $db->escapeString($epinfo['eptitle']);
                             }
                         }
                     }
                     $db->exec(sprintf("update releases set tvinfoID=%d, seriesfull = %s, season = %s, episode = %s, tvairdate=%s, tvtitle=%s, episodeinfoID=%s where ID = %d", $tvinforow["ID"], $seriesfull, $season, $episode, $tvairdate, $tvepisodetitle, $episodeid, $arr['ID']));
                 } else {
                     $this->markAsNotFound($show, $arr["ID"]);
                 }
             } else {
                 $this->markAsNotFound($show, $arr["ID"]);
             }
             $ret++;
         }
     }
     return $ret;
 }
<?php

define('FS_ROOT', realpath(dirname(__FILE__)));
require_once FS_ROOT . "/../../www/config.php";
require_once FS_ROOT . "/../../www/lib/framework/db.php";
require_once FS_ROOT . "/../../www/lib/tvmaze.php";
require_once FS_ROOT . "/../../www/lib/tvinfo.php";
require_once FS_ROOT . "/../../www/lib/category.php";
/*
 *  Get a sample of releases from tv category and test results against TVMaze SingleSearch
 *  http://www.tvmaze.com/api
 */
$db = new DB();
$tvinfo = new TvInfo();
$tvmaze = new TVMaze();
$matches = $totalchecked = 0;
$sql = sprintf("select distinct searchname from releases where categoryID in (%d, %d) limit 100", Category::CAT_TV_HD, Category::CAT_TV_SD);
$rows = $db->query($sql);
$cleannames = array();
foreach ($rows as $row) {
    $show = $tvinfo->parseNameEpSeason($row['searchname']);
    $cleannames[] = $show["cleanname"];
}
foreach (array_unique($cleannames) as $row) {
    if ($row && starts_with_upper($row)) {
        $totalchecked++;
        $mazerow = $tvmaze->singleSearch($row);
        if ($mazerow) {
            echo sprintf("Y Show: %s | Maze: %s : %s%%\n", $row, $mazerow->name, $tvinfo->compare($row, $mazerow->name), $matches++);
            print_r($mazerow->externalIDs);
        } else {