From a68d794068e10ca32473a43bed7f9dfd724b1a4e Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 20 Feb 2018 14:51:05 +0100 Subject: [PATCH] Add imdbphp/imdbphp package and use it to fetch imdb movie data --- Blacklight/Movie.php | 112 +++++++++++-------------------------------- Changelog | 1 + composer.json | 1 + composer.lock | 38 ++++++++++++++- 4 files changed, 67 insertions(+), 85 deletions(-) diff --git a/Blacklight/Movie.php b/Blacklight/Movie.php index 2f7e7c546..15f71165c 100755 --- a/Blacklight/Movie.php +++ b/Blacklight/Movie.php @@ -18,6 +18,10 @@ use Tmdb\Exception\TmdbApiException; use Blacklight\processing\tv\TraktTv; use Illuminate\Support\Facades\Cache; use GuzzleHttp\Exception\RequestException; +use Imdb\Title; +use Imdb\Config; +use Imdb\TitleSearch; +use Imdb\TitleSearchAdvanced; /** * Class Movie. @@ -170,6 +174,11 @@ class Movie */ public $omdbApi; + /** + * @var \Imdb\Config + */ + private $config; + /** * @param array $options Class instances / Echo to CLI. * @throws \Exception @@ -205,6 +214,8 @@ class Movie } $this->lookuplanguage = Settings::settingValue('indexer.categorise.imdblanguage') !== '' ? (string) Settings::settingValue('indexer.categorise.imdblanguage') : 'en'; + $this->config = new Config(); + $this->config->language = $this->lookuplanguage; $this->imdburl = (int) Settings::settingValue('indexer.categorise.imdburl') !== 0; $this->movieqty = Settings::settingValue('..maximdbprocessed') !== '' ? (int) Settings::settingValue('..maximdbprocessed') : 100; @@ -850,7 +861,7 @@ class Movie } $released = $tmdbLookup['release_date']; if (! empty($released)) { - $ret['year'] = date('Y', strtotime($released)); + $ret['year'] = Carbon::parse($released)->year; } $genresa = $tmdbLookup['genres']; if (! empty($genresa) && \count($genresa) > 0) { @@ -882,92 +893,25 @@ class Movie * @param $imdbId * * @return array|bool - * @throws \Exception */ - protected function fetchIMDBProperties($imdbId) + public function fetchIMDBProperties($imdbId) { - $imdb_regex = [ - 'title' => '/(.*?)\s?\(.*?<\/title>/i', - 'tagline' => '/taglines:<\/h4>\s([^<]+)/i', - 'plot' => '/<p itemprop="description">\s*?(.*?)\s*?<\/p>/i', - 'rating' => '/"ratingValue">([\d.]+)<\/span>/i', - 'year' => '/<title>.*?\(.*?(\d{4}).*?<\/title>/i', - 'cover' => '/<link rel=\'image_src\' href="(http:\/\/ia\.media-imdb\.com.+\.jpg)">/', - ]; + $result = new Title($imdbId, $this->config); + if ($result !== null) { + $ret = [ + 'title' => $result->title(), + 'tagline' => $result->tagline(), + 'plot' => $result->plot_split()[0]['plot'], + 'rating' => $result->rating(), + 'year' => $result->year(), + 'cover' => $result->photo(), + 'genre' => $result->genre(), + 'language' => $result->language(), + 'type' => $result->movietype(), + ]; - $imdb_regex_multi = [ - 'genre' => '/href="\/genre\/(.*?)\?/i', - 'language' => '/<a href="\/language\/.+?\'url\'>(.+?)<\/a>/s', - 'type' => '/<meta property=\'og\:type\' content=\"(.+)\" \/>/i', - ]; - - try { - $buffer = - $this->client->get( - 'http://'.($this->imdburl === false ? 'www' : 'akas').'.imdb.com/title/tt'.$imdbId.'/', - ['headers' => [ - 'Accept-Language' => (Settings::settingValue('indexer.categorise.imdblanguage') !== '') ? Settings::settingValue('indexer.categorise.imdblanguage') : 'en', - 'useragent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) '. - 'Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10', 'foo=bar', - ], - ] - )->getBody()->getContents(); - } catch (RequestException $e) { - if ($e->hasResponse()) { - if ($e->getCode() === 404) { - ColorCLI::doEcho(ColorCLI::notice('Data not available on IMDB server'), true); - } elseif ($e->getCode() === 503) { - ColorCLI::doEcho(ColorCLI::notice('IMDB service unavailable'), true); - } else { - ColorCLI::doEcho(ColorCLI::notice('Unable to fetch data from IMDB, http error reported: '.$e->getCode()), true); - } - } - } catch (\RuntimeException $e) { - ColorCLI::doEcho(ColorCLI::notice('Runtime error: '.$e->getCode()), true); - } - - if (isset($buffer) && $buffer !== false) { - $ret = []; - foreach ($imdb_regex as $field => $regex) { - if (preg_match($regex, $buffer, $matches)) { - $match = $matches[1]; - $match1 = strip_tags(trim(rtrim($match))); - $ret[$field] = $match1; - } - } - - $matches = []; - foreach ($imdb_regex_multi as $field => $regex) { - if (preg_match_all($regex, $buffer, $matches)) { - $match2 = $matches[1]; - $match3 = array_map('trim', $match2); - $ret[$field] = $match3; - } - } - - if ($this->currentTitle !== '' && isset($ret['title'])) { - // Check the similarity. - similar_text($this->currentTitle, $ret['title'], $percent); - if ($percent < self::MATCH_PERCENT) { - return false; - } - } - - // Actors. - if (preg_match('/<table class="cast_list">(.+?)<\/table>/s', $buffer, $hit)) { - if (preg_match_all('/<span class="itemprop" itemprop="name">\s*(.+?)\s*<\/span>/i', $hit[0], $results, PREG_PATTERN_ORDER)) { - $ret['actors'] = $results[1]; - } - } - - // Directors. - if (preg_match('/itemprop="directors?".+?<\/div>/s', $buffer, $hit)) { - if (preg_match_all('/"name">(.*?)<\/span>/is', $hit[0], $results, PREG_PATTERN_ORDER)) { - $ret['director'] = $results[1]; - } - } - if ($this->echooutput && isset($ret['title'])) { - ColorCLI::doEcho(ColorCLI::headerOver('IMDb Found ').ColorCLI::primaryOver($ret['title']), true); + if ($this->echooutput && $result->title() !== null) { + ColorCLI::doEcho(ColorCLI::headerOver('IMDb Found ').ColorCLI::primaryOver($result->title()), true); } return $ret; diff --git a/Changelog b/Changelog index 5df950883..d47c837fd 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-02-20 DariusIII + * Chg: Add imdbphp/imdbphp package and use it to fetch imdb movie data * Chg: Update TMDB class * Chg: Check for imdbid string lenghth in fetchTMDBProperties function * Chg: One more tmdb movie processing change diff --git a/composer.json b/composer.json index 3aff3141a..487e1e2d7 100755 --- a/composer.json +++ b/composer.json @@ -130,6 +130,7 @@ "fxp/composer-asset-plugin": "~1.1", "google/recaptcha": "~1.1", "guzzlehttp/guzzle": "^6.3", + "imdbphp/imdbphp": "^5.2", "intervention/image": "^2.4", "intervention/imagecache": "^2.3", "james-heinrich/getid3": "1.9.*", diff --git a/composer.lock b/composer.lock index 33a40a41a..4e985df6b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "d00f64380b608bd890689322c9808827", + "content-hash": "13972b063b08821a3a3de11eb9792f11", "packages": [ { "name": "adrenth/thetvdb2", @@ -2686,6 +2686,42 @@ ], "time": "2017-03-20T17:10:46+00:00" }, + { + "name": "imdbphp/imdbphp", + "version": "v5.2.5", + "source": { + "type": "git", + "url": "https://github.com/tboothman/imdbphp.git", + "reference": "b9c7a2a3e45d9c33673d6b2829e8fa5424ce4a82" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tboothman/imdbphp/zipball/b9c7a2a3e45d9c33673d6b2829e8fa5424ce4a82", + "reference": "b9c7a2a3e45d9c33673d6b2829e8fa5424ce4a82", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">5.3", + "psr/log": "~1.0" + }, + "require-dev": { + "mockery/mockery": "~0.9", + "phpunit/phpunit": "~3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Imdb\\": "src/Imdb" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "description": "Library for retrieving film and tv information from IMDb", + "time": "2018-02-05T16:42:28+00:00" + }, { "name": "intervention/image", "version": "2.4.1",