Skip to content

Commit d92765d

Browse files
committed
Search for film if no tt-id entered
1 parent 554286f commit d92765d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/HtmlPieces.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function strClean($string)
210210
* @param array|string $item
211211
* @return string
212212
*/
213-
private function count($item)
213+
public function count($item)
214214
{
215215
return (is_countable($item) ? count($item) : (is_string($item) ? strlen($item) : 0));
216216
}

src/imdb.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private function populateOptions(array $options = []): array
2222
// Default options
2323
$defaults = [
2424
'cache' => true,
25+
'category' => 'all',
2526
'curlHeaders' => ['Accept-Language: en-US,en;q=0.5'],
2627
'techSpecs' => true,
2728
];
@@ -59,6 +60,23 @@ public function film(string $filmId, array $options = []): array
5960
// -> handles finding specific content from the dom
6061
$htmlPieces = new HtmlPieces;
6162

63+
// Check for 'tt' at start of $filmId
64+
if (substr($filmId, 0, 2) !== "tt")
65+
{
66+
// Search $filmId and use first result
67+
$search_film = $this->search($filmId, [ "category" => "tt" ]);
68+
if ($htmlPieces->count($search_film["titles"]) > 0)
69+
{
70+
// Use first film returned from search
71+
$filmId = $search_film["titles"][0]["id"];
72+
} else
73+
{
74+
// No film found
75+
// -> return default (empty) response
76+
return $response->default('film');
77+
}
78+
}
79+
6280
// Load imdb film page and parse the dom
6381
$page = $dom->fetch("https://www.imdb.com/title/".$filmId, $options);
6482

@@ -112,7 +130,7 @@ public function search(string $search, array $options = []): array
112130
$htmlPieces = new HtmlPieces;
113131

114132
// Load imdb search page and parse the dom
115-
$page = $dom->fetch("https://www.imdb.com/find?q=$search&s=all", $options);
133+
$page = $dom->fetch("https://www.imdb.com/find?q=$search&s=".$options["category"], $options);
116134

117135
// Add all search data to response $store
118136
$response->add("titles", $htmlPieces->get($page, "titles"));

0 commit comments

Comments
 (0)