Skip to content

Commit c578e7e

Browse files
committed
Get film title from dom
1 parent 2d10ea9 commit c578e7e

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/HtmlPieces.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace hmerritt;
3+
4+
/**
5+
* Class HtmlPieces
6+
*
7+
*
8+
* @package hmerritt/imdb-api
9+
* @author Harry Merritt
10+
*/
11+
class HtmlPieces
12+
{
13+
14+
/**
15+
* Attempts to find and return a specific element
16+
* from the IMDB dom
17+
*
18+
* @param object $dom
19+
* @param string $element
20+
* @return string
21+
*/
22+
public function get(object $page, string $element)
23+
{
24+
// Initiate dom object
25+
// -> handles page scraping
26+
$dom = new Dom;
27+
28+
switch ($element) {
29+
case "title":
30+
return $this->strClean($dom->find($page, '.title_wrapper h1')->text);
31+
break;
32+
33+
default:
34+
return "";
35+
}
36+
}
37+
38+
/**
39+
* Cleans-up string
40+
* -> removes white-space and html entitys
41+
*
42+
* @param string $string
43+
* @return string
44+
*/
45+
private function strClean(string $string)
46+
{
47+
return empty($string) ? "" : str_replace(chr(194).chr(160), '', html_entity_decode(trim($string)));
48+
}
49+
50+
}

src/imdb.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ public function film(string $film, array $options = []): array
6262
// -> handles page scraping
6363
$dom = new Dom;
6464

65+
// Initiate html-pieces object
66+
// -> handles finding specific content from the dom
67+
$htmlPieces = new HtmlPieces;
68+
6569
// Load imdb page and parse the dom
66-
$page = $dom->fetch("https://www.imdb.com/title/tt0816692/", $options);
70+
$page = $dom->fetch($this->baseUrl."title/".$film, $options);
6771

68-
$response->add("id", "tt0816692");
69-
$response->add("title", "Interstellar");
70-
$response->add("length", "2h 49min");
71-
$response->add("year", "2014");
72+
// Add all film data to response $store
73+
$response->add("id", $film);
74+
$response->add("title", $htmlPieces->get($page, "title"));
75+
$response->add("length", $htmlPieces->get($page, "length"));
76+
$response->add("year", $htmlPieces->get($page, "year"));
7277

78+
// Return the response $store
7379
return $response->return();
7480
}
7581

0 commit comments

Comments
 (0)