-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathomdb.class.php
385 lines (335 loc) · 10.5 KB
/
omdb.class.php
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
<?php
/*
OMDb API PHP Wrapper
Author: Rasmus Lindroth
Version: 0.1
OMDb's Webpage
http://www.omdbapi.com/
*/
class OMDb {
//API url
private $url = 'http://www.omdbapi.com/';
//Request timeout
private $timeout;
//Date format
private $date_format;
//Default parameters
private $params = [
//movie, series, episode or NULL
'type' => NULL,
//Year of release or NULL
'y' => NULL,
//short, full
'plot' => 'short',
//json, if you edit
//this one you will
//have to rewrite the
//parse method
'r' => 'json',
//Rotten Tomatoes
//TRUE, FALSE
'tomatoes' => FALSE,
//API Key, you must pass this one otherwise the requests will fail
'apikey' => '',
//api version. Don't edit this one
//if you don't know what you're doing
'v' => 1
];
//$params = array(param => value)
//$timeout = request timeout in seconds
//$date = see this page for format http://php.net/manual/function.date.php,
//can be NULL and returns UNIX-time
public function __construct($params = [], $timeout = 5, $date_format = 'Y-m-d') {
//Set the API parameters
$this->setParams($params);
//Set the cURL timeout
$this->timeout = $timeout;
//Set the date format
$this->date_format = $date_format;
}
//Set the parameters for the API request
//$params = array(param => value)
public function setParams($params) {
//Make sure $params is an array
if(is_array($params) !== TRUE) {
throw new Exception('$params has to be an array.');
}
$validParams = array_keys($this->params);
foreach($params as $param => $value) {
//lowered key
$k = strtolower($param);
//Check if parameter is valid
//and make an edit to it
if(in_array($k, $validParams)) {
$this->params[$k] = $value;
}else {
throw new Exception($param . ' isn\'t a valid parameter.');
}
}
}
//Set only one parameter
public function setParam($param, $value) {
//Sends the parameter as an array to the method setParams
$this->setParams( [ $param => $value ] );
}
//Unset a parameter
public function unsetParam($param) {
$this->setParams( [ $param => NULL ] );
}
//Create URL, including extra params like id or title params
// array( array(type, value) )
private function createURL($p) {
$params = $this->params;
//Add all params from $p
foreach($p as $value) {
$params[$value[0]] = $value[1];
}
$tmp_params = [];
foreach($params as $param => $value) {
//Bool to string
if(is_bool($value)) {
$value = ($value) ? 'true' : 'false';
}
//Ignore NULL values
if(is_null($value) !== TRUE) {
$tmp_params[$param] = $value;
}
}
$query = http_build_query($tmp_params);
return $this->url . '?' . $query;
}
//Fetches the url and runs json_decode
private function request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
//Checks if the request did succed
if($info['http_code'] !== 200) {
throw new Exception(
'Request failed. HTTP CODE: '
. $info['http_code']
);
}
return json_decode($content);
}
//Handels the requests from get_by_* methods
private function get_data($url) {
$request = $this->request($url);
//Parse the request
$parsed = $this->parse_result($request);
return $parsed;
}
//Get by IMDb id
//$id = tt[0-9]
//returns an array
public function get_by_id($id, $season = NULL, $episode = NULL) {
//Checks if the IMDb id is valud
if($this->valid_imdb_id($id) === FALSE) {
throw new Exception('The IMDb id is invalid.');
}
$params = [
['i', $id]
];
if($season !== NULL) {
$params[] = ['Season', $season];
if($episode !== NULL) {
$params[] = ['Episode', $episode];
}
}
//Gets the URL
$url = $this->createURL($params);
//Gets the data and returns it
return $this->get_data($url);
}
//Get by title
//returns an array
public function get_by_title($title) {
$params = [
['t', $title]
];
//Gets the URL
$url = $this->createURL($params);
//Gets the data and returns it
return $this->get_data($url);
}
//This function search for multiple movies
//ignores the plot and tomatoes parameters
//returns array(
// Search => array(Title, Year, imdbID, Type), array(...)
// )
public function search($s, $page = NULL) {
$params = [
['s', $s]
];
if($page !== NULL) {
$params[] = ['page', $page];
}
//Gets the URL
$url = $this->createURL($params);
//Gets the data and returns it
return $this->get_data($url);
}
private static function valid_imdb_id($id) {
return preg_match('/^tt\d+?$/', $id);
}
//Explodes string
//foo, bar returns ['foo', 'bar']
//foo returns foo
private function parse_many($value) {
$arr = explode(', ', $value);
if(count($arr) === 1) {
return $arr[0];
}else {
return $arr;
}
}
//Parses array
private function parse_array($value) {
$result = [];
foreach ($value as $item) {
$parsedItem = [];
foreach ($item as $key=>$value) {
$parsedItem[$key] = $value;
}
$result[] = $parsedItem;
}
return $result;
}
//Parses date to
//to the specified format
private function parse_date($date) {
$unix = strtotime($date);
if(is_null($date) === FALSE) {
return date($this->date_format, $unix);
}else {
return $unix;
}
}
//Parses runtime to return a int with
//the minutes
private function parse_runtime($value) {
return (int)strstr($value, ' min', true);
}
//String (with comma) to int
private function parse_int($value) {
return (int)str_replace(',', '', $value);
}
//Value to float
private function parse_float($value) {
return (float)$value;
}
//String to Bool
private function parse_bool($value) {
if(trim(strtolower($value)) === 'true') {
return TRUE;
}else {
return FALSE;
}
}
//Parses all the result
//with the connected method
//and returns an array
//with the data
private function parse_result($object) {
//Rules for how to parse the data
//array,date,runtime,many,int,float,bool,search and NULL
$rules = [
'Title' => NULL,
'Year' => NULL,
'Rated' => NULL,
'Released' => 'date',
'Runtime' => 'runtime',
'Genre' => 'many',
'Director' => 'many',
'Writer' => 'many',
'Actors' => 'many',
'Plot' => NULL,
'Language' => 'many',
'Country' => 'many',
'Awards' => NULL,
'Poster' => NULL,
'Ratings' => 'array',
'Metascore' => 'int',
'imdbRating' => 'float',
'imdbVotes' => 'int',
'imdbID' => NULL,
'Type' => NULL,
'tomatoMeter' => 'int',
'tomatoImage' => NULL,
'tomatoRating' => 'float',
'tomatoReviews' => 'int',
'tomatoFresh' => 'int',
'tomatoRotten' => 'int',
'tomatoConsensus' => NULL,
'tomatoUserMeter' => 'int',
'tomatoUserRating' => 'float',
'tomatoUserReviews' => 'int',
'tomatoURL' => NULL,
'DVD' => NULL,
'BoxOffice' => NULL,
'Production' => NULL,
'Website' => NULL,
'Response' => 'bool',
'Search' => 'search',
'Error' => NULL,
'totalResults' => 'int',
'totalSeasons' => 'int',
'Episodes' => 'array',
'Season' => 'int',
'Episode' => 'int',
'seriesID' => NULL
];
//Object to array
$unParsed = (array)$object;
//Holds the parsed data
$data = [];
//Calls the appropriate method
//based on the rule connected
//with the key
foreach($unParsed as $key => $value) {
if($value === 'N/A') {
$data[$key] = NULL;
}else {
$v = $value;
switch($rules[$key]) {
case 'many':
$v = $this->parse_many($value);
break;
case 'date':
$v = $this->parse_date($value);
break;
case 'runtime':
$v = $this->parse_runtime($value);
break;
case 'int':
$v = $this->parse_int($value);
break;
case 'float':
$v = $this->parse_float($value);
break;
case 'bool':
$v = $this->parse_bool($value);
break;
case 'array':
$v = $this->parse_array($value);
break;
case 'search':
//There is multiple titles, parses
//each of them and adds them to an array
$v = [];
foreach($value as $arr) {
$v[] = $this->parse_result($arr);
}
break;
default:
$v = $value;
}
$data[$key] = $v;
}
}
return $data;
}
}