Skip to content

Commit 4e9ff99

Browse files
authored
Merge pull request #8 from kas7err/patch-pattern-matches
added genre to film search, also fix pattern matches for plot, rating and cast
2 parents c5bfd7c + e23695f commit 4e9ff99

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

src/HtmlPieces.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public function get(object $page, string $element)
3232

3333
return $this->strClean($title);
3434
break;
35+
36+
case "genre":
37+
$allGenres = $dom->find($page, "div[data-testid=genres] a");
38+
$genres = [];
39+
40+
if ($this->count($allGenres)) {
41+
foreach ($allGenres as $genre) {
42+
$genres[] = $genre->find('span')->text();
43+
}
44+
}
45+
46+
return $this->strClean(implode(", ", $genres));
47+
break;
3548

3649
case "year":
3750
$patterns = ["section section div div div ul li a", ".title_wrapper h1 #titleYear a", ".title_wrapper .subtext a[title='See more release dates']"];
@@ -80,14 +93,14 @@ public function get(object $page, string $element)
8093
break;
8194

8295
case "plot":
83-
$patterns = ["p[data-testid=plot] div", ".plot_summary .summary_text"];
96+
$patterns = ["p[data-testid=plot] > span[data-testid=plot-xl]", ".plot_summary .summary_text"];
8497
$plot = $this->findMatchInPatterns($dom, $page, $patterns);
8598

8699
return $this->strClean($plot);
87100
break;
88101

89102
case "rating":
90-
$patterns = ["main div[data-testid=hero-title-block__aggregate-rating__score] span", ".ratings_wrapper .ratingValue span[itemprop=ratingValue]"];
103+
$patterns = ["main div[data-testid=hero-rating-bar__aggregate-rating__score] span", ".ratings_wrapper .ratingValue span[itemprop=ratingValue]"];
91104
$rating = $this->findMatchInPatterns($dom, $page, $patterns);
92105

93106
return $this->strClean($rating);
@@ -139,7 +152,7 @@ public function get(object $page, string $element)
139152
case "cast":
140153
$cast = [];
141154
$findAllCastOld = $dom->find($page, 'table.cast_list tr');
142-
$findAllCast = $dom->find($page, 'section.title-cast div.title-cast__grid div');
155+
$findAllCast = $dom->find($page, 'section[data-testid=title-cast] div.title-cast__grid div.ipc-sub-grid div');
143156

144157
// Use $findAllCastOld
145158
if ($this->count($findAllCastOld)) {
@@ -183,6 +196,7 @@ public function get(object $page, string $element)
183196

184197
$actor = [];
185198
$actor["actor"] = "";
199+
$actor["avatar"] = "";
186200
$actor["actor_id"] = "";
187201
$actor["character"] = "";
188202

@@ -191,6 +205,12 @@ public function get(object $page, string $element)
191205
if ($this->count($actorLink)) {
192206
$actor["actor"] = $actorLink->text;
193207
}
208+
209+
// Avatar
210+
$actorAvatar = $castRow->find('img.ipc-image');
211+
if ($this->count($actorAvatar)) {
212+
$actor["avatar"] = $actorAvatar->getAttribute('src');
213+
}
194214

195215
// Actor ID
196216
$link = $castRow->find('a');
@@ -203,13 +223,14 @@ public function get(object $page, string $element)
203223
}
204224

205225
// Character
206-
$characterLink = $castRow->find('a[data-testid=cast-item-characters-link]');
226+
$characterLink = $castRow->find('span[data-testid=cast-item-characters-with-as]');
207227
if ($this->count($characterLink)) {
208228
$actor["character"] = $characterLink->text;
209229
}
210230

211231
$actor["character"] = $this->strClean($actor["character"]);
212232
$actor["actor"] = $this->strClean($actor["actor"]);
233+
$actor["avatar"] = $this->strClean($actor["avatar"]);
213234
$actor["actor_id"] = $this->strClean($actor["actor_id"]);
214235

215236
array_push($cast, $actor);
@@ -369,7 +390,7 @@ public function extractImdbId($str)
369390
*/
370391
public function strClean($string)
371392
{
372-
return empty($string) ? "" : str_replace(chr(194).chr(160), '', html_entity_decode(trim($string)));
393+
return empty($string) ? "" : str_replace(chr(194).chr(160), '', html_entity_decode(trim($string), ENT_QUOTES));
373394
}
374395

375396
/**

src/Imdb.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function film(string $filmId, array $options = []): array
9595
// Add all film data to response $store
9696
$response->add("id", $filmId);
9797
$response->add("title", $htmlPieces->get($page, "title"));
98+
$response->add("genre", $htmlPieces->get($page, "genre"));
9899
$response->add("year", $htmlPieces->get($page, "year"));
99100
$response->add("length", $htmlPieces->get($page, "length"));
100101
$response->add("plot", $htmlPieces->get($page, "plot"));

src/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function default(string $endpoint): array
6969
$response = [
7070
"id" => "",
7171
"title" => "",
72+
"genre" => "",
7273
"year" => "",
7374
"length" => "",
7475
"plot" => "",

tests/ImdbTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ public function testFilm()
1414

1515
$this->assertEquals('tt0816692', $film['id']);
1616
$this->assertEquals('Interstellar', $film['title']);
17-
$this->assertEquals('2h 49min', $film['length']);
17+
$this->assertEquals('Adventure, Drama, Sci-Fi', $film['genre']);
18+
$this->assertEquals('2h 49m', $film['length']);
1819
$this->assertEquals('2014', $film['year']);
20+
$this->assertEquals("A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.", $film['plot']);
21+
$this->assertEquals('8.6', $film['rating']);
1922
$this->assertEquals('vi1586278169', $film['trailer']["id"]);
2023
$this->assertEquals('https://www.imdb.com/video/vi1586278169', $film['trailer']["link"]);
21-
$this->assertContains($film['cast'][0]["character"], array('Cooper', 'Murph (Older)'));
22-
$this->assertContains($film['cast'][0]["actor"], array('Matthew McConaughey', 'Ellen Burstyn'));
23-
$this->assertContains($film['cast'][0]["actor_id"], array('nm0000190', 'nm0000995'));
24+
$this->assertContains($film['cast'][0]["character"], ['as Cooper']);
25+
$this->assertContains($film['cast'][0]["actor"], ['Matthew McConaughey']);
26+
$this->assertContains($film['cast'][0]["actor_id"], ['nm0000190']);
27+
$this->assertContains($film['cast'][0]["avatar"], ['https://m.media-amazon.com/images/M/MV5BMTg0MDc3ODUwOV5BMl5BanBnXkFtZTcwMTk2NjY4Nw@@._V1_QL75_UX140_CR0,21,140,140_.jpg']);
2428
}
2529

2630
public function testFilmBySearching()
@@ -30,13 +34,17 @@ public function testFilmBySearching()
3034

3135
$this->assertEquals('tt0816692', $film['id']);
3236
$this->assertEquals('Interstellar', $film['title']);
33-
$this->assertEquals('2h 49min', $film['length']);
37+
$this->assertEquals('Adventure, Drama, Sci-Fi', $film['genre']);
38+
$this->assertEquals('2h 49m', $film['length']);
3439
$this->assertEquals('2014', $film['year']);
40+
$this->assertEquals("A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.", $film['plot']);
41+
$this->assertEquals('8.6', $film['rating']);
3542
$this->assertEquals('vi1586278169', $film['trailer']["id"]);
3643
$this->assertEquals('https://www.imdb.com/video/vi1586278169', $film['trailer']["link"]);
37-
$this->assertContains($film['cast'][0]["character"], array('Cooper', 'Murph (Older)'));
38-
$this->assertContains($film['cast'][0]["actor"], array('Matthew McConaughey', 'Ellen Burstyn'));
39-
$this->assertContains($film['cast'][0]["actor_id"], array('nm0000190', 'nm0000995'));
44+
$this->assertContains($film['cast'][0]["character"], ['as Cooper']);
45+
$this->assertContains($film['cast'][0]["actor"], ['Matthew McConaughey']);
46+
$this->assertContains($film['cast'][0]["actor_id"], ['nm0000190']);
47+
$this->assertContains($film['cast'][0]["avatar"], ['https://m.media-amazon.com/images/M/MV5BMTg0MDc3ODUwOV5BMl5BanBnXkFtZTcwMTk2NjY4Nw@@._V1_QL75_UX140_CR0,21,140,140_.jpg']);
4048
}
4149

4250
public function testFilmOptions()

0 commit comments

Comments
 (0)