Skip to content

Commit

Permalink
Fix genre fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
jetrosuni committed Mar 17, 2024
1 parent 22f7918 commit 3db6275
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/Imdb/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public function languages_detailed()
* @return string genre first of the genres listed on the movies main page
* @brief There is not really a main genre on the IMDB sites (yet), so this
* simply returns the first one
* @see IMDB page / (TitlePage)
* @see IMDB page /reference
*/
public function genre()
{
Expand All @@ -710,35 +710,28 @@ public function genre()

/** Get all genres the movie is registered for
* @return array genres (array[0..n] of strings)
* @see IMDB page / (TitlePage)
* @see IMDB page /reference
*/
public function genres()
{
if (empty($this->moviegenres)) {
$xpath = $this->getXpathPage("Title");
$extract_genres = $xpath->query("//li[@data-testid='storyline-genres']//li[@class='ipc-inline-list__item']/a");
$genres = array();
foreach ($extract_genres as $genre) {
if (!empty($genre->nodeValue)) {
$genres[] = trim($genre->nodeValue);
}
}
if (count($genres) > 0) {
$this->moviegenres = $genres;
}
}
if (empty($this->moviegenres)) {
$genres = isset($this->jsonLD()->genre) ? $this->jsonLD()->genre : array();
if (!is_array($genres)) {
$genres = (array)$genres;
}
$this->moviegenres = $genres;
$query = <<<EOF
query Genres(\$id: ID!) {
title(id: \$id) {
titleGenres {
genres {
genre {
text
}
if (empty($this->moviegenres)) {
if (@preg_match('!Genres:</h4>(.*?)</div!ims', $this->page["Title"], $match)) {
if (@preg_match_all('!href="[^>]+?>\s*(.*?)\s*<!', $match[1], $matches)) {
$this->moviegenres = $matches[1];
}
}
}
}
}
EOF;

$data = $this->graphql->query($query, "Genres", ["id" => "tt$this->imdbID"]);
foreach ($data->title->titleGenres->genres as $genres) {
$this->moviegenres[] = $genres->genre->text;
}
}
return $this->moviegenres;
Expand Down

0 comments on commit 3db6275

Please sign in to comment.