Skip to content

Commit b3ff77b

Browse files
committed
Get cast from dom
1 parent 7abcd32 commit b3ff77b

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

src/Dom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function fetch(string $url, array $options)
3333
* @param object $dom
3434
* @param string $selection
3535
*
36-
* @return array
36+
* @return array|object
3737
*/
3838
public function find(object $dom, string $selection)
3939
{

src/HtmlPieces.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,81 @@ public function get(object $page, string $element)
6262
return $this->strClean($poster);
6363
break;
6464

65+
case "trailer":
66+
$trailerLink = $page->find('.slate a[data-video]');
67+
$trailerId = $this->count($trailerLink) ? $trailerLink->getAttribute("data-video") : "";
68+
$trailerLink = $this->count($trailerId) ? "https://www.imdb.com/videoplayer/".$trailerId : "";
69+
return [
70+
"id" => $trailerId,
71+
"link" => $trailerLink
72+
];
73+
break;
74+
75+
case "cast":
76+
$cast = [];
77+
$findAllCast = $dom->find($page, 'table.cast_list tr');
78+
foreach ($findAllCast as $castRow)
79+
{
80+
if (count($castRow->find('.primary_photo')) === 0) {
81+
continue;
82+
}
83+
$actor = [];
84+
85+
$characterLink = $castRow->find('.character a');
86+
$actor["character"] = count($characterLink) ? $characterLink->text : $dom->find($castRow, '.character')->text;
87+
88+
$actorRow = $castRow->find('td')[1];
89+
$actorLink = $actorRow->find('a');
90+
if (count($actorLink) > 0)
91+
{
92+
// Set actor name to text within link
93+
$actor["actor"] = $actorLink->text;
94+
$actor["actor_id"] = $this->extractImdbId($actorLink->href);
95+
} else
96+
{
97+
// No link found
98+
// Set actor name to whatever is there
99+
$actor["actor"] = $actorRow->text;
100+
}
101+
102+
$actor["character"] = $this->strClean($actor["character"]);
103+
$actor["actor"] = $this->strClean($actor["actor"]);
104+
$actor["actor_id"] = $this->strClean($actor["actor_id"]);
105+
106+
array_push($cast, $actor);
107+
}
108+
return $cast;
109+
break;
110+
65111
default:
66112
return "";
67113
}
68114
}
69115

116+
/**
117+
* Extract an imdb-id from a string '/ttxxxxxxx/'
118+
* Returns string of id or empty string if none found
119+
*
120+
* @param string $str
121+
* @return string
122+
*/
123+
private function extractImdbId($str)
124+
{
125+
// Search string for 2 letters followed by numbers
126+
// '/yyxxxxxxx'
127+
preg_match('/\/[A-Za-z]{2}[0-9]+/', $str, $imdbIds);
128+
$id = substr($imdbIds[0], 1);
129+
if ($id == NULL)
130+
{
131+
$id = "";
132+
}
133+
return $id;
134+
}
135+
70136
/**
71137
* Cleans-up string
72138
* -> removes white-space and html entitys
139+
* turns null into empty string
73140
*
74141
* @param $string
75142
* @return string
@@ -79,4 +146,14 @@ private function strClean($string)
79146
return empty($string) ? "" : str_replace(chr(194).chr(160), '', html_entity_decode(trim($string)));
80147
}
81148

149+
/**
150+
* Count (either array items or string length)
151+
*
152+
* @param array|string $item
153+
* @return string
154+
*/
155+
private function count($item) {
156+
return (is_array($item) ? count($item) : strlen($item));
157+
}
158+
82159
}

src/imdb.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ private function populateOptions(array $options = []): array
4545
/**
4646
* Gets film data from IMDB. Will first search if the
4747
* film name is passed instead of film-id
48-
* @param string $film
48+
* @param string $filmId
4949
* @param array $options
5050
* @return array $response
5151
*/
52-
public function film(string $film, array $options = []): array
52+
public function film(string $filmId, array $options = []): array
5353
{
5454
// Combine user options with default ones
5555
$options = $this->populateOptions($options);
@@ -67,10 +67,10 @@ public function film(string $film, array $options = []): array
6767
$htmlPieces = new HtmlPieces;
6868

6969
// Load imdb page and parse the dom
70-
$page = $dom->fetch($this->baseUrl."title/".$film, $options);
70+
$page = $dom->fetch($this->baseUrl."title/".$filmId, $options);
7171

7272
// Add all film data to response $store
73-
$response->add("id", $film);
73+
$response->add("id", $filmId);
7474
$response->add("title", $htmlPieces->get($page, "title"));
7575
$response->add("year", $htmlPieces->get($page, "year"));
7676
$response->add("length", $htmlPieces->get($page, "length"));

0 commit comments

Comments
 (0)