Skip to content

Commit a50c634

Browse files
author
steph
committed
Add seasons and episodes
1 parent 86ba665 commit a50c634

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/HtmlPieces.php

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HtmlPieces
1919
* @param string $element
2020
* @return string
2121
*/
22-
public function get(object $page, string $element)
22+
public function get(object $page, string $element, string $url='')
2323
{
2424
// Initiate dom object
2525
// -> handles page scraping
@@ -32,7 +32,7 @@ public function get(object $page, string $element)
3232

3333
return $this->strClean($title);
3434
break;
35-
35+
3636
case "genre":
3737
$allGenres = $dom->find($page, "div[data-testid=genres] a");
3838
$genres = [];
@@ -193,14 +193,14 @@ public function get(object $page, string $element)
193193
if ($this->count($castRow->find('img')) === 0) {
194194
continue;
195195
}
196-
196+
197197
$actor = [];
198198
$actor["actor"] = "";
199199
$actor["avatar"] = "";
200200
$actor["avatar_hq"] = "";
201201
$actor["actor_id"] = "";
202202
$actor["character"] = "";
203-
203+
204204
// Actor
205205
$actorLink = $castRow->find('a[data-testid=title-cast-item__actor]');
206206
if ($this->count($actorLink)) {
@@ -217,7 +217,7 @@ public function get(object $page, string $element)
217217
$actor["avatar_hq"] = preg_match('/\.\_/', $actor["avatar_hq"]) ? preg_split('/\.\_.*/', $actor["avatar_hq"])[0] . ".jpg" : $actor["avatar_hq"];
218218
}
219219
}
220-
220+
221221
// Actor ID
222222
$link = $castRow->find('a');
223223
if ($this->count($link)) {
@@ -227,24 +227,61 @@ public function get(object $page, string $element)
227227
$actor["actor_id"] = $matches[0];
228228
}
229229
}
230-
230+
231231
// Character
232232
$characterLink = $castRow->find('[data-testid=cast-item-characters-link] span');
233233
if ($this->count($characterLink)) {
234234
$actor["character"] = $characterLink->text;
235235
}
236-
236+
237237
$actor["character"] = $this->strClean($actor["character"]);
238238
$actor["actor"] = $this->strClean($actor["actor"]);
239239
$actor["avatar"] = $this->strClean($actor["avatar"]);
240240
$actor["actor_id"] = $this->strClean($actor["actor_id"]);
241-
241+
242242
array_push($cast, $actor);
243243
}
244244
}
245245
return $cast;
246246
break;
247247

248+
case "seasons":
249+
$seasons = [];
250+
$findAllSeasons = $dom->find($page, "#bySeason > option");
251+
$dom = new \PHPHtmlParser\Dom();
252+
foreach ($findAllSeasons as $seasonRow){
253+
$season = [];
254+
$seasonValue = $seasonRow->getAttribute('value');
255+
$season['season'] = $seasonValue;
256+
// Using imdb ajax api to get episodes
257+
$season['episodes'] = $this->get($dom->loadFromUrl($url."/_ajax?season=".$seasonValue), "episodes");
258+
array_push($seasons, $season);
259+
}
260+
return $seasons;
261+
break;
262+
263+
case "episodes":
264+
$episodes = [];
265+
$findAllEpisodes = $dom->find($page, ".eplist > .list_item");
266+
foreach ($findAllEpisodes as $episodeRow){
267+
$episode = [];
268+
$hyperlink = $episodeRow->find("a[itemprop=url]");
269+
$episode["id"] = $this->extractImdbId($hyperlink->getAttribute("href"));
270+
$episode['title'] = $episodeRow->find('a[itemprop=name]')->text;
271+
$episode['description'] = $episodeRow->find(".item_description")->text;
272+
$rating = $episodeRow->find(".ipl-rating-star__rating");
273+
if($this->count($rating)) {
274+
$episode['rating'] = $rating->text;
275+
}
276+
$image = $hyperlink->find("img");
277+
if($this->count($image)) {
278+
$episode["poster"] = $image->getAttribute("src");
279+
}
280+
array_push($episodes, $episode);
281+
}
282+
return $episodes;
283+
break;
284+
248285
case "technical_specs":
249286
$technical_specs = [];
250287
$table = $dom->find($page, '.dataTable tr');
@@ -315,7 +352,7 @@ public function get(object $page, string $element)
315352
*
316353
* @param object $page
317354
* @param array $patterns
318-
* @return string
355+
* @return string
319356
*/
320357
public function findMatchInPatterns(object $dom, object $page, array $patterns, string $type = "text")
321358
{

src/Imdb.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ private function populateOptions(array $options = []): array
2525
'category' => 'all',
2626
'curlHeaders' => ['Accept-Language: en-US,en;q=0.5'],
2727
'techSpecs' => true,
28+
'seasons' => true,
2829
];
2930

3031
// Merge any user options with the default ones
@@ -115,6 +116,16 @@ public function film(string $filmId, array $options = []): array
115116
$response->add("technical_specs", []);
116117
}
117118

119+
// If seasons is enabled
120+
if ($options['seasons']) {
121+
$url = "https://www.imdb.com/title/$filmId/episodes";
122+
$page_seasons = $dom->fetch($url, $options);
123+
// If film has episodes or seasons
124+
if (count($page_seasons->find(".error_code_404")) == 0) {
125+
$response->add("seasons", $htmlPieces->get($page_seasons, "seasons", $url));
126+
}
127+
}
128+
118129
// If caching is enabled
119130
if ($options["cache"]) {
120131
// Add result to the cache

src/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function default(string $endpoint): array
8080
"id" => "",
8181
"link" => ""
8282
],
83+
"seasons" => [],
8384
"cast" => [],
8485
"technical_specs" => []
8586
];

0 commit comments

Comments
 (0)