From b44db312b6991a7baef7e07d9eb37b7fcadeeb02 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 18 Apr 2017 15:50:24 +0200 Subject: [PATCH] Change AniDB support a bit --- cli/data/populate_anidb.php | 6 ++-- nntmux/db/populate/AniDB.php | 60 +++++++++++++++++++++++++++----- nntmux/processing/post/AniDB.php | 8 ++--- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/cli/data/populate_anidb.php b/cli/data/populate_anidb.php index 3dc2b9bf7..e16e3099f 100755 --- a/cli/data/populate_anidb.php +++ b/cli/data/populate_anidb.php @@ -4,6 +4,7 @@ */ require_once dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bootstrap.php'; +use nntmux\ColorCLI; use nntmux\db\DB; use nntmux\db\populate\AniDB; @@ -16,9 +17,10 @@ if ($argc > 1 && $argv[1] === 'true' && isset($argv[2])) { (new AniDB(['Settings' => $pdo, 'Echo' => true]))->populateTable('info'); } } else { - $pdo->log->doEcho(PHP_EOL . $pdo->log->error( + ColorCLI::doEcho(PHP_EOL . ColorCLI::error( 'To execute this script you must provide a boolean argument.' . PHP_EOL . 'Argument1: true|false to run this script or not' . PHP_EOL . - 'Argument2: full|info for what type of data to populate'), true + 'Argument2: full|info for what type of data to populate.' . PHP_EOL . + 'WARNING: Argument "info" will get you banned from AniDB almost instantly'), true ); } diff --git a/nntmux/db/populate/AniDB.php b/nntmux/db/populate/AniDB.php index 173786bf7..6699d5428 100755 --- a/nntmux/db/populate/AniDB.php +++ b/nntmux/db/populate/AniDB.php @@ -2,6 +2,7 @@ namespace nntmux\db\populate; +use GuzzleHttp\Client; use app\models\Settings; use nntmux\ColorCLI; use nntmux\ReleaseImage; @@ -71,6 +72,7 @@ class AniDB $this->echooutput = ($options['Echo'] && NN_ECHOCLI); $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); + $this->guzzleclient = new Client(); $anidbupdint = Settings::value('APIs.AniDB.max_update_frequency'); $lastupdated = Settings::value('APIs.AniDB.last_full_update'); @@ -86,17 +88,17 @@ class AniDB /** * Main switch that initiates AniDB table population * - * @param string $type + * @param string $type + * @param int|string $anidbId */ - public function populateTable($type = '') + public function populateTable($type = '', $anidbId = '') { switch ($type) { case 'full': $this->populateMainTable(); - $this->populateInfoTable(); break; case 'info': - $this->populateInfoTable(); + $this->populateInfoTable($anidbId); break; } } @@ -145,7 +147,7 @@ class AniDB $AniDBAPIArray = []; - if (!$apiresponse) { + if ($apiresponse === false) { echo 'AniDB: Error getting response.' . PHP_EOL; } elseif (preg_match('/\Banned\<\/error\>/', $apiresponse)) { $this->banned = true; @@ -414,11 +416,52 @@ class AniDB /** * Directs flow for populating the AniDB Info/Episodes table + * + * @param string $anidbId */ - private function populateInfoTable() + private function populateInfoTable($anidbId = '') { - $anidbIds = $this->pdo->query(sprintf('SELECT DISTINCT anidbid AS anidbid FROM anidb_titles')); - foreach ($anidbIds as $anidbId) { + if ($anidbId === '') { + $anidbIds = $this->pdo->query(sprintf( + 'SELECT DISTINCT at.anidbid AS anidbid, ai.updated + FROM anidb_titles at + LEFT JOIN anidb_info ai ON ai.anidbid = at.anidbid + WHERE ai.updated IS NULL' + ) + ); + foreach ($anidbIds as $anidbId) { + $AniDBAPIArray = $this->getAniDbAPI($anidbId['anidbid']); + + if ($this->banned === true) { + ColorCLI::doEcho( + ColorCLI::error( + 'AniDB Banned, import will fail, please wait 24 hours before retrying.' + ), + true + ); + exit; + } + + if ($AniDBAPIArray === false && $this->echooutput) { + ColorCLI::doEcho( + ColorCLI::info( + 'Anime ID: ' . $anidbId['anidbid'] . ' not available for update yet.' + ), + true + ); + } else { + $this->updateAniChildTables($AniDBAPIArray, $anidbId); + if (NN_DEBUG) { + ColorCLI::doEcho( + ColorCLI::headerOver( + 'Added/Updated AniDB ID: ' . $anidbId['anidbid'] + ), + true + ); + } + } + } + } else { $AniDBAPIArray = $this->getAniDbAPI($anidbId['anidbid']); if ($this->banned === true) { @@ -506,6 +549,7 @@ class AniDB * Directs flow for updating child AniDB tables * * @param array $AniDBInfoArray + * @param $anidbId */ private function updateAniChildTables($AniDBInfoArray = [], $anidbId) { diff --git a/nntmux/processing/post/AniDB.php b/nntmux/processing/post/AniDB.php index 06363dbd4..88f3f4e87 100755 --- a/nntmux/processing/post/AniDB.php +++ b/nntmux/processing/post/AniDB.php @@ -26,7 +26,7 @@ class AniDB public $padb; /** - * @var \nntmux\db\Settings + * @var DB */ public $pdo; @@ -192,10 +192,10 @@ class AniDB private function getAnidbByName($searchName = '') { return $this->pdo->queryOneRow( - sprintf(" + sprintf(' SELECT at.anidbid, at.title FROM anidb_titles AS at - WHERE at.title %s", + WHERE at.title %s', $this->pdo->likeString($searchName, true, true) ) ); @@ -237,7 +237,7 @@ class AniDB if ($updatedAni === false) { if ($this->updateTimeCheck($anidbId['anidbid']) !== false) { - $this->padb->populateTable('info', $anidbId['anidbid']); + $this->padb->populateTable('info', $anidbId); $this->doRandomSleep(); $updatedAni = $this->checkAniDBInfo($anidbId['anidbid']); $type = 'Remote';