From dddf4614ccc2a571c942ecb2aeb72a452cba2ddc Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 8 Jun 2016 09:14:56 +0200 Subject: [PATCH] AniDB code cleanup, ported from nZEDb --- Changelog | 2 + build/nntmux.xml | 2 +- newznab/AniDB.php | 76 +++++++----- newznab/db/populate/AniDB.php | 162 ++++++++++++------------ newznab/db/populate/PopulateTitles.php | 145 ++++++++++++++++++++++ newznab/processing/post/AniDB.php | 165 +++++++++++++------------ 6 files changed, 364 insertions(+), 188 deletions(-) create mode 100644 newznab/db/populate/PopulateTitles.php diff --git a/Changelog b/Changelog index 408ce2cd3..e2786670e 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2016-06-08 DariusIII + * Upd: AniDB code cleanup, ported from nZEDb 2016-06-07 DariusIII * Upd: Make Charisma theme dark, fix some bootstrap issues. * Upd: Update composer.lock file diff --git a/build/nntmux.xml b/build/nntmux.xml index bbd3b3290..87f69d90e 100755 --- a/build/nntmux.xml +++ b/build/nntmux.xml @@ -8,7 +8,7 @@ 9 - 5275 + 5276 0000-00-00 diff --git a/newznab/AniDB.php b/newznab/AniDB.php index 637fa70a8..c5ebe4052 100755 --- a/newznab/AniDB.php +++ b/newznab/AniDB.php @@ -44,10 +44,10 @@ class AniDB // FIXME fix the missing variables for this query $this->pdo->queryExec( sprintf(' - UPDATE anidb_titles AS at INNER JOIN anidb_info ai USING (anidbid) SET title = %s, type = %s, startdate = %s, enddate = %s, - related = %s, similar = %s, creators = %s, description = %s, rating = %s, - categories = %s, characters = %s, epnos = %s, airdates = %s, - episodetitles = %s, unixtime = %d WHERE anidbid = %d', + UPDATE anidb_titles AS at INNER JOIN anidb_info ai USING (anidbid) SET title = %s, type = %s, startdate = %s, enddate = %s, + related = %s, similar = %s, creators = %s, description = %s, rating = %s, + categories = %s, characters = %s, epnos = %s, airdates = %s, + episodetitles = %s, unixtime = %d WHERE anidbid = %d', $this->pdo->escapeString($title), $this->pdo->escapeString($type), $this->pdo->escapeString($startdate), @@ -77,11 +77,11 @@ class AniDB { $this->pdo->queryExec( sprintf(' - DELETE at, ai, ae - FROM anidb_titles AS at - LEFT OUTER JOIN anidb_info ai USING (anidbid) - LEFT OUTER JOIN anidb_episodes ae USING (anidbid) - WHERE anidbid = %d', + DELETE at, ai, ae + FROM anidb_titles AS at + LEFT OUTER JOIN anidb_info ai USING (anidbid) + LEFT OUTER JOIN anidb_episodes ae USING (anidbid) + WHERE anidbid = %d', $anidbID ) ); @@ -96,31 +96,33 @@ class AniDB */ public function getAnimeList($letter = '', $animetitle = '') { - $regex = 'REGEXP'; - $rsql = ''; + $rsql = $tsql = ''; if ($letter != '') { if ($letter == '0-9') { $letter = '[0-9]'; } - $rsql .= sprintf('AND at.title %s %s', $regex, $this->pdo->escapeString('^' . $letter)); + $rsql .= sprintf('AND at.title REGEXP %s', $this->pdo->escapeString('^' . $letter)); } - $tsql = ''; if ($animetitle != '') { $tsql .= sprintf('AND at.title %s', $this->pdo->likeString($animetitle, true, true)); } return $this->pdo->queryDirect( - sprintf('SELECT at.anidbid, at.title, ai.type, ai.categories, ai.rating, ai.startdate, ai.enddate - FROM anidb_titles at - LEFT JOIN anidb_info ai USING (anidbid) - INNER JOIN releases r ON at.anidbid = r.anidbid - WHERE at.anidbid > 0 %s %s - GROUP BY at.anidbid - ORDER BY at.title ASC', + sprintf(' + SELECT at.anidbid, at.title, + ai.type, ai.categories, ai.rating, ai.startdate, ai.enddate + FROM anidb_titles at + LEFT JOIN anidb_info ai USING (anidbid) + STRAIGHT_JOIN releases r ON at.anidbid = r.anidbid + WHERE at.anidbid > 0 %s %s + AND r.categories_id = %d + GROUP BY at.anidbid + ORDER BY at.title ASC', $rsql, - $tsql + $tsql, + Category::TV_ANIME ) ); } @@ -147,12 +149,15 @@ class AniDB } return $this->pdo->query( - sprintf('SELECT at.anidbid, GROUP_CONCAT(at.title SEPARATOR ", ") AS title, ai.description - FROM anidb_titles AS at LEFT JOIN anidb_info AS ai USING (anidbid) - WHERE 1=1 %s - AND at.lang = "en" - GROUP BY at.anidbid - ORDER BY at.anidbid ASC %s', + sprintf(" + SELECT at.anidbid, GROUP_CONCAT(at.title SEPARATOR ', ') AS title, + ai.description + FROM anidb_titles AS at + LEFT JOIN anidb_info AS ai USING (anidbid) + WHERE 1=1 %s + AND at.lang = 'en' + GROUP BY at.anidbid + ORDER BY at.anidbid ASC %s", $rsql, $limit ) @@ -173,8 +178,10 @@ class AniDB } $res = $this->pdo->queryOneRow( - sprintf('SELECT COUNT(DISTINCT at.anidbid) AS num - FROM anidb_titles AS at LEFT JOIN anidb_info AS ai USING (anidbid) + sprintf(' + SELECT COUNT(DISTINCT at.anidbid) AS num + FROM anidb_titles AS at + LEFT JOIN anidb_info AS ai USING (anidbid) WHERE 1=1 %s', $rsql @@ -193,14 +200,17 @@ class AniDB public function getAnimeInfo($anidbID) { $animeInfo = $this->pdo->query( - sprintf('SELECT at.anidbid, at.lang, at.title, - ai.startdate, ai.enddate, ai.updated, ai.related, ai.creators, ai.description, - ai.rating, ai.picture, ai.categories, ai.characters, ai.type, ai.similar - FROM anidb_titles AS at LEFT JOIN anidb_info ai USING (anidbid) + sprintf(' + SELECT at.anidbid, at.lang, at.title, + ai.startdate, ai.enddate, ai.updated, ai.related, ai.creators, ai.description, + ai.rating, ai.picture, ai.categories, ai.characters, ai.type, ai.similar + FROM anidb_titles AS at + LEFT JOIN anidb_info AS ai USING (anidbid) WHERE at.anidbid = %d', $anidbID ) ); + return isset($animeInfo[0]) ? $animeInfo[0] : false; } diff --git a/newznab/db/populate/AniDB.php b/newznab/db/populate/AniDB.php index f3b7e2338..036848913 100644 --- a/newznab/db/populate/AniDB.php +++ b/newznab/db/populate/AniDB.php @@ -4,6 +4,7 @@ namespace newznab\db\populate; use newznab\ReleaseImage; use newznab\db\Settings; +use newznab\utility\Utility; class AniDB { @@ -33,7 +34,7 @@ class AniDB private $anidbId; /** - * The name of the NNTmux client for AniDB lookups + * The name of the nZEDb client for AniDB lookups * @var string */ private $apiKey; @@ -70,7 +71,6 @@ class AniDB $this->echooutput = ($options['Echo'] && NN_ECHOCLI); $this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); - // $maxanidbprocessed = $this->pdo->getSetting('maxanidbprocessed'); $anidbupdint = $this->pdo->getSetting('intanidbupdate'); $lastupdated = $this->pdo->getSetting('lastanidbupdate'); @@ -97,6 +97,7 @@ class AniDB case 'info': $this->anidbId = $anidbId; $this->populateInfoTable(); + break; } } @@ -114,12 +115,12 @@ class AniDB { return $this->pdo->queryOneRow( sprintf(' - SELECT anidbid - FROM anidb_titles - WHERE anidbid = %d - AND type = %s - AND lang = %s - AND title = %s', + SELECT anidbid + FROM anidb_titles + WHERE anidbid = %d + AND type = %s + AND lang = %s + AND title = %s', $id, $this->pdo->escapeString($type), $this->pdo->escapeString($lang), @@ -135,7 +136,7 @@ class AniDB */ private function getAniDbAPI() { - $timestamp = $this->pdo->getSetting('banned') + 90000; + $timestamp = $this->pdo->getSetting('APIs.AniDB.banned') + 90000; if ($timestamp > time()) { echo "Banned from AniDB lookups until " . date('Y-m-d H:i:s', $timestamp) . "\n"; return false; @@ -148,18 +149,15 @@ class AniDB echo "AniDB: Error getting response." . PHP_EOL; } elseif (preg_match("/\Banned\<\/error\>/", $apiresponse)) { $this->banned = true; - $this->pdo->setSetting(['banned' => time()]); + $this->pdo->setSetting(['APIs.AniDB.banned' => time()]); } elseif (preg_match("/\Anime not found\<\/error\>/", $apiresponse)) { echo "AniDB : Anime not yet on site. Remove until next update.\n"; } elseif ($AniDBAPIXML = new \SimpleXMLElement($apiresponse)) { + $AniDBAPIArray['similar'] = $this->processAPIResponceElement($AniDBAPIXML->similaranime, 'anime'); - $AniDBAPIArray['related'] = $this->processAPIResponceElement($AniDBAPIXML->relatedanime, 'anime'); - $AniDBAPIArray['creators'] = $this->processAPIResponceElement($AniDBAPIXML->creators); - $AniDBAPIArray['characters'] = $this->processAPIResponceElement($AniDBAPIXML->characters); - $AniDBAPIArray['categories'] = $this->processAPIResponceElement($AniDBAPIXML->categories); $episodeArray = []; @@ -170,7 +168,7 @@ class AniDB $episodeArray[$i]['episode_id'] = (int)$episode->attributes()->id[0]; $episodeArray[$i]['episode_no'] = (int)$episode->epno; - $episodeArray[$i]['airdate'] = (string)$episode->airdate; + $episodeArray[$i]['airdate'] = (string)$episode->airdate; if ($AniDBAPIXML->title && $AniDBAPIXML->title[0]->attributes()) { foreach ($AniDBAPIXML->title->children() as $title) { @@ -189,13 +187,15 @@ class AniDB //start and end date come from AniDB API as date strings -- no manipulation needed $AniDBAPIArray['startdate'] = isset($AniDBAPIXML->startdate) ? $AniDBAPIXML->startdate : '0000-00-00'; - $AniDBAPIArray['enddate'] = isset($AniDBAPIXML->enddate) ? $AniDBAPIXML->enddate : '0000-00-00'; + $AniDBAPIArray['enddate'] = isset($AniDBAPIXML->enddate) ? $AniDBAPIXML->enddate : '0000-00-00'; if (isset($AniDBAPIXML->ratings->permanent)) { $AniDBAPIArray['rating'] = $AniDBAPIXML->ratings->permanent; } else { - $AniDBAPIArray['rating'] = isset($AniDBAPIXML->ratings->temporary) ? - $AniDBAPIXML->ratings->temporary : $AniDBAPIArray['rating'] = ''; + $AniDBAPIArray['rating'] = (isset($AniDBAPIXML->ratings->temporary) + ? $AniDBAPIXML->ratings->temporary + : $AniDBAPIArray['rating'] = '' + ); } $AniDBAPIArray += [ @@ -207,6 +207,7 @@ class AniDB return $AniDBAPIArray; } + return false; } @@ -219,12 +220,13 @@ class AniDB private function processAPIResponceElement(\SimpleXMLElement $element, $property = null) { $property = empty($property) ? 'name' : $property; - $temp = ''; + $temp = ''; if ($element && $element[0]->attributes()) { foreach ($element->children() as $entry) { $temp .= (string)$entry->$property . ', '; } } + return (empty($temp) ? '' : substr($temp, 0, -2)); } @@ -235,26 +237,19 @@ class AniDB */ private function getAniDbResponse() { - $curlString = sprintf( + $urlString = sprintf( 'http://api.anidb.net:9001/httpapi?request=anime&client=%s&clientver=%d&protover=1&aid=%d', $this->apiKey, self::CLIENT_VERSION, $this->anidbId ); - $ch = curl_init($curlString); - - $curlOpts = [ - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_HEADER => 0, - CURLOPT_FAILONERROR => 1, - CURLOPT_ENCODING => 'gzip' - ]; - - curl_setopt_array($ch, $curlOpts); - $apiresponse = curl_exec($ch); - curl_close($ch); - return $apiresponse; + return Utility::getUrl( + [ + 'url' => $urlString, + 'method' => 'get' + ] + ); } /** @@ -272,15 +267,15 @@ class AniDB if ($check === false) { $this->pdo->queryInsert( sprintf(' - INSERT IGNORE INTO anidb_titles - (anidbid, type, lang, title) - VALUES - (%d, %s, %s, %s)', + INSERT IGNORE INTO anidb_titles + (anidbid, type, lang, title) + VALUES (%d, %s, %s, %s)', $id, $this->pdo->escapeString($type), $this->pdo->escapeString($lang), $this->pdo->escapeString($title) - )); + ) + ); } else { echo $this->pdo->log->warning("Duplicate: $id"); } @@ -297,9 +292,12 @@ class AniDB { $this->pdo->queryInsert( sprintf(' - INSERT INTO anidb_info (anidbid, type, startdate, enddate, related, similar, creators, description, rating, picture, categories, characters, updated) - VALUES - (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())', + INSERT INTO anidb_info + ( + anidbid, type, startdate, enddate, related, similar, creators, + description, rating, picture, categories, characters, updated + ) + VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())', $this->anidbId, $this->pdo->escapeString($AniDBInfoArray['type']), $this->pdo->escapeString($AniDBInfoArray['startdate']), @@ -314,10 +312,10 @@ class AniDB $this->pdo->escapeString($AniDBInfoArray['characters']) ) ); - if (isset($AniDBInfoArray['epsarr'])) { $this->insertAniDBEpisodes($AniDBInfoArray['epsarr']); } + return $AniDBInfoArray['picture']; } @@ -332,8 +330,9 @@ class AniDB foreach ($episodeArr as $episode) { $this->pdo->queryInsert( sprintf(' - INSERT IGNORE INTO anidb_episodes (anidbid, episodeid, episode_no, episode_title, airdate) - VALUES (%d, %d, %d, %s, %s)', + INSERT IGNORE INTO anidb_episodes + (anidbid, episodeid, episode_no, episode_title, airdate) + VALUES (%d, %d, %d, %s, %s)', $this->anidbId, $episode['episode_id'], $episode['episode_no'], @@ -357,22 +356,16 @@ class AniDB } $animetitles = new \SimpleXMLElement("compress.zlib://http://anidb.net/api/anime-titles.xml.gz", null, true); - /* - $lines = gzfile(realpath(NN_ROOT . '..' . DS . 'anime-titles.xml.gz')); - $file = implode('', $lines); - $animetitles = new \SimpleXMLElement($file, 0, false); - */ //Even if the update process fails, //we must mark the last update time or risk ban $this->setLastUpdated(); if ($animetitles instanceof \Traversable) { - $count = count($animetitles); + $count = $animetitles->count(); if ($this->echooutput) { echo $this->pdo->log->header( - "Total of " . number_format($count) . - " titles to add." . PHP_EOL + "Total of " . number_format($count) . " titles to add." . PHP_EOL ); } @@ -380,16 +373,21 @@ class AniDB echo "Remaining: $count \r"; foreach ($anime->title as $title) { $xmlAttribs = $title->attributes('xml', true); - $this->insertAniDb((string)$anime['aid'], + $this->insertAniDb( + (string)$anime['aid'], (string)$title['type'], (string)$xmlAttribs->lang, - (string)$title[0]); + (string)$title[0] + ); $this->pdo->log->primary( - "Inserting: %d, %s, %s, %s", - $anime['aid'], - $title['type'], - $xmlAttribs->lang, - $title[0]); + sprintf( + "Inserting: %d, %s, %s, %s", + $anime['aid'], + $title['type'], + $xmlAttribs->lang, + $title[0] + ) + ); } $count--; } @@ -414,17 +412,29 @@ class AniDB $AniDBAPIArray = $this->getAniDbAPI(); if ($this->banned === true) { - $this->pdo->log->doEcho($this->pdo->log->error("AniDB Banned, import will fail, please wait 24 hours before retrying."), - true); + $this->pdo->log->doEcho( + $this->pdo->log->error( + "AniDB Banned, import will fail, please wait 24 hours before retrying." + ), + true + ); exit; } elseif ($AniDBAPIArray === false && $this->echooutput) { - $this->pdo->log->doEcho($this->pdo->log->info("Anime ID: {$this->anidbId} not available for update yet."), - true); + $this->pdo->log->doEcho( + $this->pdo->log->info( + "Anime ID: {$this->anidbId} not available for update yet." + ), + true + ); } else { $this->updateAniChildTables($AniDBAPIArray); if (NN_DEBUG) { - $this->pdo->log->doEcho($this->pdo->log->headerOver("Added/Updated AniDB ID: {$this->anidbId}"), - true); + $this->pdo->log->doEcho( + $this->pdo->log->headerOver( + "Added/Updated AniDB ID: {$this->anidbId}" + ), + true + ); } } } @@ -434,7 +444,7 @@ class AniDB */ private function setLastUpdated() { - $this->pdo->setSetting(['lastanidbupdate' => time()]); + $this->pdo->setSetting(['APIs.anidb.last_full_update' => time()]); } /** @@ -448,12 +458,12 @@ class AniDB { $this->pdo->queryExec( sprintf(' - UPDATE anidb_info - SET type = %s, startdate = %s, enddate = %s, related = %s, - similar = %s, creators = %s, description = %s, - rating = %s, picture = %s, categories = %s, characters = %s, - updated = NOW() - WHERE anidbid = %d', + UPDATE anidb_info + SET type = %s, startdate = %s, enddate = %s, related = %s, + similar = %s, creators = %s, description = %s, + rating = %s, picture = %s, categories = %s, characters = %s, + updated = NOW() + WHERE anidbid = %d', $this->pdo->escapeString($AniDBInfoArray['type']), $this->pdo->escapeString($AniDBInfoArray['startdate']), $this->pdo->escapeString($AniDBInfoArray['enddate']), @@ -468,8 +478,8 @@ class AniDB $this->anidbId ) ); - $this->insertAniDBEpisodes($AniDBInfoArray['epsarr']); + return $AniDBInfoArray['picture']; } @@ -482,9 +492,9 @@ class AniDB { $check = $this->pdo->queryOneRow( sprintf(' - SELECT ai.anidbid AS info - FROM anidb_info ai - WHERE ai.anidbid = %d', + SELECT ai.anidbid AS info + FROM anidb_info ai + WHERE ai.anidbid = %d', $this->anidbId ) ); diff --git a/newznab/db/populate/PopulateTitles.php b/newznab/db/populate/PopulateTitles.php new file mode 100644 index 000000000..7e795f975 --- /dev/null +++ b/newznab/db/populate/PopulateTitles.php @@ -0,0 +1,145 @@ +. + * @author niel + * @copyright 2014 nZEDb + */ +namespace newznab\db\populate; + +use newznab\db\Settings; +use newznab\utility\Utility; + +class PopulateTitles +{ + /** + * @var \newznab\db\Settings + */ + public $pdo; + + /** + * @var \SimpleXMLElement + */ + protected $dataXML; + + /** + * @var \PDOStatement + */ + protected $checkForDuplicate; + + /** + * @var \PDOStatement + */ + protected $insertEntry; + + protected $mainTable; + + /** + * URL of source data. + * + * @var string + */ + protected $sourceURL; + + protected $tempTable; + + public function __constructor($options) + { + $defaults = [ + 'pdo' => null, + ]; + $options += $defaults; + + $this->sourceURL = $options['data-source-url']; + $this->mainTable = $options['main-table']; + $this->tempTable = $options['main-table'] . '_tmp'; + + if (isset($options['pdo']) && $options['pdo'] instanceof Settings) { + $this->pdo = $options['pdo']; + } + } + + protected function checkForDuplicate(array $parameters) + { + if ($this->checkForDuplicate instanceof \PDOStatement) { + $this->checkForDuplicate->execute($parameters); + return $this->checkForDuplicate->fetchAll(); + } else { + throw new \RuntimeException("Duplicate check query not yet prepared!"); + } + } + + protected function createTempTable() + { + // Clear the old temporary table. + $sql = "DROP TABLE IF EXISTS {$this->tempTable}"; + $result = $this->pdo()->exec($sql); + + if ($result !== false) { + $sql = "CREATE TABLE {$this->tempTable} LIKE {$this->mainTable}"; + $result = $this->pdo()->exec($sql); + } + return $result; + } + + protected function insertEntry(array $parameters) + { + if ($this->insertEntry instanceof \PDOStatement) { + $this->insertEntry->execute($parameters); + return $this->insertEntry->fetchAll(); + } else { + throw new \RuntimeException("Insertion query not yet prepared!"); + } + } + + protected function loadXMLFromFile($file) + { + $useErrors = libxml_use_internal_errors(true); + $this->dataXML = simplexml_load_file($file); + + if (!$this->dataXML) { + foreach (libxml_get_errors() as $error) { + echo "\t", $error->message; + } + } + + libxml_use_internal_errors($useErrors); + return ($this->dataXML !== false); + } + + protected function pdo() + { + if ($this->pdo === null) { + $this->pdo = new Settings(); + } + return $this->pdo; + } + + /** + * @param string $pathname full path to file for saving. Will be overwritten. + * + * @return bool|int + */ + protected function saveSourceFile($pathname) + { + $result = false; + $file = Utility::getUrl(['url' => $this->sourceURL]); + if ($file !== false) { + $result = file_put_contents($pathname, $file); + } + return $result; + } +} diff --git a/newznab/processing/post/AniDB.php b/newznab/processing/post/AniDB.php index 51ea9c0d1..9653f1585 100755 --- a/newznab/processing/post/AniDB.php +++ b/newznab/processing/post/AniDB.php @@ -50,9 +50,9 @@ class AniDB $options += $defaults; $this->echooutput = ($options['Echo'] && NN_ECHOCLI); - $this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); + $this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); - $qty = $this->pdo->getSetting('maxanidbprocessed'); + $qty = $this->pdo->getSetting('maxanidbprocessed'); $this->aniqty = !empty($qty) ? $qty : 100; $this->status = 'NULL'; @@ -64,40 +64,42 @@ class AniDB public function processAnimeReleases() { $results = $this->pdo->queryDirect( - sprintf(' - SELECT searchname, id - FROM releases - WHERE nzbstatus = %d - AND anidbid IS NULL - AND categories_id = %d - ORDER BY postdate DESC - LIMIT %d', - NZB::NZB_ADDED, - Category::TV_ANIME, - $this->aniqty - ) + sprintf(' + SELECT searchname, id + FROM releases + WHERE nzbstatus = %d + AND anidbid IS NULL + AND categories_id = %d + ORDER BY postdate DESC + LIMIT %d', + NZB::NZB_ADDED, + Category::TV_ANIME, + $this->aniqty + ) ); if ($results instanceof \Traversable) { $this->doRandomSleep(); - $this->padb = new \newznab\db\populate\AniDB([ - 'Echo' => $this->echooutput, - 'Settings' => $this->pdo - ]); + $this->padb = new \newznab\db\populate\AniDB( + [ + 'Echo' => $this->echooutput, + 'Settings' => $this->pdo + ] + ); foreach ($results as $release) { $matched = $this->matchAnimeRelease($release); if ($matched === false) { $this->pdo->queryExec( - sprintf(' - UPDATE releases - SET anidbid = %d - WHERE id = %d', - $this->status, - $release['id'] - ) + sprintf(' + UPDATE releases + SET anidbid = %d + WHERE id = %d', + $this->status, + $release['id'] + ) ); } } @@ -117,15 +119,15 @@ class AniDB private function checkAniDBInfo($anidbId, $episode = -1) { return $this->pdo->queryOneRow( - sprintf(' - SELECT ae.anidbid, ae.episode_no, - ae.airdate, ae.episode_title - FROM anidb_episodes ae - WHERE ae.anidbid = %d - AND ae.episode_no = %d', - $anidbId, - $episode - ) + sprintf(' + SELECT ae.anidbid, ae.episode_no, + ae.airdate, ae.episode_title + FROM anidb_episodes ae + WHERE ae.anidbid = %d + AND ae.episode_no = %d', + $anidbId, + $episode + ) ); } @@ -146,22 +148,27 @@ class AniDB */ private function extractTitleEpisode($cleanName = '') { - if (preg_match('/(^|.*\")(\[[a-zA-Z\.\!?-]+\][\s_]*)?(\[BD\][\s_]*)?(\[\d{3,4}[ip]\][\s_]*)?(?P[\w\s_.+!?\'-\(\)]+)(New Edit|(Blu-?ray)?( ?Box)?( ?Set)?)?([ _]-[ _]|([ ._-]Epi?(sode)?[ ._-]?0?)?[ ._-]?|[ ._-]Vol\.|[ ._-]E)(?P<epno>\d{1,3}|Movie|O[VA]{2}|Complete Series)(v\d|-\d+)?[-_. ].*[\[\(\"]/i', - $cleanName, - $matches) + $cleanName = str_replace('_', ' ', $cleanName); + + if (preg_match('/(^|.*\")(\[[a-zA-Z\.\!?-]+\][\s_]*)?(\[BD\][\s_]*)?(\[\d{3,4}[ip]\][\s_]*)?(?P<title>[\w\s_.+!?\'-\(\)]+)(New Edit|(Blu-?ray)?( ?Box)?( ?Set)?)?([ _]-[ _]|([ ._-]Epi?(sode)?[ ._-]?0?)?[ ._-]?|[ ._-]Vol\.|[ ._-]E)(?P<epno>\d{1,3}|Movie|OVA|Complete Series)(v\d|-\d+)?[-_. ].*[\[\(\"]/i', + $cleanName, + $matches) ) { $matches['epno'] = (int)$matches['epno']; if (in_array($matches['epno'], ['Movie', 'OVA'])) { - $matches['epno'] = (int)1; + $matches['epno'] = 1; } } else if (preg_match('/^(\[[a-zA-Z\.\-!?]+\][\s_]*)?(\[BD\])?(\[\d{3,4}[ip]\])?(?P<title>[\w\s_.+!?\'-\(\)]+)(New Edit|(Blu-?ray)?( ?Box)?( ?Set)?)?\s*[\(\[](BD|\d{3,4}[ipx])/i', - $cleanName, - $matches) + $cleanName, + $matches) ) { - $matches['epno'] = (int)1; + $matches['epno'] = 1; } else { if (NN_DEBUG) { - $this->pdo->log->doEcho(PHP_EOL . "Could not parse searchname {$cleanName}.", true); + $this->pdo->log->doEcho( + PHP_EOL . "Could not parse searchname {$cleanName}.", + true + ); } $this->status = self::PROC_EXTFAIL; } @@ -183,12 +190,12 @@ class AniDB private function getAnidbByName($searchName = '') { return $this->pdo->queryOneRow( - sprintf(" - SELECT at.anidbid, at.title - FROM anidb_titles AS at - WHERE at.title %s", - $this->pdo->likeString($searchName, true, true) - ) + sprintf(" + SELECT at.anidbid, at.title + FROM anidb_titles AS at + WHERE at.title %s", + $this->pdo->likeString($searchName, true, true) + ) ); } @@ -211,8 +218,8 @@ class AniDB if (is_array($cleanArr) && isset($cleanArr['title']) && is_numeric($cleanArr['epno'])) { echo $this->pdo->log->header(PHP_EOL . "Looking Up: ") . - $this->pdo->log->primary(" Title: {$cleanArr['title']}" . PHP_EOL . - " Episode: {$cleanArr['epno']}"); + $this->pdo->log->primary(" Title: {$cleanArr['title']}" . PHP_EOL . + " Episode: {$cleanArr['epno']}"); // get anidb number for the title of the name $anidbId = $this->getAnidbByName($cleanArr['title']); @@ -231,37 +238,39 @@ class AniDB $this->padb->populateTable('info', $anidbId['anidbid']); $this->doRandomSleep(); $updatedAni = $this->checkAniDBInfo($anidbId['anidbid']); - $type = 'Remote'; + $type = 'Remote'; } else { echo PHP_EOL . - $this->pdo->log->info("This AniDB ID was not found to be accurate locally, but has been updated too recently to check AniDB.") . - PHP_EOL; + $this->pdo->log->info("This AniDB ID was not found to be accurate locally, but has been updated too recently to check AniDB.") . + PHP_EOL; } } $this->updateRelease($anidbId['anidbid'], $release['id']); $this->pdo->log->doEcho( - $this->pdo->log->headerOver("Matched {$type} AniDB ID: ") . - $this->pdo->log->primary($anidbId['anidbid']) . - $this->pdo->log->alternateOver(" Title: ") . - $this->pdo->log->primary($anidbId['title']) . - $this->pdo->log->alternateOver(" Episode #: ") . - $this->pdo->log->primary($cleanArr['epno']) . - $this->pdo->log->alternateOver(" Episode Title: ") . - $this->pdo->log->primary($updatedAni['episode_title']) + $this->pdo->log->headerOver("Matched {$type} AniDB ID: ") . + $this->pdo->log->primary($anidbId['anidbid']) . + $this->pdo->log->alternateOver(" Title: ") . + $this->pdo->log->primary($anidbId['title']) . + $this->pdo->log->alternateOver(" Episode #: ") . + $this->pdo->log->primary($cleanArr['epno']) . + $this->pdo->log->alternateOver(" Episode Title: ") . + $this->pdo->log->primary($updatedAni['episode_title']) ); $matched = true; } else { if (NN_DEBUG) { - $this->pdo->log->doEcho(PHP_EOL . - "Could not match searchname: {$release['searchname']}.", - true); + $this->pdo->log->doEcho( + PHP_EOL . "Could not match searchname: {$release['searchname']}.", + true + ); } $this->status = self::PROC_NOMATCH; } } + return $matched; } @@ -272,13 +281,13 @@ class AniDB private function updateRelease($anidbId, $relId) { $this->pdo->queryExec( - sprintf(" - UPDATE releases - SET anidbid = %d - WHERE id = %d", - $anidbId, - $relId - ) + sprintf(" + UPDATE releases + SET anidbid = %d + WHERE id = %d", + $anidbId, + $relId + ) ); } @@ -292,13 +301,13 @@ class AniDB private function updateTimeCheck($anidbId) { return $this->pdo->queryOneRow( - sprintf(" - SELECT anidbid - FROM anidb_info ai - WHERE DATEDIFF(NOW(), ai.updated) < 7 - AND ai.anidbid = %d", - $anidbId - ) + sprintf(" + SELECT anidbid + FROM anidb_info ai + WHERE ai.updated < (NOW() - INTERVAL 7 DAY) + AND ai.anidbid = %d", + $anidbId + ) ); } }