From c67307841aba4757ac7b1a70acfd94df2dda383c Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 30 Oct 2015 23:38:18 +0100 Subject: [PATCH] Fix problems created by moving TVMaze up in processing. --- Changelog | 1 + newznab/libraries/TraktAPI.php | 8 +-- newznab/processing/PostProcess.php | 2 +- newznab/processing/tv/TV.php | 73 +++++++++++++++--------- newznab/processing/tv/TVDB.php | 44 +++++++++------ newznab/processing/tv/TVMaze.php | 18 +++--- newznab/processing/tv/TvRage.php | 89 ++++++++++++++---------------- 7 files changed, 129 insertions(+), 106 deletions(-) diff --git a/Changelog b/Changelog index 2b4aabf5c..dbe190c26 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2015-10-30 DariusIII + Fix: Some of the problems created by moving TVMaze up in processing Chg: Move ApaiIO and Tmdb libraries into libs folder Chg: Simplify the is_array Chg: Change admin/user-edit diff --git a/newznab/libraries/TraktAPI.php b/newznab/libraries/TraktAPI.php index c20d16b8f..c9d8492eb 100644 --- a/newznab/libraries/TraktAPI.php +++ b/newznab/libraries/TraktAPI.php @@ -227,7 +227,7 @@ HEADERS; * * @param integer $id The ID to look for. * @param string $site One of the supported sites ('imdb', 'tmdb', 'trakt', 'tvdb', 'tvrage') - * @param string $type videos.type flag (-1 for episodes). + * @param integer $type videos.type flag (-1 for episodes). * * @return bool */ @@ -321,10 +321,8 @@ HEADERS; */ public function slugify($phrase) { - $result = strtolower($phrase); - $result = preg_replace('#[^a-z0-9\s-]#', '', $result); - $result = trim(preg_replace('#[\s-]+#', ' ', $result)); - $result = preg_replace('#\s#', '-', $result); + $result = preg_replace('#[^a-z0-9\s-]#', '', strtolower($phrase)); + $result = preg_replace('#\s#', '-', trim(preg_replace('#[\s-]+#', ' ', $result))); return $result; } diff --git a/newznab/processing/PostProcess.php b/newznab/processing/PostProcess.php index 1758f7e92..1d077cc6d 100755 --- a/newznab/processing/PostProcess.php +++ b/newznab/processing/PostProcess.php @@ -260,8 +260,8 @@ class PostProcess $processTV = (is_numeric($processTV) ? $processTV : $this->pdo->getSetting('lookuptvrage')); if ($processTV > 0) { (new TVDB(['Echo' => $this->echooutput, 'Settings' => $this->pdo]))->processTVDB($groupID, $guidChar, $processTV); - //(new TvRage(['Echo' => $this->echooutput, 'Settings' => $this->pdo]))->processTvRage($groupID, $guidChar, $processTV); (new TVMaze(['Echo' => $this->echooutput, 'Settings' => $this->pdo]))->processTVMaze($groupID, $guidChar, $processTV); + //(new TvRage(['Echo' => $this->echooutput, 'Settings' => $this->pdo]))->processTvRage($groupID, $guidChar, $processTV); } } diff --git a/newznab/processing/tv/TV.php b/newznab/processing/tv/TV.php index 26e64bc8b..223bc3723 100644 --- a/newznab/processing/tv/TV.php +++ b/newznab/processing/tv/TV.php @@ -9,7 +9,7 @@ use newznab\processing\Videos; */ abstract class TV extends Videos { -// Television Sources + // Television Sources const SOURCE_NONE = 0; // No Scrape source const SOURCE_TVDB = 1; // Scrape source was TVDB const SOURCE_TVMAZE = 2; // Scrape source was TVMAZE @@ -45,6 +45,11 @@ abstract class TV extends Videos */ public $imgSavePath; + /** + * @var array Site ID columns for TV + */ + public $siteColumns; + /** * @param array $options Class instances / Echo to CLI. */ @@ -61,6 +66,7 @@ abstract class TV extends Videos $this->catWhere = 'categoryid BETWEEN 5000 AND 5999 AND categoryid NOT IN (5070)'; $this->tvqty = ($this->pdo->getSetting('maxrageprocessed') != '') ? $this->pdo->getSetting('maxrageprocessed') : 75; $this->imgSavePath = NN_COVERS . 'tvshows' . DS; + $this->siteColumns = ['tvdb', 'trakt', 'tvrage', 'tvmaze', 'imdb', 'tmdb']; } /** @@ -197,14 +203,23 @@ abstract class TV extends Videos */ public function add($showArr = array()) { + $videoId = false; + + // Check if the country is not a proper code and retrieve if not if ($showArr['country'] !== '' && strlen($showArr['country']) > 2) { $showArr['country'] = $this->countryCode($showArr['country']); } - // Check if video already exists based on site info + // Check if video already exists based on site ID info // if that fails be sure we're not inserting duplicates by checking the title - - $videoId = $this->getVideoIDFromSiteID($showArr['column'], $showArr['siteid']); + foreach ($this->siteColumns AS $column) { + if ($showArr[$column] > 0) { + $videoId = $this->getVideoIDFromSiteID($column, $showArr[$column]); + } + if ($videoId !== false) { + break; + } + } if ($videoId === false) { $videoId = $this->getByTitleQuery($showArr['title']); @@ -220,12 +235,12 @@ abstract class TV extends Videos $this->pdo->escapeString((isset($showArr['country']) ? $showArr['country'] : '')), $this->pdo->escapeString($showArr['started']), $showArr['source'], - $showArr['tvdbid'], - $showArr['traktid'], - $showArr['tvrageid'], - $showArr['tvmazeid'], - $showArr['imdbid'], - $showArr['tmdbid'] + $showArr['tvdb'], + $showArr['trakt'], + $showArr['tvrage'], + $showArr['tvmaze'], + $showArr['imdb'], + $showArr['tmdb'] ) ); $this->pdo->queryInsert( @@ -290,20 +305,26 @@ abstract class TV extends Videos $showArr['country'] = $this->countryCode($showArr['country']); } - $ifString = 'IF(%s = 0, %s, %s)'; + $ifStringID = 'IF(%s = 0, %s, %s)'; + $ifStringInfo = "IF(%s = '', %s, %s)"; $this->pdo->queryExec( sprintf(' - UPDATE videos - SET countries_id = %s, tvdb = %s, trakt = %s, tvrage = %s, tvmaze = %s, imdb = %s, tmdb = %s - WHERE id = %d', - $this->pdo->escapeString((isset($showArr['country']) ? $showArr['country'] : '')), - sprintf($ifString, 'tvdb', $showArr['tvdbid'], 'tvdb'), - sprintf($ifString, 'trakt', $showArr['traktid'], 'trakt'), - sprintf($ifString, 'tvrage', $showArr['tvrageid'], 'tvrage'), - sprintf($ifString, 'tvmaze', $showArr['tvmazeid'], 'tvmaze'), - sprintf($ifString, 'imdb', $showArr['imdbid'], 'imdb'), - sprintf($ifString, 'tmdb', $showArr['tmdbid'], 'tmdb'), + UPDATE videos v + LEFT JOIN tv_info tvi ON v.id = tvi.videos_id + SET v.countries_id = %s, v.tvdb = %s, v.trakt = %s, v.tvrage = %s, + v.tvmaze = %s, v.imdb = %s, v.tmdb = %s, + tvi.summary = %s, tvi.publisher = %s + WHERE v.id = %d', + sprintf($ifStringInfo, 'v.countries_id', $this->pdo->escapeString($showArr['country']), 'v.countries_id'), + sprintf($ifStringID, 'v.tvdb', $showArr['tvdb'], 'v.tvdb'), + sprintf($ifStringID, 'v.trakt', $showArr['trakt'], 'v.trakt'), + sprintf($ifStringID, 'v.tvrage', $showArr['tvrage'], 'v.tvrage'), + sprintf($ifStringID, 'v.tvmaze', $showArr['tvmaze'], 'v.tvmaze'), + sprintf($ifStringID, 'v.imdb', $showArr['imdb'], 'v.imdb'), + sprintf($ifStringID, 'v.tmdb', $showArr['tmdb'], 'v.tmdb'), + sprintf($ifStringInfo, 'tvi.summary', $this->pdo->escapeString($showArr['summary']), 'tvi.summary'), + sprintf($ifStringInfo, 'tvi.publisher', $this->pdo->escapeString($showArr['publisher']), 'tvi.publisher'), $videoId ) ); @@ -624,9 +645,9 @@ abstract class TV extends Videos ]; $matches = ''; - $following = '[^a-z0-9](\d\d-\d\d|\d{1,2}x\d{2,3}|\(?(19|20)\d{2}\)?|(480|720|1080)[ip]|AAC2?|BD-?Rip|Blu-?Ray|D0?\d' . + $following = '[^a-z0-9](\d\d-\d\d|\d{1,3}x\d{2,3}|\(?(19|20)\d{2}\)?|(480|720|1080)[ip]|AAC2?|BD-?Rip|Blu-?Ray|D0?\d' . '|DD5|DiVX|DLMux|DTS|DVD(-?Rip)?|E\d{2,3}|[HX][-_. ]?26[45]|ITA(-ENG)?|HEVC|[HPS]DTV|PROPER|REPACK|Season|Episode|' . - 'S\d+[^a-z0-9]?(E\d+)?|WEB[-_. ]?(DL|Rip)|XViD)[^a-z0-9]'; + 'S\d+[^a-z0-9]?(E\d+)?[ab]?|WEB[-_. ]?(DL|Rip)|XViD)[^a-z0-9]'; // For names that don't start with the title. if (preg_match('/[^a-z0-9]{2,}(?P[\w .-]*?)' . $following . '/i', $relname, $matches)) { @@ -682,7 +703,7 @@ abstract class TV extends Videos // 01.01.09 else if (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]/i', $relname, $matches)) { // Add extra logic to capture the proper YYYY year - $showInfo['season'] = $matches[4] = ($matches[4] <= 99 && $matches[4] > 15) ? '19' . $matches[4] : '20' . $matches[5]; + $showInfo['season'] = $matches[4] = ($matches[4] <= 99 && $matches[4] > 15) ? '19' . $matches[4] : '20' . $matches[4]; $showInfo['episode'] = $matches[2] . '/' . $matches[3]; $tmpAirdate = $showInfo['season'] . '/' . $showInfo['episode']; $showInfo['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $tmpAirdate))); //yyyy-mm-dd @@ -721,7 +742,7 @@ abstract class TV extends Videos $countryMatch = $yearMatch = ''; // Country or origin matching. - if (preg_match('/\W(US|UK|AU|NZ|CA|NL|Canada|Australia|America|United[^a-z0-9]States|United[^a-z0-9]Kingdom)\W/', $showInfo['name'], $countryMatch)) { + if (preg_match('/[^a-z0-9](US|UK|AU|NZ|CA|NL|Canada|Australia|America|United[^a-z0-9]States|United[^a-z0-9]Kingdom)[^a-z0-9]/i', $showInfo['name'], $countryMatch)) { $currentCountry = strtolower($countryMatch[1]); if ($currentCountry == 'canada') { $showInfo['country'] = 'CA'; @@ -734,6 +755,8 @@ abstract class TV extends Videos } else { $showInfo['country'] = strtoupper($countryMatch[1]); } + } else { + $showInfo['country'] = ''; } // Clean show name. diff --git a/newznab/processing/tv/TVDB.php b/newznab/processing/tv/TVDB.php index 3a86666db..e2f14b21c 100644 --- a/newznab/processing/tv/TVDB.php +++ b/newznab/processing/tv/TVDB.php @@ -109,17 +109,21 @@ class TVDB extends TV $this->pdo->log->primary(" not found in local db, checking web."); } + // Check if we have a valid country and set it in the array + $country = (isset($release['country']) && strlen($release['country']) == 2 + ? (string)$release['country'] + : '' + ); + // Get the show from TVDB - $tvdbShow = $this->getShowInfo((string)$release['cleanname']); + $tvdbShow = $this->getShowInfo((string)$release['cleanname'], $country); if (is_array($tvdbShow)) { - $tvdbShow['country'] = (isset($release['country']) && $release['country'] !== 2 - ? (string)$release['country'] - : '' - ); + $tvdbShow['country'] = $country; $videoId = $this->add($tvdbShow); - $tvdbid = (int)$tvdbShow['tvdbid']; + $tvdbid = (int)$tvdbShow['tvdb']; } + } else if ($this->echooutput) { echo $this->pdo->log->primaryOver("Video ID for ") . $this->pdo->log->headerOver($release['cleanname']) . @@ -172,7 +176,7 @@ class TVDB extends TV } } } //Processing failed, set the episode ID to the next processing group - $this->setVideoNotFound(parent::PROCESS_TRAKT, $row['id']); + $this->setVideoNotFound(parent::PROCESS_TVMAZE, $row['id']); } } } @@ -194,11 +198,13 @@ class TVDB extends TV * Calls the API to perform initial show name match to TVDB title * Returns a formatted array of show data or false if no match * - * @param $cleanName + * @param string $cleanName + * + * @param string $country * * @return array|bool */ - protected function getShowInfo($cleanName) + protected function getShowInfo($cleanName, $country = '') { $return = $response = false; $highestMatch = 0; @@ -207,6 +213,12 @@ class TVDB extends TV } catch (\Exception $error) { } + if ($response === false && $country !== '') { + try { + $response = (array)$this->client->getSeries(rtrim(str_replace($country, '', $cleanName)), 'en'); + } catch (\Exception $error) { } + } + sleep(1); if (is_array($response)) { @@ -336,19 +348,17 @@ class TVDB extends TV preg_match('/tt(?P\d{6,7})$/i', $show->imdbId, $imdb); return [ - 'tvdbid' => (int)$show->id, - 'column' => 'tvdb', - 'siteid' => (int)$show->id, 'title' => (string)$show->name, 'summary' => (string)$show->overview, 'started' => (string)$show->firstAired->format($this->timeFormat), 'publisher' => (string)$show->network, 'source' => (int)parent::SOURCE_TVDB, - 'imdbid' => (int)(isset($imdb['imdbid']) ? $imdb['imdbid'] : 0), - 'traktid' => 0, - 'tvrageid' => 0, - 'tvmazeid' => 0, - 'tmdbid' => 0 + 'imdb' => (int)(isset($imdb['imdbid']) ? $imdb['imdbid'] : 0), + 'tvdb' => (int)$show->id, + 'trakt' => 0, + 'tvrage' => 0, + 'tvmaze' => 0, + 'tmdb' => 0 ]; } diff --git a/newznab/processing/tv/TVMaze.php b/newznab/processing/tv/TVMaze.php index 323dbe58f..261023cd6 100644 --- a/newznab/processing/tv/TVMaze.php +++ b/newznab/processing/tv/TVMaze.php @@ -108,7 +108,7 @@ class TVMaze extends TV if (is_array($tvmazeShow)) { $videoId = $this->add($tvmazeShow); - $tvmazeid = (int)$tvmazeShow['tvmazeid']; + $tvmazeid = (int)$tvmazeShow['tvmaze']; } } else if ($this->echooutput) { echo $this->pdo->log->primaryOver("Video ID for ") . @@ -162,7 +162,7 @@ class TVMaze extends TV } } } //Processing failed, set the episode ID to the next processing group - $this->setVideoNotFound(parent::PROCESS_IMDB, $row['id']); + $this->setVideoNotFound(parent::PROCESS_TRAKT, $row['id']); } } } @@ -347,20 +347,18 @@ class TVMaze extends TV $this->posterUrl = (string)(isset($show->mediumImage) ? $show->mediumImage : ''); return [ - 'tvmazeid' => (int)$show->id, - 'column' => 'tvmaze', - 'siteid' => (int)$show->id, 'title' => (string)$show->name, 'summary' => (string)$show->summary, 'started' => (string)$show->premiered, 'publisher' => (string)$show->network, 'country' => (string)$show->country, 'source' => (int)parent::SOURCE_TVMAZE, - 'imdbid' => 0, - 'tvdbid' => (int)(isset($show->externalIDs['thetvdb']) ? $show->externalIDs['thetvdb'] : 0), - 'traktid' => 0, - 'tvrageid' => (int)(isset($show->externalIDs['tvrage']) ? $show->externalIDs['tvrage'] : 0), - 'tmdbid' => 0 + 'imdb' => 0, + 'tvdb' => (int)(isset($show->externalIDs['thetvdb']) ? $show->externalIDs['thetvdb'] : 0), + 'tvmaze' => (int)$show->id, + 'trakt' => 0, + 'tvrage' => (int)(isset($show->externalIDs['tvrage']) ? $show->externalIDs['tvrage'] : 0), + 'tmdb' => 0 ]; } diff --git a/newznab/processing/tv/TvRage.php b/newznab/processing/tv/TvRage.php index 67b805723..6a89aa23d 100644 --- a/newznab/processing/tv/TvRage.php +++ b/newznab/processing/tv/TvRage.php @@ -13,11 +13,6 @@ class TvRage extends TV const APIKEY = '7FwjZ8loweFcOhHfnU3E'; const MATCH_PROBABILITY = 75; - /** - * @var array|bool|int|string - */ - public $rageqty; - /** * @var string */ @@ -74,7 +69,6 @@ class TvRage extends TV public function __construct(array $options = []) { parent::__construct($options); - $this->rageqty = ($this->pdo->getSetting('maxrageprocessed') != '') ? $this->pdo->getSetting('maxrageprocessed') : 75; $this->xmlEpisodeInfoUrl = "http://services.tvrage.com/myfeeds/episodeinfo.php?key=" . TvRage::APIKEY; $this->imgSavePath = NN_COVERS . 'tvrage' . DS; } @@ -155,11 +149,11 @@ class TvRage extends TV $this->setVideoIdFound($this->videoId, $arr['id'], $episodeId); // Cant find videos_id, so set tv_episodes_id to PROCESS_TVMAZE. } else { - $this->setVideoNotFound(parent::PROCESS_TVMAZE, $arr['id']); + $this->setVideoNotFound(parent::NO_MATCH_FOUND, $arr['id']); } // Not a tv episode, so set videos_id to n/a. } else { - $this->setVideoNotFound(parent::PROCESS_TVMAZE, $arr['id']); + $this->setVideoNotFound(parent::NO_MATCH_FOUND, $arr['id']); } } } @@ -370,64 +364,36 @@ class TvRage extends TV $highestPercent = 0; - foreach ($arrXml['show'] as $arr) { + foreach ($arrXml['show'] as $show) { - if ($title == $arr['name']) { - $matchedTitle = [ - 'title' => $arr['name'], - 'showid' => $arr['showid'], - 'country' => $this->countryCode($arr['country']), - 'publisher' => $arr['network'], - 'started' => date('Y-m-d', strtotime($arr['started'])), - 'tvr' => $arr - ]; + if ($title == $show['name']) { + $matchedTitle = $show; break; } // Get a match percentage based on our name and the name returned from tvr. - $matchPercent = $this->checkMatch($title, $arr['name'], self::MATCH_PROBABILITY); + $matchPercent = $this->checkMatch($title, $show['name'], self::MATCH_PROBABILITY); if ($matchPercent > $highestPercent) { - $matchedTitle = [ - 'title' => $arr['name'], - 'showid' => $arr['showid'], - 'country' => $this->countryCode($arr['country']), - 'publisher' => $arr['network'], - 'started' => date('Y-m-d', strtotime($arr['started'])), - 'tvr' => $arr - ]; + $matchedTitle = $show; $highestPercent = $matchPercent; } // Check if there are any akas for this result and get a match percentage for them too. - if (isset($arr['akas']['aka'])) { - if (is_array($arr['akas']['aka'])) { + if (isset($show['akas']['aka'])) { + if (is_array($show['akas']['aka'])) { // Multiple akas. - foreach ($arr['akas']['aka'] as $aka) { + foreach ($show['akas']['aka'] as $aka) { $matchPercent = $this->checkMatch($title, $aka, self::MATCH_PROBABILITY); if ($matchPercent > $highestPercent) { - $matchedTitle = [ - 'title' => $arr['name'], - 'showid' => $arr['showid'], - 'country' => $this->countryCode($arr['country']), - 'publisher' => $arr['network'], - 'started' => date('Y-m-d', strtotime($arr['started'])), - 'tvr' => $arr - ]; + $matchedTitle = $show; $highestPercent = $matchPercent; } } } else { // One aka. - $matchPercent = $this->checkMatch($title, $arr['akas']['aka'], self::MATCH_PROBABILITY); + $matchPercent = $this->checkMatch($title, $show['akas']['aka'], self::MATCH_PROBABILITY); if ($matchPercent > $highestPercent) { - $matchedTitle = [ - 'title' => $arr['name'], - 'showid' => $arr['showid'], - 'country' => $this->countryCode($arr['country']), - 'publisher' => $arr['network'], - 'started' => date('Y-m-d', strtotime($arr['started'])), - 'tvr' => $arr - ]; + $matchedTitle = $show; $highestPercent = $matchPercent; } } @@ -439,6 +405,33 @@ class TvRage extends TV } } } - return $matchedTitle; + return $this->formatShowArr($matchedTitle); + } + + /** + * Assigns API show response values to a formatted array for insertion + * Returns the formatted array + * + * @param $show + * + * @return array + */ + private function formatShowArr($show) + { + return [ + 'title' => (string)$show['name'], + 'summary' => (string)'', + 'started' => (string)date('Y-m-d', strtotime($show['started'])), + 'publisher' => (string)$show['network'], + 'country' => (string)$show['country'], + 'source' => (int)parent::SOURCE_TVRAGE, + 'imdb' => 0, + 'tvdb' => 0, + 'trakt' => 0, + 'tvrage' => (int)$show['showid'], + 'tvmaze' => 0, + 'tmdb' => 0, + 'tvr' => $show + ]; } }