Skip to content

Commit cde2e01

Browse files
committed
Uppercase filename for Imdb class
1 parent cf2f629 commit cde2e01

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

src/Imdb.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
namespace hmerritt;
3+
4+
/**
5+
* Class Imdb
6+
*
7+
*
8+
* @package hmerritt/imdb-api
9+
* @author Harry Merritt
10+
*/
11+
class Imdb
12+
{
13+
14+
/**
15+
* Returns default options combined with any user options
16+
*
17+
* @param string $options
18+
* @return array $defaults
19+
*/
20+
private function populateOptions(array $options = []): array
21+
{
22+
// Default options
23+
$defaults = [
24+
'cache' => true,
25+
'category' => 'all',
26+
'curlHeaders' => ['Accept-Language: en-US,en;q=0.5'],
27+
'techSpecs' => true,
28+
];
29+
30+
// Merge any user options with the default ones
31+
foreach ($options as $key => $option) {
32+
$defaults[$key] = $option;
33+
}
34+
35+
// Return final options array
36+
return $defaults;
37+
}
38+
39+
/**
40+
* Gets film data from IMDB. Will first search if the
41+
* film name is passed instead of film-id
42+
* @param string $filmId
43+
* @param array $options
44+
* @return array $response
45+
*/
46+
public function film(string $filmId, array $options = []): array
47+
{
48+
// Combine user options with default ones
49+
$options = $this->populateOptions($options);
50+
51+
// Initiate response object
52+
// -> handles what the api returns
53+
$response = new Response;
54+
55+
// Initiate cache object
56+
// -> handles storing/retrieving cached results
57+
$cache = new Cache;
58+
59+
// Initiate dom object
60+
// -> handles page scraping
61+
$dom = new Dom;
62+
63+
// Initiate html-pieces object
64+
// -> handles finding specific content from the dom
65+
$htmlPieces = new HtmlPieces;
66+
67+
// Check for 'tt' at start of $filmId
68+
if (substr($filmId, 0, 2) !== "tt")
69+
{
70+
// Search $filmId and use first result
71+
$search_film = $this->search($filmId, [ "category" => "tt" ]);
72+
if ($htmlPieces->count($search_film["titles"]) > 0)
73+
{
74+
// Use first film returned from search
75+
$filmId = $search_film["titles"][0]["id"];
76+
} else
77+
{
78+
// No film found
79+
// -> return default (empty) response
80+
return $response->default('film');
81+
}
82+
}
83+
84+
// If caching is enabled
85+
if ($options["cache"]) {
86+
// Check cache for film
87+
if ($cache->has($filmId)) {
88+
return $cache->get($filmId)->film;
89+
}
90+
}
91+
92+
// Load imdb film page and parse the dom
93+
$page = $dom->fetch("https://www.imdb.com/title/".$filmId, $options);
94+
95+
// Add all film data to response $store
96+
$response->add("id", $filmId);
97+
$response->add("title", $htmlPieces->get($page, "title"));
98+
$response->add("year", $htmlPieces->get($page, "year"));
99+
$response->add("length", $htmlPieces->get($page, "length"));
100+
$response->add("plot", $htmlPieces->get($page, "plot"));
101+
$response->add("rating", $htmlPieces->get($page, "rating"));
102+
$response->add("rating_votes", $htmlPieces->get($page, "rating_votes"));
103+
$response->add("poster", $htmlPieces->get($page, "poster"));
104+
$response->add("trailer", $htmlPieces->get($page, "trailer"));
105+
$response->add("cast", $htmlPieces->get($page, "cast"));
106+
107+
// If techSpecs is enabled in user $options
108+
// -> Make a second request to load the full techSpecs page
109+
if ($options["techSpecs"]) {
110+
$page_techSpecs = $dom->fetch("https://www.imdb.com/title/$filmId/technical", $options);
111+
$response->add("technical_specs", $htmlPieces->get($page_techSpecs, "technical_specs"));
112+
}
113+
else {
114+
$response->add("technical_specs", []);
115+
}
116+
117+
// If caching is enabled
118+
if ($options["cache"]) {
119+
// Add result to the cache
120+
$cache->add($filmId, $response->return());
121+
}
122+
123+
// Return the response $store
124+
return $response->return();
125+
}
126+
127+
/**
128+
* Searches IMDB for films, people and companies
129+
* @param string $search
130+
* @param array $options
131+
* @return array $searchData
132+
*/
133+
public function search(string $search, array $options = []): array
134+
{
135+
// Combine user options with default ones
136+
$options = $this->populateOptions($options);
137+
138+
// Initiate response object
139+
// -> handles what the api returns
140+
$response = new Response;
141+
142+
// Initiate dom object
143+
// -> handles page scraping
144+
$dom = new Dom;
145+
146+
// Initiate html-pieces object
147+
// -> handles finding specific content from the dom
148+
$htmlPieces = new HtmlPieces;
149+
150+
// Load imdb search page and parse the dom
151+
$page = $dom->fetch("https://www.imdb.com/find?q=$search&s=".$options["category"], $options);
152+
153+
// Add all search data to response $store
154+
$response->add("titles", $htmlPieces->get($page, "titles"));
155+
$response->add("names", $htmlPieces->get($page, "names"));
156+
$response->add("companies", $htmlPieces->get($page, "companies"));
157+
158+
return $response->return();
159+
}
160+
161+
}

0 commit comments

Comments
 (0)