Skip to content

Commit

Permalink
Switch to canihavesomecoffee/thetvdbapi for TheTVDB API searches
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIII committed Sep 12, 2018
1 parent f71f82f commit f91229d
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 154 deletions.
139 changes: 42 additions & 97 deletions Blacklight/processing/tv/TVDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace Blacklight\processing\tv;

use Blacklight\ColorCLI;
use Adrenth\Thetvdb\Client;
use Blacklight\ReleaseImage;
use Adrenth\Thetvdb\Exception\UnauthorizedException;
use Adrenth\Thetvdb\Exception\CouldNotLoginException;
use Adrenth\Thetvdb\Exception\RequestFailedException;
use Adrenth\Thetvdb\Exception\InvalidArgumentException;
use Adrenth\Thetvdb\Exception\InvalidJsonInResponseException;
use CanIHaveSomeCoffee\TheTVDbAPI\Exception\ResourceNotFoundException;
use CanIHaveSomeCoffee\TheTVDbAPI\Exception\UnauthorizedException;
use CanIHaveSomeCoffee\TheTVDbAPI\TheTVDbAPI;

/**
* Class TVDB -- functions used to post process releases against TVDB.
Expand All @@ -21,7 +18,7 @@ class TVDB extends TV
private const MATCH_PROBABILITY = 75;

/**
* @var \Adrenth\Thetvdb\Client
* @var \CanIHaveSomeCoffee\TheTVDbAPI\TheTVDbAPI
*/
public $client;

Expand Down Expand Up @@ -54,19 +51,16 @@ class TVDB extends TV
public function __construct(array $options = [])
{
parent::__construct($options);
$this->client = new Client();
$this->client->setLanguage('en');
$this->client = new TheTVDbAPI();
$this->client->setAcceptedLanguages(['en']);
$this->local = false;

// Check if we can get the time for API status
// If we can't then we set local to true
try {
$this->token = $this->client->authentication()->login(self::TVDB_API_KEY);
} catch (CouldNotLoginException $error) {
echo ColorCLI::warning('Could not reach TVDB API. Running in local mode only!');
$this->local = true;
} catch (UnauthorizedException $error) {
echo ColorCLI::warning('Bad response from TVDB API. Running in local mode only!');
ColorCLI::doEcho(ColorCLI::warning('Could not reach TVDB API. Running in local mode only!'), true);
$this->local = true;
}

Expand Down Expand Up @@ -243,63 +237,44 @@ protected function getBanner($videoID, $siteId): bool
* 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 string $cleanName
*
* @param string $cleanName
* @param string $country
*
* @return array|false
* @return array|bool|false
*/
protected function getShowInfo($cleanName, $country = '')
{
$return = $response = false;
$highestMatch = 0;
try {
$response = $this->client->search()->seriesByName($cleanName);
} catch (InvalidArgumentException $error) {
return false;
} catch (InvalidJsonInResponseException $error) {
if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) {
return false;
}
} catch (RequestFailedException $error) {
return false;
} catch (UnauthorizedException $error) {
if (strpos($error->getMessage(), 'Unauthorized') === 0) {
return false;
}
$response = $this->client->search()->searchByName($cleanName);
} catch (ResourceNotFoundException $e) {
$response = false;
}

if ($response === false && $country !== '') {
try {
$response = $this->client->search()->seriesByName(rtrim(str_replace($country, '', $cleanName)));
} catch (InvalidArgumentException $error) {
return false;
} catch (InvalidJsonInResponseException $error) {
if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) {
return false;
}
} catch (RequestFailedException $error) {
return false;
} catch (UnauthorizedException $error) {
if (strpos($error->getMessage(), 'Unauthorized') === 0) {
return false;
}
$response = $this->client->search()->searchByName(rtrim(str_replace($country, '', $cleanName)));
} catch (ResourceNotFoundException $e) {
$response = false;
ColorCLI::doEcho(ColorCLI::notice('Show not found on TVDB'), true);
}
}

sleep(1);

if (\is_array($response)) {
foreach ($response->getData() as $show) {
foreach ($response as $show) {
if ($this->checkRequiredAttr($show, 'tvdbS')) {
// Check for exact title match first and then terminate if found
if (strtolower($show->getSeriesName()) === strtolower($cleanName)) {
if (strtolower($show->seriesName) === strtolower($cleanName)) {
$highest = $show;
break;
}

// Check each show title for similarity and then find the highest similar value
$matchPercent = $this->checkMatch(strtolower($show->getSeriesName()), strtolower($cleanName), self::MATCH_PROBABILITY);
$matchPercent = $this->checkMatch(strtolower($show->seriesName), strtolower($cleanName), self::MATCH_PROBABILITY);

// If new match has a higher percentage, set as new matched title
if ($matchPercent > $highestMatch) {
Expand All @@ -308,8 +283,8 @@ protected function getShowInfo($cleanName, $country = '')
}

// Check for show aliases and try match those too
if (! empty($show->getAliases())) {
foreach ($show->getAliases() as $key => $name) {
if (! empty($show->aliases)) {
foreach ($show->aliases as $key => $name) {
$matchPercent = $this->checkMatch(strtolower($name), strtolower($cleanName), $matchPercent);
if ($matchPercent > $highestMatch) {
$highestMatch = $matchPercent;
Expand Down Expand Up @@ -372,62 +347,32 @@ protected function getEpisodeInfo($tvDbId, $season, $episode, $airDate = '', $vi

if ($airDate !== '') {
try {
$response = $this->client->series()->getEpisodesWithQuery($tvDbId, ['firstAired' => $airDate]);
} catch (InvalidArgumentException $error) {
return false;
} catch (InvalidJsonInResponseException $error) {
if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) {
return false;
}
} catch (RequestFailedException $error) {
$response = $this->client->series()->getEpisodesWithQuery($tvDbId, ['firstAired' => $airDate])->getData();
} catch (ResourceNotFoundException $error) {
return false;
} catch (UnauthorizedException $error) {
if (strpos($error->getMessage(), 'Unauthorized') === 0) {
return false;
}
}
} elseif ($videoId > 0) {
try {
$response = $this->client->series()->getEpisodes($tvDbId);
} catch (InvalidArgumentException $error) {
$response = $this->client->series()->getEpisodes($tvDbId)->getData();
} catch (ResourceNotFoundException $error) {
return false;
} catch (InvalidJsonInResponseException $error) {
if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) {
return false;
}
} catch (RequestFailedException $error) {
return false;
} catch (UnauthorizedException $error) {
if (strpos($error->getMessage(), 'Unauthorized') === 0) {
return false;
}
}
} else {
try {
$response = $this->client->series()->getEpisodesWithQuery($tvDbId, ['airedSeason' => $season, 'airedEpisode' => $episode]);
} catch (InvalidArgumentException $error) {
$response = $this->client->series()->getEpisodesWithQuery($tvDbId, ['airedSeason' => $season, 'airedEpisode' => $episode])->getData();
} catch (ResourceNotFoundException $error) {
return false;
} catch (InvalidJsonInResponseException $error) {
if (strpos($error->getMessage(), 'Could not decode JSON data') === 0 || strpos($error->getMessage(), 'Incorrect data structure') === 0) {
return false;
}
} catch (RequestFailedException $error) {
return false;
} catch (UnauthorizedException $error) {
if (strpos($error->getMessage(), 'Unauthorized') === 0) {
return false;
}
}
}

sleep(1);

if (\is_object($response->getData())) {
if ($this->checkRequiredAttr($response->getData(), 'tvdbE')) {
if (\is_object($response)) {
if ($this->checkRequiredAttr($response, 'tvdbE')) {
$return = $this->formatEpisodeInfo($response);
}
} elseif ($videoId > 0 && \is_array($response->getData())) {
foreach ($response->getData() as $singleEpisode) {
} elseif ($videoId > 0 && \is_array($response)) {
foreach ($response as $singleEpisode) {
if ($this->checkRequiredAttr($singleEpisode, 'tvdbE')) {
$this->addEpisode($videoId, $this->formatEpisodeInfo($singleEpisode));
}
Expand All @@ -451,18 +396,18 @@ protected function formatShowInfo($show): array

return [
'type' => parent::TYPE_TV,
'title' => (string) $show->getSeriesName(),
'summary' => (string) $show->getOverview(),
'started' => $show->firstAired->format('Y-m-d'),
'publisher' => (string) $show->getNetwork(),
'title' => (string) $show->seriesName,
'summary' => (string) $show->overview,
'started' => $show->firstAired,
'publisher' => (string) $show->network,
'source' => parent::SOURCE_TVDB,
'imdb' => (int) ($imdb['imdbid'] ?? 0),
'tvdb' => (int) $show->getid(),
'tvdb' => (int) $show->id,
'trakt' => 0,
'tvrage' => 0,
'tvmaze' => 0,
'tmdb' => 0,
'aliases' => ! empty($show->getAliases()) ? $show->getAliases() : '',
'aliases' => ! empty($show->aliases) ? $show->aliases : '',
'localzone' => "''",
];
}
Expand All @@ -478,11 +423,11 @@ protected function formatShowInfo($show): array
protected function formatEpisodeInfo($episode): array
{
return [
'title' => (string) $episode->name,
'series' => (int) $episode->season,
'episode' => (int) $episode->number,
'se_complete' => 'S'.sprintf('%02d', $episode->season).'E'.sprintf('%02d', $episode->number),
'firstaired' => $episode->firstAired->format('Y-m-d'),
'title' => (string) $episode->episodeName,
'series' => (int) $episode->airedSeason,
'episode' => (int) $episode->airedEpisodeNumber,
'se_complete' => 'S'.sprintf('%02d', $episode->airedSeason).'E'.sprintf('%02d', $episode->airedEpisodeNumber),
'firstaired' => $episode->firstAired,
'summary' => (string) $episode->overview,
];
}
Expand Down
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2018-09-12 DariusIII
* Chg: Switch to canihavesomecoffee/thetvdbapi for TheTVDB API searches
* Chg: Small cosmetic changes in some of the classes
* Chg: Add genealabs/laravel-caffeine again, update to version 0.7.1
* Chg: Update some of the regexes in NameFixer to prevent bypassing of file extensions (ie. file.jpg.php)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"ext-spl": "*",
"ext-xmlwriter": "*",
"ext-zlib": "*",
"adrenth/thetvdb2": "~2.0",
"aharen/omdbapi": "^2.0",
"anhskohbo/no-captcha": "^3.0",
"b3rs3rk/steamfront": "dev-master",
Expand Down Expand Up @@ -128,6 +127,7 @@
"bower-asset/select2": "~4.0.2",
"bower-asset/slimScroll": "~1.3.7",
"bower-asset/tinymce-dist": "^4.7",
"canihavesomecoffee/thetvdbapi": "^1.0",
"dariusiii/rarinfo": "^2.5",
"dborsatto/php-giantbomb": "dev-master",
"doctrine/dbal": "^2.7",
Expand Down
Loading

0 comments on commit f91229d

Please sign in to comment.