From 97cd37db26f4a6d0a31995ba45fd34f9a3f75d54 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Sun, 10 Oct 2021 15:12:54 +0200 Subject: [PATCH] Add tvdb V4 API support --- .env.example | 3 +- Blacklight/processing/tv/TVDB.php | 31 ++++++++--------- misc/testing/Tests/test_tvdb_API.php | 50 ++++++++++++---------------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/.env.example b/.env.example index 8a7c28114..578a4604d 100644 --- a/.env.example +++ b/.env.example @@ -142,7 +142,8 @@ IGDB_CACHE_LIFETIME= TMDB_APIKEY=9a4e16adddcd1e86da19bcaf5ff3c2a3 TMDB_CACHE=false TMDB_LOG=false -TVDB_APIKEY= +TVDB_APIKEY=ef6fb572-bcee-4d99-9b52-90549ad7553a +TVDB_PIN= GIANTBOMB_APIKEY= ANIDB_APIKEY= FANARTTV_APIKEY= diff --git a/Blacklight/processing/tv/TVDB.php b/Blacklight/processing/tv/TVDB.php index 0ad9fc6c8..23c9fcb87 100755 --- a/Blacklight/processing/tv/TVDB.php +++ b/Blacklight/processing/tv/TVDB.php @@ -320,21 +320,14 @@ class TVDB extends TV * @param int $tvDbId * @param int $season * @param int $episode - * @param string $airDate * @param int $videoId * @return array|false */ - protected function getEpisodeInfo($tvDbId, $season, $episode, $airDate = '', $videoId = 0) + protected function getEpisodeInfo($tvDbId, $season, $episode, $videoId = 0) { $return = $response = false; - if ($airDate !== '') { - try { - $response = $this->client->series()->extended($tvDbId); - } catch (ResourceNotFoundException $error) { - return false; - } - } elseif ($videoId > 0) { + if ($videoId > 0) { try { $response = $this->client->series()->allEpisodes($tvDbId); } catch (ResourceNotFoundException $error) { @@ -342,7 +335,11 @@ class TVDB extends TV } } else { try { - $response = $this->client->series()->extended($tvDbId, ['airedSeason' => $season, 'airedEpisodeNumber' => $episode]); + foreach ($this->client->series()->episodes($tvDbId) as $episodeBaseRecord) { + if ($episodeBaseRecord->seasonNumber === $season && $episodeBaseRecord->number === $episode) { + $response = $episodeBaseRecord; + } + } } catch (ResourceNotFoundException $error) { return false; } @@ -382,15 +379,15 @@ class TVDB extends TV } try { - $fanart = $this->client->series()->extended($show->id); - $this->fanartUrl = ! empty($fanart[0]->thumbnail) ? $fanart[0]->thumbnail : ''; + $fanArt = $this->client->series()->extended($show->id); + $this->fanartUrl = ! empty($fanArt[0]->thumbnail) ? $fanArt[0]->thumbnail : ''; } catch (ResourceNotFoundException $e) { $this->colorCli->notice('Fanart image not found on TVDB', true); } try { $imdbId = $this->client->series()->extended($show->id); - preg_match('/tt(?P\d{6,7})$/i', $imdbId->imdbId, $imdb); + preg_match('/tt(?P\d{6,7})$/i', $imdbId->getIMDBId(), $imdb); } catch (ResourceNotFoundException $e) { $this->colorCli->notice('Show ID not found on TVDB', true); } @@ -425,10 +422,10 @@ class TVDB extends TV protected function formatEpisodeInfo($episode): array { return [ - 'title' => (string) $episode->episodeName, - 'series' => (int) $episode->airedSeason, - 'episode' => (int) $episode->airedEpisodeNumber, - 'se_complete' => 'S'.sprintf('%02d', $episode->airedSeason).'E'.sprintf('%02d', $episode->airedEpisodeNumber), + 'title' => (string) $episode->name, + 'series' => (int) $episode->seasonNumber, + 'episode' => (int) $episode->number, + 'se_complete' => 'S'.sprintf('%02d', $episode->seasonNumber).'E'.sprintf('%02d', $episode->number), 'firstaired' => $episode->firstAired, 'summary' => (string) $episode->overview, ]; diff --git a/misc/testing/Tests/test_tvdb_API.php b/misc/testing/Tests/test_tvdb_API.php index 9865d1bf2..85a069f0e 100755 --- a/misc/testing/Tests/test_tvdb_API.php +++ b/misc/testing/Tests/test_tvdb_API.php @@ -2,66 +2,61 @@ require_once dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'bootstrap/autoload.php'; -use Adrenth\Thetvdb\Exception\InvalidArgumentException; -use Adrenth\Thetvdb\Exception\InvalidJsonInResponseException; -use Adrenth\Thetvdb\Exception\RequestFailedException; -use Adrenth\Thetvdb\Exception\UnauthorizedException; use Blacklight\processing\tv\TVDB; $c = new Blacklight\ColorCLI(); -$tvdb = new TVDB(); +$tvDB = new TVDB(); -if (isset($argv[1]) && ! empty($argv[1]) && isset($argv[2]) && is_numeric($argv[2]) && isset($argv[3]) && is_numeric($argv[3])) { +if (! empty($argv[1]) && isset($argv[2], $argv[3]) && is_numeric($argv[2]) && is_numeric($argv[3])) { // Test if your TvDB API key and configuration are working // If it works you should get a var dumped array of the show/season/episode entered $season = (int) $argv[2]; $episode = (int) $argv[3]; - $day = (isset($argv[4]) && is_numeric($argv[4]) ? $argv[4] : ''); // Search for a show - $series = $tvdb->client->search()->seriesByName((string) $argv[1]); + $series = $tvDB->client->search()->search((string) $argv[1], ['type' => 'series']); // Use the first show found (highest match) and get the requested season/episode from $argv if ($series) { - $serie = $series->getData(); - print_r($serie); + print_r($series[0]); + $initialEpisodeObj = ''; - if ($season > 0 && $episode > 0 && $day === '') { + if ($season > 0 && $episode > 0) { try { - $episodeObj = $tvdb->client->series()->getEpisodesWithQuery($serie[0]->getid(), ['airedSeason' => $season, 'airedEpisode' => $episode]); + $initialEpisodeObj = $tvDB->client->series()->episodes($series[0]->tvdb_id); } catch (InvalidArgumentException $error) { echo 'Invalid argument(s) used'.PHP_EOL; return false; - } catch (InvalidJsonInResponseException $error) { - if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) { + } catch (\CanIHaveSomeCoffee\TheTVDbAPI\Exception\ParseException $error) { + if (str_starts_with($error->getMessage(), 'Could not decode JSON data') || str_starts_with($error->getMessage(), 'Incorrect data structure')) { return false; } - } catch (RequestFailedException $error) { + } catch (\CanIHaveSomeCoffee\TheTVDbAPI\Exception\ResourceNotFoundException $error) { return false; - } catch (UnauthorizedException $error) { - if (strpos($error->getMessage(), 'Unauthorized') === 0) { + } catch (\CanIHaveSomeCoffee\TheTVDbAPI\Exception\UnauthorizedException $error) { + if (str_starts_with($error->getMessage(), 'Unauthorized')) { return false; } } - if ($episodeObj) { + if ($initialEpisodeObj) { + foreach ($initialEpisodeObj as $episodeBaseRecord) { + if ($episodeBaseRecord->number === $episode && $episodeBaseRecord->seasonNumber === $season) { + $episodeObj = $episodeBaseRecord; + } + } print_r($episodeObj); } } elseif ($season === 0 && $episode === 0) { - $episodeObj = $tvdb->client->series()->getEpisodes($serie[0]->getid()); - if (is_object($episodeObj)) { - foreach ($episodeObj->getData() as $ep) { + $initialEpisodeObj = $tvDB->client->series()->allEpisodes($series[0]->tvdb_id); + if (is_object($initialEpisodeObj)) { + foreach ($initialEpisodeObj as $ep) { print_r($ep); } } - } elseif (preg_match('#^(19|20)\d{2}\/\d{2}\/\d{2}$#', $season.'/'.$episode.'/'.$day, $airdate)) { - $episodeObj = $tvdb->client->series()->getEpisodesWithQuery($series[0]->id, ['firstAired' => (string) $airdate[0]]); - if ($episodeObj) { - print_r($episodeObj); - } } else { exit($c->error('Invalid episode data returned from TVDB API.')); } @@ -69,7 +64,6 @@ if (isset($argv[1]) && ! empty($argv[1]) && isset($argv[2]) && is_numeric($argv[ exit($c->error('Invalid show data returned from TVDB API.')); } } else { - exit($c->error('Invalid arguments. This script requires a text string (show name) followed by a season and episode number.'.PHP_EOL. - 'You can also optionally supply "YYYY" "MM" "DD" arguments instead of season/episode for an airdate lookup.') + exit($c->error('Invalid arguments. This script requires a text string (show name) followed by a season and episode number.') ); }