-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclass.imdb.php
More file actions
397 lines (351 loc) · 10.4 KB
/
class.imdb.php
File metadata and controls
397 lines (351 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* A simple class which parses IMDB Webpage and extracts Details from it.
* Requires PHP 5.1 (uses DomXPath, DomDocument)
*
* @author Abdullah Rubiyath <http://www.itsalif.info/>
* @version 2.3
* @license MIT
*
* Release Date: May 21, 2009
* Last Updated: August 10, 2012
*
*
* How to Use (no knowledge of XPath required):
* --------------
* Initialize the Class and then just pass the URL to get the basic Information
*
* + BASIC USAGE:
* $imdbObj = new Imdb();
* $movieInfo = $imdbObj->get('http://www.imdb.com/title/tt0367882/');
*
* $movieInfo should contain all the details of movie in associative Array Format
*
* You can also grab based on a search string like:
* $movieInfo = $imdbObj->get('The Matrix');
*
*
* CHANGE LOG (Jan 20, 2010)
* --------------------------
* Version 2
* > Added the showCast Method. When this method is invoked with 'true' parameter,
* the cast in the movie is also grabbed.
*
* CHANGE LOG (July 8, 2010)
* --------------------------
* Version 2.1
* > Added a simple regex check for validity of URL on isValidURL method.
* > Added a simple search query check on getImdbURL method.
*
* CHANGE LOG (Oct 10, 2010)
* --------------------------
* > Rewrote the XPath expression as IMDB Changed their Layout completely.
*
* CHANGE LOG (Dec 9, 2010)
* -------------------------
* > Fixed a Bug on Cast and added Budget Info.
*
* CHANGE LOG (May 29, 2011)
* -------------------------
* > Version 2.2
* > Added Genres - Provided by Greg Fitzgerald (Github: https://github.com/gregf)
*
* CHANGE LOG (August 10, 2012)
* ----------------------------
* > Version 2.3
* > Replaced DomDocument->load with CURL for loading IMDB Page
* > Replaced title grabbing with regex (as XPath doesn't seem to work?)
* > Updated the xpath expression for Runtime
*/
class Imdb {
/**
* @var array $grabList
*/
protected $grabList;
/**
* @var array $defaultList
*/
protected $defaultList;
/**
* @var boolean $csv
*/
protected $csv;
/**
* @var boolean $cast
*/
protected $cast;
/**
* @var $castList
*/
protected $castList;
/**
* @var $rating
*/
protected $rating;
/**
* @var $genres
*/
protected $genres;
/**
* This Constructor of the Class
* @return
*/
public function __construct() {
/* the suffix of xpath expression for each item to be grabbed */
$this->defaultList = array (
'Country:' => '/a',
'Language:' => "/a",
'Runtime:' => '/time',
'Aspect Ratio:' => '/text()',
'Release Date:' => '/text()',
'Budget:' => '/text()'
);
$this->cast = true;
$this->castList = array();
$this->grabList = $this->defaultList;
$this->csv = true;
$this->rating = true;
$this->genres = true;
}
/**
* The destructor of the Class
*/
public function __destruct() {
unset($this->defaultList);
unset($this->grabList);
}
/**
* This Method Grabs the info from IMDB website and returns it in an
* associative array format, false on failure
*
* @param string|$url The Url or Search Query of the IMDB movie to be grabbed
* @return array|boolean Associative array in key=>value pairs on success,
* False on failure
*/
public function get($url) {
if (!$this->isValidURL($url)) {
$url = $this->getImdbURL($url);
}
$imdbDom = new DomDocument();
$pageOutput = $this->getURLContent($url);
$imdbLoad = $imdbDom->loadHTML($pageOutput);
if ($imdbLoad === false) {
return false;
}
$xpath = new DomXPath($imdbDom);
$grabValue = array (
'title:' => $this->removeNewLines($xpath
->query('//h1[@class="header"]/span/text()')
->item(0)
->nodeValue),
'year:' => $this->removeNewLines($xpath
->query('//h1[@class="header"]/span/a/text()')
->item(0)
->nodeValue),
'url:' => $url
);
$imageNode = $xpath->query("//td[@id='img_primary']/div/a/img");
if ($imageNode->length != 0) {
$grabValue['image'] = $imageNode->item(0)->getAttribute('src');
}
// grab director, writer, (Dont get run time from here)
$nodeList = $xpath->query("//td[@id='overview-top']/div[@class='txt-block']");
for($i = 0; $i < $nodeList->length-1; $i++) {
$nodeNameList = $xpath->query('h4', $nodeList->item($i));
$grabName = trim($nodeNameList->item(0)->nodeValue);
$nodeValueList = $xpath->query('a', $nodeList->item($i));
$grabValue[$grabName] = $this->getValue('a', $nodeValueList);
}
// grab story line:
$grabValue['Storyline:'] = $this->removeNewLines($xpath
->query("//div[@class='article'][h2='Storyline']/div/p")
->item(0)
->firstChild
->nodeValue
);
/* check if cast needs to be grabbed */
if ($this->cast) {
$castNameList = $xpath->query("//td[@itemprop='actor']/a/span/text()");
$castThumbList = $xpath->query("//td[@class='primary_photo']/a/img");
$castCharList = $xpath->query("//td[@class='character']/div/a/text()");
$totalElem = $castNameList->length;
for ($i = 0; $i < $totalElem; $i++) {
$this->castList[$i] = array (
"name" => $castNameList->item($i)->nodeValue ,
"thumb" => $castThumbList->item($i)->getAttribute('loadlate'),
"cast" => $castCharList->item($i)->nodeValue
);
}
$grabValue['Cast:'] = $this->castList;
}
if ($this->rating) {
$grabValue['User Rating:'] = $xpath->query("//span[@itemprop='ratingValue']/text()")->item(0)->nodeValue;
$grabValue['Total Votes:'] = $xpath->query("//span[@itemprop='ratingCount']/text()")->item(0)->nodeValue;
}
if ($this->genres) {
$genresNameList = $xpath->query("//div[@class='infobar']/a");
$totalElem = $genresNameList->length;
$this->genresList = $this->getValue('/a', $genresNameList);
$grabValue['Genres:'] = $this->genresList;
}
foreach($this->grabList as $k=>$v) {
$xpathString = "//div[@class='article']/div[@class='txt-block'][h4='{$k}']{$v}";
$grabValue[$k] = $this->getValue($v, $xpath->query($xpathString));
}
return $grabValue;
}
/**
* An Internal Method that calculates the values for each nodes. Its only accessed
* internally from within the class.
*
* @param string $nodeType The type of node (/text(), /a etc.)
* @param array $nodeList The List of Nodes from where query is to be made
*
* @return string|array
*/
protected function getValue($nodeType, $nodeList) {
/* strpos is used instead of regex, because strpos is faster, and === has not been used */
$totalItem = $nodeList->length;
$nodeValue = '';
if (strpos($nodeType, '/text()') !== false) {
for($i = 0; $i < $totalItem; $i++) {
$nodeValue .= trim($nodeList->item($i)->nodeValue);
}
} else {
// if user wants a csv value of nodes, return csv, else return an array
if ($this->csv) {
for ($i = 0; $i < $totalItem; $i++) {
$nodeValue .= trim($nodeList->item($i)->nodeValue).',';
}
$nodeValue = substr($nodeValue, 0, -1);
} else {
$nodeValue = array();
for ($i = 0; $i < $totalItem; $i++) {
$nodeVal = trim($nodeList->item($i)->nodeValue);
if($nodeVal != '') {
$nodeValue[] = $nodeVal;
}
}
}
}
return $nodeValue;
}
/**
* Removes new lines in a string
*
* @param $str The string to be processed
* @return $str the clean string
*/
protected function removeNewLines($str) {
return str_replace("\n", "", $str);
}
/**
* This Method sets whether to grab cast or not.
*
* @param boolean $t
* @return object
*/
public function showCast($t) {
$this->cast = $t;
return $this;
}
/**
* Set the Rating (whether rating should be shown or not)
*
* @param boolean $t
* @return object Current Object
*/
public function showRating($t) {
$this->rating = $t;
return $this;
}
/**
* Set the Genres (whether genres should be grabbed or not)
*
* @param boolean $t
* @return object
*/
public function showGenres($t) {
$this->genres = $t;
return $this;
}
/**
* Set the permission to use CSV or not
*
* @return object
* @param string $status
*/
public function useCSV($status) {
$this->csv = $status;
return $this;
}
/**
* Checks if a URL is a valid URL or not
*
* @return boolean
* @param string $url
**/
public function isValidURL($url) {
// a simple regex to check if the url matches imdb.com domain style
$matchCount = preg_match("/(www\.)?imdb\.com\/title\/[A-Za-z0-9]+/i", $url);
return !($matchCount < 1);
}
/**
* Grab a proper URL from a search query
*
* @param string $url The imdb url to be grabbed
* @return string/boolean Returns the url on success and false on failure
*/
public function getImdbURL($url) {
$queryStr = '//p[@style]/b/a';
$validTitleStr = "//head/link[@rel='canonical']";
$searchURL = 'http://www.imdb.com/find?q='.urlencode($url);
$output = $this->getURLContent($searchURL);
/* replacing XPath with regex as regex doesn't work with the new URL */
preg_match_all('/link rel="canonical" href="([^"]+)?"/', $output, $matches);
/* ensure there was a match */
if (count($matches[1]) < 1) {
return false;
}
// See if Imdb redirected to a page. It happens for some movies (Shutter Island)
$linkHrefURL = $matches[1][0];
if($this->isValidURL($linkHrefURL)) {
return $linkHrefURL;
}
$searchDom = new DomDocument();
$searchLoad = $searchDom->loadHTML($output);
$xpath = new DomXPath($searchDom);
if($xpath->query($queryStr)->length > 0) {
return 'http://www.imdb.com'.$xpath->query($queryStr)->item(0)->getAttribute('href');
} else {
return false;
}
}
/**
* Uses CURL to extract content from the imdb url
*
* @param $url The imdb url to be grabbed
* @return The contents of the page (from $url) as string
*/
private function getURLContent($url) {
// create curl resource
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 2, // stop after 2 redirects?
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
/* store the output */
$output = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
return $output;
}
}