From 396604da78f86e67d0c3244f2ab4a593d2e962f1 Mon Sep 17 00:00:00 2001 From: Fabian Beiner Date: Wed, 25 Apr 2018 15:03:51 +0200 Subject: [PATCH] Released v6.1.0. --- imdb.class.php | 425 +++++++++++++++++-------------------------------- 1 file changed, 142 insertions(+), 283 deletions(-) diff --git a/imdb.class.php b/imdb.class.php index ca66465..bd5ac3b 100644 --- a/imdb.class.php +++ b/imdb.class.php @@ -14,7 +14,7 @@ * @author Fabian Beiner * @license https://opensource.org/licenses/MIT The MIT License * @link https://github.com/FabianBeiner/PHP-IMDB-Grabber/ GitHub Repository - * @version 6.0.8 + * @version 6.1.0 */ class IMDB { @@ -55,6 +55,7 @@ class IMDB const IMDB_LOCATION = '~href="\/search\/title\?locations=(.*)">(.*)<\/a>~Ui'; const IMDB_MPAA = '~
  • (?:\s+)(TV-Y|TV-Y7|TV-G|TV-PG|TV-14|TV-MA|G|PG|PG-13|R|NC-17|NR|UR)(?:\s+)<\/li>~Ui'; const IMDB_NAME = '~href="/name/(.+)/?(?:\?[^"]*)?"[^>]*>(.+)~Ui'; + const IMDB_DESCRIPTION = '~
    \s+
    (.*)
    \s+
    ~Ui'; const IMDB_NOT_FOUND = '~

    No results found for ~Ui'; const IMDB_PLOT = '~]*>\s*Plot\s*Summary\s*\s*\s*

    (.+)

    ~Ui'; const IMDB_PLOT_KEYWORDS = '~]*>Plot\s*Keywords\s*(.+)(?:]*>[^<]*\s*

  • \s*\s*)?~Ui'; @@ -160,8 +161,8 @@ public function __construct($sSearch, $iCache = null, $sSearchFor = 'all') error_reporting(-1); echo '
    Running: fetchUrl("' . $sSearch . '")
    '; } - if (null !== $iCache && (int)$iCache > 0) { - $this->iCache = (int)$iCache; + if (null !== $iCache && (int) $iCache > 0) { + $this->iCache = (int) $iCache; } $this->fetchUrl($sSearch); } @@ -293,184 +294,15 @@ private function fetchUrl($sSearch) */ public function getAll() { - $aData = []; - $aData['Aka'] = [ - 'name' => 'Also Known As', - 'value' => $this->getAka() - ]; - $aData['Akas'] = [ - 'name' => '(all) Also Known As', - 'value' => $this->getAkas() - ]; - $aData['AspectRatio'] = [ - 'name' => 'Aspect Ratio', - 'value' => $this->getAspectRatio() - ]; - $aData['Awards'] = [ - 'name' => 'Awards', - 'value' => $this->getAwards() - ]; - $aData['CastLinked'] = [ - 'name' => 'Cast', - 'value' => $this->getCastAsUrl() - ]; - $aData['Cast'] = [ - 'name' => 'Cast', - 'value' => $this->getCast() - ]; - $aData['CastAndCharacterLinked'] = [ - 'name' => 'Cast and Character', - 'value' => $this->getCastAndCharacterAsUrl() - ]; - $aData['CastAndCharacter'] = [ - 'name' => 'Cast and Character', - 'value' => $this->getCastAndCharacter() - ]; - $aData['Certification'] = [ - 'name' => 'Certification', - 'value' => $this->getCertification() - ]; - $aData['Color'] = [ - 'name' => 'Color', - 'value' => $this->getColor() - ]; - $aData['CompanyLinked'] = [ - 'name' => 'Company', - 'value' => $this->getCompanyAsUrl() - ]; - $aData['Company'] = [ - 'name' => 'Company', - 'value' => $this->getCompany() - ]; - $aData['CountryLinked'] = [ - 'name' => 'Country', - 'value' => $this->getCountryAsUrl() - ]; - $aData['Country'] = [ - 'name' => 'Country', - 'value' => $this->getCountry() - ]; - $aData['CreatorLinked'] = [ - 'name' => 'Creator', - 'value' => $this->getCreatorAsUrl() - ]; - $aData['Creator'] = [ - 'name' => 'Creator', - 'value' => $this->getCreator() - ]; - $aData['DirectorLinked'] = [ - 'name' => 'Director', - 'value' => $this->getDirectorAsUrl() - ]; - $aData['Director'] = [ - 'name' => 'Director', - 'value' => $this->getDirector() - ]; - $aData['GenreLinked'] = [ - 'name' => 'Genre', - 'value' => $this->getGenreAsUrl() - ]; - $aData['Genre'] = [ - 'name' => 'Genre', - 'value' => $this->getGenre() - ]; - $aData['LanguageLinked'] = [ - 'name' => 'Language', - 'value' => $this->getLanguageAsUrl() - ]; - $aData['Language'] = [ - 'name' => 'Language', - 'value' => $this->getLanguage() - ]; - $aData['LocationLinked'] = [ - 'name' => 'Location', - 'value' => $this->getLocationAsUrl() - ]; - $aData['Location'] = [ - 'name' => 'Location', - 'value' => $this->getLocation() - ]; - $aData['MPAA'] = [ - 'name' => 'MPAA', - 'value' => $this->getMpaa() - ]; - $aData['PlotKeywords'] = [ - 'name' => 'Plot Keywords', - 'value' => $this->getPlotKeywords() - ]; - $aData['Plot'] = [ - 'name' => 'Plot', - 'value' => $this->getPlot() - ]; - $aData['Poster'] = [ - 'name' => 'Poster', - 'value' => $this->getPoster('big') - ]; - $aData['Rating'] = [ - 'name' => 'Rating', - 'value' => $this->getRating() - ]; - $aData['ReleaseDate'] = [ - 'name' => 'Release Date', - 'value' => $this->getReleaseDate() - ]; - $aData['IsReleased'] = [ - 'name' => 'Is released', - 'value' => ($this->isReleased() ? 'Yes' : 'No') - ]; - $aData['Runtime'] = [ - 'name' => 'Runtime', - 'value' => $this->getRuntime() - ]; - $aData['SeasonsLinked'] = [ - 'name' => 'Seasons', - 'value' => $this->getSeasonsAsUrl() - ]; - $aData['Seasons'] = [ - 'name' => 'Seasons', - 'value' => $this->getSeasons() - ]; - $aData['SoundMix'] = [ - 'name' => 'Sound Mix', - 'value' => $this->getSoundMix() - ]; - $aData['Tagline'] = [ - 'name' => 'Tagline', - 'value' => $this->getTagline() - ]; - $aData['Title'] = [ - 'name' => 'Title', - 'value' => $this->getTitle() - ]; - $aData['TrailerLinked'] = [ - 'name' => 'Trailer', - 'value' => $this->getTrailerAsUrl() - ]; - $aData['Url'] = [ - 'name' => 'Url', - 'value' => $this->getUrl() - ]; - $aData['UserReview'] = [ - 'name' => 'User Review', - 'value' => $this->getUserReview() - ]; - $aData['Votes'] = [ - 'name' => 'Votes', - 'value' => $this->getVotes() - ]; - $aData['WriterLinked'] = [ - 'name' => 'Writer', - 'value' => $this->getWriterAsUrl() - ]; - $aData['Writer'] = [ - 'name' => 'Writer', - 'value' => $this->getWriter() - ]; - $aData['Year'] = [ - 'name' => 'Year', - 'value' => $this->getYear() - ]; - + $aData = []; + foreach (get_class_methods(__CLASS__) as $method) { + if (substr($method, 0, 3) === 'get' && $method !== 'getAll') { + $aData[$method] = [ + 'name' => ltrim($method, 'get'), + 'value' => $this->{$method}() + ]; + } + } array_multisort($aData); return $aData; @@ -494,7 +326,7 @@ public function getAka() /** * Returns all local names * - * @return string The aka name. + * @return string All local names. */ public function getAkas() { @@ -581,20 +413,6 @@ public function getAwards() return self::$sNotFound; } - /** - * @return string The budget of the movie or $sNotFound. - */ - public function getBudget() { - if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_BUDGET, 1); - if (false !== $sMatch) { - return IMDBHelper::cleanString($sMatch); - } - } - - return self::$sNotFound; - } - /** * @param int $iLimit How many cast members should be returned? * @param bool $bMore Add … if there are more cast members than printed. @@ -605,7 +423,8 @@ public function getBudget() { public function getCastAsUrl($iLimit = 0, $bMore = true, $sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { if (0 !== $iLimit && $i >= $iLimit) { @@ -643,7 +462,8 @@ public function getCastAsUrl($iLimit = 0, $bMore = true, $sTarget = '') public function getCast($iLimit = 0, $bMore = true) { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { if (0 !== $iLimit && $i >= $iLimit) { @@ -681,6 +501,7 @@ public function getCastAndCharacterAsUrl($iLimit = 0, $bMore = true, $sTarget = if (true === $this->isReady) { $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); $aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { if (0 !== $iLimit && $i >= $iLimit) { @@ -722,6 +543,7 @@ public function getCastAndCharacter($iLimit = 0, $bMore = true) if (true === $this->isReady) { $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST); $aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { if (0 !== $iLimit && $i >= $iLimit) { @@ -773,6 +595,21 @@ public function getColor() return self::$sNotFound; } + /** + * @return string The company producing the movie or $sNotFound. + */ + public function getCompany() + { + if (true === $this->isReady) { + $sMatch = $this->getCompanyAsUrl(); + if (self::$sNotFound !== $sMatch) { + return IMDBHelper::cleanString($sMatch); + } + } + + return self::$sNotFound; + } + /** * @param string $sTarget Add a target to the links? * @@ -797,12 +634,12 @@ public function getCompanyAsUrl($sTarget = '') } /** - * @return string The company producing the movie or $sNotFound. + * @return string A list with countries or $sNotFound. */ - public function getCompany() + public function getCountry() { if (true === $this->isReady) { - $sMatch = $this->getCompanyAsUrl(); + $sMatch = $this->getCountryAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -819,7 +656,8 @@ public function getCompany() public function getCountryAsUrl($sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_COUNTRY); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_COUNTRY); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -840,12 +678,12 @@ public function getCountryAsUrl($sTarget = '') } /** - * @return string A list with countries or $sNotFound. + * @return string A list with the creators or $sNotFound. */ - public function getCountry() + public function getCreator() { if (true === $this->isReady) { - $sMatch = $this->getCountryAsUrl(); + $sMatch = $this->getCreatorAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -862,8 +700,9 @@ public function getCountry() public function getCreatorAsUrl($sTarget = '') { if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CREATOR, 1); - $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CREATOR, 1); + $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -884,12 +723,27 @@ public function getCreatorAsUrl($sTarget = '') } /** - * @return string A list with the creators or $sNotFound. + * @return string The description of the movie or $sNotFound. */ - public function getCreator() + public function getDescription() { if (true === $this->isReady) { - $sMatch = $this->getCreatorAsUrl(); + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DESCRIPTION, 1); + if (false !== $sMatch) { + return IMDBHelper::cleanString($sMatch); + } + } + + return self::$sNotFound; + } + + /** + * @return string A list with the directors or $sNotFound. + */ + public function getDirector() + { + if (true === $this->isReady) { + $sMatch = $this->getDirectorAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -906,8 +760,9 @@ public function getCreator() public function getDirectorAsUrl($sTarget = '') { if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DIRECTOR, 1); - $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DIRECTOR, 1); + $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -928,12 +783,12 @@ public function getDirectorAsUrl($sTarget = '') } /** - * @return string A list with the directors or $sNotFound. + * @return string A list with the genres or $sNotFound. */ - public function getDirector() + public function getGenre() { if (true === $this->isReady) { - $sMatch = $this->getDirectorAsUrl(); + $sMatch = $this->getGenreAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -950,7 +805,8 @@ public function getDirector() public function getGenreAsUrl($sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_GENRE); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_GENRE); + $aReturn = []; if (count($aMatch[2])) { foreach (array_unique($aMatch[2]) as $i => $sName) { $aReturn[] = @@ -971,12 +827,12 @@ public function getGenreAsUrl($sTarget = '') } /** - * @return string A list with the genres or $sNotFound. + * @return string A list with the languages or $sNotFound. */ - public function getGenre() + public function getLanguage() { if (true === $this->isReady) { - $sMatch = $this->getGenreAsUrl(); + $sMatch = $this->getLanguageAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -993,7 +849,8 @@ public function getGenre() public function getLanguageAsUrl($sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LANGUAGE); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LANGUAGE); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -1014,12 +871,12 @@ public function getLanguageAsUrl($sTarget = '') } /** - * @return string A list with the languages or $sNotFound. + * @return string A list with the location or $sNotFound. */ - public function getLanguage() + public function getLocation() { if (true === $this->isReady) { - $sMatch = $this->getLanguageAsUrl(); + $sMatch = $this->getLocationAsUrl(); if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -1036,7 +893,8 @@ public function getLanguage() public function getLocationAsUrl($sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LOCATION); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LOCATION); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -1056,21 +914,6 @@ public function getLocationAsUrl($sTarget = '') return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound); } - /** - * @return string A list with the location or $sNotFound. - */ - public function getLocation() - { - if (true === $this->isReady) { - $sMatch = $this->getLocationAsUrl(); - if (self::$sNotFound !== $sMatch) { - return IMDBHelper::cleanString($sMatch); - } - } - - return self::$sNotFound; - } - /** * @return string The MPAA of the movie or $sNotFound. */ @@ -1169,6 +1012,22 @@ public function getRating() return self::$sNotFound; } + /** + * Release date doesn't contain all the information we need to create a media and + * we need this function that checks if users can vote target media (if can, it's released). + * + * @return true If the media is released + */ + public function isReleased() + { + $strReturn = $this->getReleaseDate(); + if ($strReturn == self::$sNotFound || $strReturn == 'Not yet released') { + return false; + } + + return true; + } + /** * @return string The release date of the movie or $sNotFound. */ @@ -1185,29 +1044,28 @@ public function getReleaseDate() } /** - * Release date doesn't contain all the information we need to create a media and - * we need this function that checks if users can vote target media (if can, it's released). - * - * @return true If the media is released + * @return string The runtime of the movie or $sNotFound. */ - public function isReleased() + public function getRuntime() { - $strReturn = $this->getReleaseDate(); - if ($strReturn == self::$sNotFound || $strReturn == 'Not yet released') { - return false; + if (true === $this->isReady) { + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RUNTIME, 1); + if (false !== $sMatch) { + return IMDBHelper::cleanString($sMatch); + } } - return true; + return self::$sNotFound; } /** - * @return string The runtime of the movie or $sNotFound. + * @return string A list with the seasons or $sNotFound. */ - public function getRuntime() + public function getSeasons() { if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RUNTIME, 1); - if (false !== $sMatch) { + $sMatch = $this->getSeasonsAsUrl(); + if (self::$sNotFound !== $sMatch) { return IMDBHelper::cleanString($sMatch); } } @@ -1223,7 +1081,8 @@ public function getRuntime() public function getSeasonsAsUrl($sTarget = '') { if (true === $this->isReady) { - $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SEASONS); + $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SEASONS); + $aReturn = []; if (count($aMatch[1])) { foreach (range(1, max($aMatch[1])) as $i => $sName) { $aReturn[] = @@ -1245,21 +1104,6 @@ public function getSeasonsAsUrl($sTarget = '') return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound); } - /** - * @return string A list with the seasons or $sNotFound. - */ - public function getSeasons() - { - if (true === $this->isReady) { - $sMatch = $this->getSeasonsAsUrl(); - if (self::$sNotFound !== $sMatch) { - return IMDBHelper::cleanString($sMatch); - } - } - - return self::$sNotFound; - } - /** * @return string The sound mix of the movie or $sNotFound. */ @@ -1376,6 +1220,21 @@ public function getVotes() return self::$sNotFound; } + /** + * @return string A list with the writers or $sNotFound. + */ + public function getWriter() + { + if (true === $this->isReady) { + $sMatch = $this->getWriterAsUrl(); + if (self::$sNotFound !== $sMatch) { + return IMDBHelper::cleanString($sMatch); + } + } + + return self::$sNotFound; + } + /** * @param string $sTarget Add a target to the links? * @@ -1384,8 +1243,9 @@ public function getVotes() public function getWriterAsUrl($sTarget = '') { if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_WRITER, 1); - $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_WRITER, 1); + $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME); + $aReturn = []; if (count($aMatch[2])) { foreach ($aMatch[2] as $i => $sName) { $aReturn[] = @@ -1406,13 +1266,13 @@ public function getWriterAsUrl($sTarget = '') } /** - * @return string A list with the writers or $sNotFound. + * @return string The year of the movie or $sNotFound. */ - public function getWriter() + public function getYear() { if (true === $this->isReady) { - $sMatch = $this->getWriterAsUrl(); - if (self::$sNotFound !== $sMatch) { + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_YEAR, 1); + if (false !== $sMatch) { return IMDBHelper::cleanString($sMatch); } } @@ -1421,12 +1281,12 @@ public function getWriter() } /** - * @return string The year of the movie or $sNotFound. + * @return string The budget of the movie or $sNotFound. */ - public function getYear() + public function getBudget() { if (true === $this->isReady) { - $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_YEAR, 1); + $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_BUDGET, 1); if (false !== $sMatch) { return IMDBHelper::cleanString($sMatch); } @@ -1475,8 +1335,7 @@ public static function matchRegex($sContent, $sPattern, $iIndex = null) * @param array $aReturn Original input. * @param bool $bHaveMore Have more elements indicator. * - * @return string Multiple results separated by selected separator string. - * @return array Multiple results enclosed into native array. + * @return string|array Multiple results separated by selected separator string, or enclosed into native array. */ public static function arrayOutput($bArrayOutput, $sSeparator, $sNotFound, $aReturn = null, $bHaveMore = false) { @@ -1608,9 +1467,9 @@ public static function runCurl($sUrl, $bDownload = false) [ CURLOPT_BINARYTRANSFER => ($bDownload ? true : false), CURLOPT_CONNECTTIMEOUT => self::IMDB_TIMEOUT, - CURLOPT_ENCODING => 'gzip,deflate', + CURLOPT_ENCODING => '', CURLOPT_FOLLOWLOCATION => 0, - CURLOPT_FRESH_CONNECT => 1, + CURLOPT_FRESH_CONNECT => 0, CURLOPT_HEADER => ($bDownload ? false : true), CURLOPT_HTTPHEADER => [ 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', @@ -1622,8 +1481,8 @@ public static function runCurl($sUrl, $bDownload = false) CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_TIMEOUT => self::IMDB_TIMEOUT, - CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36', - CURLOPT_VERBOSE => false + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0', + CURLOPT_VERBOSE => 0 ]); $sOutput = curl_exec($oCurl); $aCurlInfo = curl_getinfo($oCurl);