-
Notifications
You must be signed in to change notification settings - Fork 160
/
imdb.class.php
2549 lines (2195 loc) · 93.2 KB
/
imdb.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PHP IMDb.com Grabber
*
* This PHP library enables you to scrape data from IMDB.com.
*
*
* If you want to thank me for this library, please buy me something at Amazon
* (https://www.amazon.de/hz/wishlist/ls/8840JITISN9L/) or use
* https://www.paypal.me/FabianBeiner. Thank you! 🙌
*
* @author Fabian Beiner <[email protected]>
* @license https://opensource.org/licenses/MIT The MIT License
* @link https://github.com/FabianBeiner/PHP-IMDB-Grabber/ GitHub Repository
* @version 6.2.4
*/
class IMDB
{
/**
* Set this to true if you run into problems.
*/
const IMDB_DEBUG = false;
/**
* Set the preferred language for the User Agent.
*/
const IMDB_LANG = 'en-US,en;q=0.9';
/**
* Set this to true if you want to start with normal search and
* if you get no result, it will use the advanced method
*/
const IMDB_SEARCH_ORIGINAL = true;
/**
* Set this to true if you want to search for exact titles
* it falls back to false if theres no result
*/
const IMDB_EXACT_SEARCH = true;
/**
* Set the sensitivity for search results in percentage.
*/
const IMDB_SENSITIVITY = 85;
/**
* Define the timeout for cURL requests.
*/
const IMDB_TIMEOUT = 15;
/**
* These are the regular expressions used to extract the data.
* If you don’t know what you’re doing, you shouldn’t touch them.
*/
const IMDB_AKA = '~<td[^>]*>\s*Also\s*Known\s*As\s*</td>\s*<td>(.+)</td>~Uis';
const IMDB_ASPECT_RATIO = '~<td[^>]*>Aspect\s*Ratio</td>\s*<td>(.+)</td>~Uis';
const IMDB_AWARDS = '~<div\s*class="titlereference-overview-section">\s*Awards:(.+)</div>~Uis';
const IMDB_BUDGET = '~<td[^>]*>Budget<\/td>\s*<td>\s*(.*)(?:\(estimated\))\s*<\/td>~Ui';
const IMDB_CAST = '~<td[^>]*itemprop="actor"[^>]*>\s*<a\s*href="/name/([^/]*)/\?[^"]*"[^>]*>\s*<span.+>(.+)</span~Ui';
const IMDB_CAST_IMAGE = '~(loadlate="(.*)"[^>]*><\/a>\s+<\/td>\s+)?<td[^>]*itemprop="actor"[^>]*>\s*<a\s*href="\/name\/([^/]*)\/\?[^"]*"[^>]*>\s*<span.+>(.+)<\/span+~Uis';
const IMDB_CERTIFICATION = '~<td[^>]*>\s*Certification\s*</td>\s*<td>(.+)</td>~Ui';
const IMDB_CHAR = '~<td class="character">(?:\s+)<div>(.*)(?:\s+)(?: /| \(.*\)|<\/div>)~Ui';
const IMDB_COLOR = '~<a href="\/search\/title\?colors=(?:.*)">(.*)<\/a>~Ui';
const IMDB_COMPANIES = '~production_companies&ref_=(?:.*)">Edit</a>\s+</header>\s+<ul class="simpleList">(.*)Distributors</h4>~Uis';
const IMDB_COMPANY = '~<li>\s+<a href="\/company\/(co[0-9]+)\/">(.*?)</a>~';
const IMDB_COUNTRY = '~<a href="/country/(\w+)">(.*)</a>~Ui';
const IMDB_CREATOR = '~<div[^>]*>\s*(?:Creator|Creators)\s*:\s*<ul[^>]*>(.+)</ul>~Uxsi';
const IMDB_DISTRIBUTOR = '@href="[^"]*update=[t0-9]+:distributors[^"]*">Edit</a>\s*</header>\s*<ul\s*class="simpleList">(.*):special_effects_companies@Uis';
const IMDB_DISTRIBUTORS = '@\/company\/(co[0-9]+)\/">(.*?)<\/a>\s+(?:\(([0-9]+)\))?\s+(?:\((.*?)\))?\s+(?:\((.*?)\))?\s+(?:\((?:.*?)\))?\s+</li>@';
const IMDB_DIRECTOR = '~<div[^>]*>\s*(?:Director|Directors)\s*:\s*<ul[^>]*>(.+)</ul>~Uxsi';
const IMDB_GENRE = '~href="/genre/([a-zA-Z_-]*)/?">([a-zA-Z_ -]*)</a>~Ui';
const IMDB_GROSS = '~pl-zebra-list__label">Cumulative Worldwide Gross<\/td>\s*<td>\s*(.*?)\s*<\/td>~i';
const IMDB_ID = '~((?:tt\d{6,})|(?:itle\?\d{6,}))~';
const IMDB_LANGUAGE = '~<a href="\/language\/(\w+)">(.*)<\/a>~Ui';
const IMDB_LOCATION = '~href="\/search\/title\?locations=(.*)">(.*)<\/a>~Ui';
const IMDB_LOCATIONS = '~href="(?<url>\/search\/title\/\?locations=[^>]*)">\s?(?<location>.*)\s?<\/a><p(.*)>\((?<specification>.*)\)<\/p>~Ui';
const IMDB_MPAA = '~<li class="ipl-inline-list__item">(?:\s+)(TV-Y|TV-Y7|TV-Y7-FV|TV-G|TV-PG|TV-14|TV-MA|TV-MA-L|TV-MA-S|TV-MA-V|G|PG|PG-13|R|NC-17|NR|UR|M|X)(?:\s+)<\/li>~Ui';
const IMDB_MUSIC = '~Music by\s*<\/h4>.*<table class=.*>(.*)</table>~Us';
const IMDB_NAME = '~href="/name/(.+)/?(?:\?[^"]*)?"[^>]*>(.+)</a>~Ui';
const IMDB_MOVIE_DESC = '~<section class="titlereference-section-overview">\s+<div>\s+(.*)\s*?</div>\s+<hr>\s+<div class="titlereference-overview-section">~Ui';
const IMDB_SERIES_DESC = '~<div>\s+(?:.*?</a>\s+</span>\s+</div>\s+<hr>\s+<div>\s+)(.*)\s+</div>\s+<hr>\s+<div class="titlereference-overview-section">~Ui';
const IMDB_SERIESEP_DESC = '~All Episodes(?:.*?)</li>\s+(?:.*?)?</ul>\s+</span>\s+<hr>\s+</div>\s+<div>\s+(.*?)\s+</div>\s+<hr>~';
const IMDB_NOT_FOUND_ADV = '~"results-section-empty-results-msg"~Ui';
const IMDB_NOT_FOUND_DES = 'Know what this is about';
const IMDB_NOT_FOUND_ORG = '~<h1 class="findHeader">No results found for ~Ui';
const IMDB_PLOT = '~<td[^>]*>\s*Plot\s*Summary\s*</td>\s*<td>\s*<p>\s*(.*)\s*</p>~Ui';
const IMDB_PLOT_KEYWORDS = '~<td[^>]*>Plot\s*Keywords</td>\s*<td>(.+)(?:<a\s*href="/title/[^>]*>[^<]*</a>\s*</li>\s*</ul>\s*)?</td>~Ui';
const IMDB_POSTER = '~<link\s*rel=\'image_src\'\s*href="(.*)">~Ui';
const IMDB_RATING = '~class="ipl-rating-star__rating">(.*)<~Ui';
const IMDB_RATING_COUNT = '~class="ipl-rating-star__total-votes">\((.*)\)<~Ui';
const IMDB_RELEASE_DATE = '~href="/title/tt\d+/releaseinfo">\s*(.*?)(?:\s*\([^)]*\))?\s*</a~i';
const IMDB_RUNTIME = '~<td[^>]*>\s*Runtime\s*</td>\s*<td>(.+)</td>~Ui';
const IMDB_SEARCH_ADV = '~<a href="/title/(tt\d+).*?ipc-title-link-wrapper~i';
const IMDB_SEARCH_ORG = '~find-title-result">(?:.*?)alt="(.*?)"(?:.*?)href="\/title\/(tt\d{6,})\/(?:.*?)">(.*?)<\/a>~';
const IMDB_SEASONS = '~episodes\?season=(?:\d+)">(\d+)<~Ui';
const IMDB_SOUND_MIX = '~<td[^>]*>\s*Sound\s*Mix\s*</td>\s*<td>(.+)</td>~Ui';
const IMDB_TAGLINE = '~<td[^>]*>\s*Taglines\s*</td>\s*<td>(.+)</td>~Ui';
const IMDB_TITLE = '~itemprop="name">(.*)(<\/h3>|<span)~Ui';
const IMDB_TITLE_EP = '~titlereference-watch-ribbon"(?:.*)itemprop="name">(.*?)\s+<span\sclass="titlereference-title-year">~Ui';
const IMDB_TITLE_ORIG = '~</h3>(?:\s+)(.*)(?:\s+)<span class=\"titlereference-original-title-label~Ui';
const IMDB_TOP250 = '~href="/chart/top(?:tv)?".class(?:.*?)#([0-9]{1,})</a>~Ui';
const IMDB_TRAILER = '~href="/title/(?:tt\d+)/videoplayer/(vi[0-9]*)"~Ui';
const IMDB_TYPE = '~href="/genre/(?:[a-zA-Z_-]*)/?">(?:[a-zA-Z_ -]*)</a>\s+</li>\s+(?:.*item">)\s+(?:<a href="(?:.*)</a>\s+</li>\s+(?:.*item">)\s+)?([a-zA-Z_ -]*)\s+</li>~Ui';
const IMDB_URL = '~https?://(?:.*\.|.*)imdb.com/(?:t|T)itle(?:\?|/)(..\d+)~i';
const IMDB_USER_REVIEW = '~href="/title/[t0-9]*/reviews"[^>]*>([^<]*)\s*User~Ui';
const IMDB_VOTES = '~"ipl-rating-star__total-votes">\s*\((.*)\)\s*<~Ui';
const IMDB_WRITER = '~<div[^>]*>\s*(?:Writer|Writers)\s*:\s*<ul[^>]*>(.+)</ul>~Ui';
const IMDB_YEAR = '~:title\' content="[^"]+ \((?:[^()]+ )?(\d{4}(?:–\d{4})?)\)~';
/**
* @var string The string returned, if nothing is found.
*/
public static $sNotFound = 'n/A';
/**
* @var null|int The ID of the movie.
*/
public $iId = null;
/**
* @var bool Is the content ready?
*/
public $isReady = false;
/**
* @var string Char that separates multiple entries.
*/
public $sSeparator = ' / ';
/**
* @var null|string The URL to the movie.
*/
public $sUrl = null;
/**
* @var bool Return responses enclosed in array
*/
public $bArrayOutput = false;
/**
* @var int Maximum cache time.
*/
private $iCache = 1440;
/**
* @var null|string The root of the script.
*/
private $sRoot = null;
/**
* @var null|string Holds the source.
*/
private $sSource = null;
/**
* @var string What to search for?
*/
private $sSearchFor = 'all';
/**
* @param string $sSearch IMDb URL or movie title to search for.
* @param null $iCache Custom cache time in minutes.
* @param string $sSearchFor What to search for?
*
* @throws Exception
*/
public function __construct($sSearch, $iCache = null, $sSearchFor = 'all')
{
$this->sRoot = dirname(__FILE__);
if ( ! is_writable($this->sRoot . '/posters') && ! mkdir($this->sRoot . '/posters')) {
throw new Exception('The directory “' . $this->sRoot . '/posters” isn’t writable.');
}
if ( ! is_writable($this->sRoot . '/cache') && ! mkdir($this->sRoot . '/cache')) {
throw new Exception('The directory “' . $this->sRoot . '/cache” isn’t writable.');
}
if ( ! is_writable($this->sRoot . '/cast') && ! mkdir($this->sRoot . '/cast')) {
throw new Exception('The directory “' . $this->sRoot . '/cast” isn’t writable.');
}
if ( ! function_exists('curl_init')) {
throw new Exception('You need to enable the PHP cURL extension.');
}
if (in_array(
$sSearchFor,
[
'movie',
'tv',
'episode',
'game',
'documentary',
'all',
]
)) {
$this->sSearchFor = $sSearchFor;
}
if (true === self::IMDB_DEBUG) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
echo '<pre><b>Running:</b> fetchUrl("' . $sSearch . '")</pre>';
}
if (null !== $iCache && (int) $iCache > 0) {
$this->iCache = (int) $iCache;
}
if (self::IMDB_EXACT_SEARCH) {
if ($this->fetchUrl($sSearch, self::IMDB_SEARCH_ORIGINAL, true)) {
return true;
}
}
if ($this->fetchUrl($sSearch, self::IMDB_SEARCH_ORIGINAL)) {
return true;
}
if ($this->fetchUrl($sSearch, !self::IMDB_SEARCH_ORIGINAL)) {
return true;
}
}
/**
* @param string $sSearch IMDb URL or movie title to search for.
*
* @return bool True on success, false on failure.
*/
private function fetchUrl($sSearch, $orgSearch = false, $exactSearch = false)
{
$sSearch = trim($sSearch);
// Try to find a valid URL.
$sId = IMDBHelper::matchRegex($sSearch, self::IMDB_ID, 1);
if (false !== $sId) {
$this->iId = preg_replace('~[\D]~', '', $sId);
$this->sUrl = 'https://www.imdb.com/title/tt' . $this->iId . '/reference';
$bSearch = false;
} else {
if (!$orgSearch) {
switch (strtolower($this->sSearchFor)) {
case 'movie':
$sParameters = '&title_type=feature';
break;
case 'tv':
$sParameters = '&title_type=tv_movie,tv_series,tv_special,tv_miniseries';
break;
case 'episode':
$sParameters = '&title_type=tv_episode';
break;
case 'game':
$sParameters = '&title_type=video_game';
break;
case 'documentary':
$sParameters = '&title_type=documentary';
break;
case 'video':
$sParameters = '&title_type=video';
break;
default:
$sParameters = '';
}
if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
$sParameters .= '&release_date=' . $fMatch[2] . '-01-01,' . $fMatch[2] . '-12-31';
$sSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
}
$this->sUrl = 'https://www.imdb.com/search/title/?title=' . rawurlencode(str_replace(' ', '+', $sSearch)) . $sParameters;
} else {
switch (strtolower($this->sSearchFor)) {
case 'movie':
$sParameters = '&s=tt&ttype=ft';
break;
case 'tv':
$sParameters = '&s=tt&ttype=tv';
break;
case 'episode':
$sParameters = '&s=tt&ttype=ep';
break;
case 'game':
$sParameters = '&s=tt&ttype=vg';
break;
default:
$sParameters = '&s=tt';
}
if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
$sYear = $fMatch[2];
$sTempSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
$sSearch = $sTempSearch . ' (' . $sYear . ')';
}
if ($exactSearch) {
$sParameters .= '&exact=true';
}
$this->sUrl = 'https://www.imdb.com/find/?q=' . rawurlencode(str_replace(' ', ' ', $sSearch)) . $sParameters;
}
$bSearch = true;
// Was this search already performed and cached?
$sRedirectFile = $this->sRoot . '/cache/' . sha1($this->sUrl) . '.redir';
if (is_readable($sRedirectFile)) {
if (self::IMDB_DEBUG) {
echo '<pre><b>Using redirect:</b> ' . basename($sRedirectFile) . '</pre>';
}
$sRedirect = file_get_contents($sRedirectFile);
$this->sUrl = trim($sRedirect);
$this->iId = preg_replace('~[\D]~', '', IMDBHelper::matchRegex($sRedirect, self::IMDB_ID, 1));
$bSearch = false;
}
}
// Does a cache of this movie exist?
if (! is_null($this->iId)) {
$sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '.cache';
if (is_readable($sCacheFile)) {
$iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
if ($iDiff < $this->iCache) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Using cache:</b> ' . basename($sCacheFile) . '</pre>';
}
$this->sSource = file_get_contents($sCacheFile);
$this->isReady = true;
return true;
}
}
}
// Run cURL on the URL.
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Running cURL:</b> ' . $this->sUrl . '</pre>';
}
$aCurlInfo = IMDBHelper::runCurl($this->sUrl);
$sSource = isset($aCurlInfo['contents']) ? $aCurlInfo['contents'] : false;
if (false === $sSource) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
}
return false;
}
if (!$orgSearch) {
// Was the movie found?
$sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_SEARCH_ADV, 1);
if (false !== $sMatch) {
$sUrl = 'https://www.imdb.com/title/' . $sMatch . '/reference';
if (true === self::IMDB_DEBUG) {
echo '<pre><b>New redirect saved:</b> ' . basename($sRedirectFile) . ' => ' . $sUrl . '</pre>';
}
file_put_contents($sRedirectFile, $sUrl);
$this->sSource = null;
self::fetchUrl($sUrl);
return true;
}
$sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_NOT_FOUND_ADV, 0);
if (false !== $sMatch) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Movie not found:</b> ' . $sSearch . '</pre>';
}
return false;
}
} else {
$aReturned = IMDBHelper::matchRegex($sSource, self::IMDB_SEARCH_ORG);
if ($aReturned) {
$rData = [];
$fTempPercent = 0.00;
$iTempId = "";
$sYear = 0;
if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
$sYear = $fMatch[2];
$sTempSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
if (true === self::IMDB_DEBUG) {
echo '<pre><b>YEAR:</b> ' . $sTempSearch . ' => ' . $sYear . '</pre>';
}
}
foreach ($aReturned[1] as $i => $value) {
$sId = $aReturned[2][$i];
$sTitle = $aReturned[3][$i];
$perc = 0.00;
$year = 0;
if ($sYear === 0) {
$sim = similar_text($sSearch, $sTitle, $perc);
} else {
$sMatch = IMDBHelper::matchRegex($aReturned[1][$i], '~\(?([0-9]{4})\)?~', 1);
if (false !== $sMatch) {
$year = $sMatch;
}
if ($sYear != $year) {
continue;
}
$sim = similar_text($sTempSearch, $sTitle, $perc);
}
$rData[] = [
'id' => $sId,
'title' => $sTitle,
'year' => $year,
'match' => floatval($perc)
];
}
if (sizeof($rData) === 0) {
return false;
}
if (true === self::IMDB_DEBUG) {
foreach ($rData as $sArray) {
echo '<pre><b>Found results:</b> ' . $sArray['id'] . ' => ' . $sArray['title'] . ' (' . $sArray['match']. '%) </pre>';
}
}
//get highest match of search results
$matches = array_column($rData, 'match');
$maxv = max($matches);
$marray = array_filter($rData, function($item) use ($maxv) {
return $item['match'] == $maxv;
});
$marray = reset($marray);
if (sizeof($marray) > 0) {
if (!$exactSearch && round($marray['match'], 0) < self::IMDB_SENSITIVITY) {
echo '<pre><b>Bad sensitivity:</b> ' . $marray['id'] . ' => ' . $marray['title'] . ' (' . $marray['match']. '%) </pre>';
return false;
}
$sUrl = 'https://www.imdb.com/title/' . $marray['id'] . '/reference';
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Get best result:</b> ' . $marray['title'] . ' ' . $marray['id'] . ' => ' . $marray['match'] . '% </pre>';
echo '<pre><b>New redirect saved:</b> ' . basename($sRedirectFile) . ' => ' . $sUrl . '</pre>';
}
file_put_contents($sRedirectFile, $sUrl);
$this->sSource = null;
self::fetchUrl($sUrl);
return true;
}
}
$sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_NOT_FOUND_ORG, 0);
if (false !== $sMatch) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Movie not found:</b> ' . $sSearch . '</pre>';
}
return false;
}
return false;
}
$this->sSource = str_replace(
[
"\n",
"\r\n",
"\r",
],
'',
$sSource
);
$this->isReady = true;
// Save cache.
if (false === $bSearch) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>Cache created:</b> ' . basename($sCacheFile) . '</pre>';
}
file_put_contents($sCacheFile, $this->sSource);
}
return true;
}
/**
* @return array All data.
*/
public function getAll()
{
$aData = [];
foreach (get_class_methods(__CLASS__) as $method) {
if (substr($method, 0, 3) === 'get' && $method !== 'getAll' && $method !== 'getCastImages') {
$aData[$method] = [
'name' => ltrim($method, 'get'),
'value' => $this->{$method}(),
];
}
}
array_multisort($aData);
return $aData;
}
/**
* @return string “Also Known As” or $sNotFound.
*/
public function getAka()
{
if (true === $this->isReady) {
$aReturn = [];
$sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_AKA, 1);
if (false !== $sMatch) {
$aReturn[] = explode('|', IMDBHelper::cleanString($sMatch));
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* Returns all local names
*
* @return string All local names.
*/
public function getAkas()
{
if (true === $this->isReady) {
// Does a cache of this movie exist?
$sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_akas.cache';
$bUseCache = false;
if (is_readable($sCacheFile)) {
$iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
if ($iDiff < $this->iCache || false) {
$bUseCache = true;
}
}
if ($bUseCache) {
$aRawReturn = file_get_contents($sCacheFile);
$aReturn = unserialize($aRawReturn);
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
} else {
$fullAkas = sprintf('https://www.imdb.com/title/tt%s/releaseinfo/', $this->iId);
$aCurlInfo = IMDBHelper::runCurl($fullAkas);
$sSource = $aCurlInfo['contents'] ?? false;
if (false === $sSource) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
}
return false;
}
$aReturned = IMDBHelper::matchRegex($sSource, '~<span class="ipc-metadata-list-item__label"[^>]*>([^<]+)</span>.*?<span class="ipc-metadata-list-item__list-content-item"[^>]*>([^<]+)</span>(?:\s*<span class="ipc-metadata-list-item__list-content-item--subText"[^>]*>\(([^)]+)\)</span>)?~s');
if ($aReturned) {
$aReturn = [];
foreach ($aReturned[1] ?? [] as $i => $strName) {
if (strpos($strName, '(') === false) {
$aReturn[] = [
'title' => IMDBHelper::cleanString($aReturned[2][$i]),
'country' => IMDBHelper::cleanString($strName),
];
}
}
file_put_contents($sCacheFile, serialize($aReturn));
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
}
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* Returns meta score
*
* @return string metascore
* @return string reviews
*/
public function getMetaScore()
{
if (true === $this->isReady) {
// Does a cache of this movie exist?
$sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_metascore.cache';
$bUseCache = false;
if (is_readable($sCacheFile)) {
$iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
if ($iDiff < $this->iCache || false) {
$bUseCache = true;
}
}
if ($bUseCache) {
$aRawReturn = file_get_contents($sCacheFile);
$aReturn = unserialize($aRawReturn);
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
} else {
$fullCritics = sprintf('https://www.imdb.com/title/tt%s/criticreviews/', $this->iId);
$aCurlInfo = IMDBHelper::runCurl($fullCritics);
$sSource = $aCurlInfo['contents'] ?? false;
if (false === $sSource) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
}
return IMDB::$sNotFound;
}
$aReturned = IMDBHelper::matchRegex(
$sSource,
'~<div class="sc-8c54a7fa-1 gTQbcw">(\d+)</div>.*?(\d+) reviews~s'
);
if ($aReturned) {
$aReturn = [];
$aReturn[] = [
'metascore' => isset($aReturned[1][0]) ? IMDBHelper::cleanString($aReturned[1][0]) : '',
'reviews' => isset($aReturned[2][0]) ? IMDBHelper::cleanString($aReturned[2][0]) : '',
];
file_put_contents($sCacheFile, serialize($aReturn));
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
}
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* Returns Critic Reviews based on Metascore
*
* @return string rating
* @return string url
* @return string publisher
* @return string author
* @return string review
*/
public function getMetaCritics()
{
if (true === $this->isReady) {
// Does a cache of this movie exist?
$sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_criticreviews.cache';
$bUseCache = false;
if (is_readable($sCacheFile)) {
$iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
if ($iDiff < $this->iCache || false) {
$bUseCache = true;
}
}
if ($bUseCache) {
$aRawReturn = file_get_contents($sCacheFile);
$aReturn = unserialize($aRawReturn);
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
} else {
$fullCritics = sprintf('https://www.imdb.com/title/tt%s/criticreviews/', $this->iId);
$aCurlInfo = IMDBHelper::runCurl($fullCritics);
$sSource = $aCurlInfo['contents'] ?? false;
if (false === $sSource) {
if (true === self::IMDB_DEBUG) {
echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
}
return IMDB::$sNotFound;
}
$aReturned = IMDBHelper::matchRegex(
$sSource,
'~<div class="sc-d8486f96-2 (?:fgepEK|kPhAAe|crRWFG)">(\d+)</div>.*?<span class="sc-d8486f96-5 jyAgZO">(.*?)</span>(?:<a.*?href="(.*?)".*?>)?(.*?)</(?:span|a)>.*?<div>(.*?)</div>~'
);
if ($aReturned) {
$aReturn = [];
foreach ($aReturned[1] as $i => $strScore) {
$aReturn[] = [
'rating' => IMDBHelper::cleanString($strScore),
'url' => IMDBHelper::cleanString($aReturned[2][$i]),
'publisher' => IMDBHelper::cleanString($aReturned[3][$i]),
'author' => IMDBHelper::cleanString($aReturned[4][$i]),
'review' => IMDBHelper::cleanString($aReturned[5][$i]),
];
}
file_put_contents($sCacheFile, serialize($aReturn));
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
}
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @return string “Aspect Ratio” or $sNotFound.
*/
public function getAspectRatio()
{
if (true === $this->isReady) {
$sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_ASPECT_RATIO, 1);
if (false !== $sMatch) {
return IMDBHelper::cleanString($sMatch);
}
}
return self::$sNotFound;
}
/**
* @return string The awards of the movie or $sNotFound.
*/
public function getAwards()
{
if (true === $this->isReady) {
$sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_AWARDS, 1);
if (false !== $sMatch) {
return IMDBHelper::cleanString($sMatch);
}
}
return self::$sNotFound;
}
/**
* @param int $iLimit How many cast members should be returned?
* @param bool $bMore Add … if there are more cast members than printed.
* @param string $sTarget Add a target to the links?
*
* @return string A list with linked cast members or $sNotFound.
*/
public function getCastAsUrl($iLimit = 0, $bMore = true, $sTarget = '')
{
if (true === $this->isReady) {
$aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
$aReturn = [];
if (count($aMatch[2])) {
foreach ($aMatch[2] as $i => $sName) {
if (0 !== $iLimit && $i >= $iLimit) {
break;
}
$aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
$aMatch[1][$i]
) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
$sName
) . '</a>';
}
$bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
return IMDBHelper::arrayOutput(
$this->bArrayOutput,
$this->sSeparator,
self::$sNotFound,
$aReturn,
$bHaveMore
);
}
}
return self::$sNotFound;
}
/**
* @param int $iLimit How many cast members should be returned?
* @param bool $bMore Add … if there are more cast members than printed.
*
* @return string A list with cast members or $sNotFound.
*/
public function getCast($iLimit = 0, $bMore = true)
{
if (true === $this->isReady) {
$aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
$aReturn = [];
if (count($aMatch[2])) {
foreach ($aMatch[2] as $i => $sName) {
if (0 !== $iLimit && $i >= $iLimit) {
break;
}
$aReturn[] = IMDBHelper::cleanString($sName);
}
$bMore = (0 !== $iLimit && $bMore && (count($aMatch[2]) > $iLimit) ? '…' : '');
$bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
return IMDBHelper::arrayOutput(
$this->bArrayOutput,
$this->sSeparator,
self::$sNotFound,
$aReturn,
$bHaveMore
);
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @param int $iLimit How many cast images should be returned?
* @param bool $bMore Add … if there are more cast members than printed.
* @param string $sSize small, mid or big cast images
* @param bool $bDownload Return URL or Download
*
* @return array Array with cast name as key, and image as value.
*/
public function getCastImages($iLimit = 0, $bMore = true, $sSize = 'small', $bDownload = false)
{
if (true === $this->isReady) {
$aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST_IMAGE);
$aReturn = [];
if (count($aMatch[4])) {
foreach ($aMatch[4] as $i => $sName) {
if (0 !== $iLimit && $i >= $iLimit) {
break;
}
$sMatch = $aMatch[2][$i];
if ('big' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
$sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@.jpg';
} elseif ('mid' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
$sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@._V1_UX214_AL_.jpg';
}
if (false === $bDownload) {
$sMatch = IMDBHelper::cleanString($sMatch);
} else {
$sLocal = IMDBHelper::saveImageCast($sMatch, $aMatch[3][$i]);
if (file_exists(dirname(__FILE__) . '/' . $sLocal)) {
$sMatch = $sLocal;
} else {
//the 'big' image isn't available, try the 'mid' one (vice versa)
if ('big' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
//trying the 'mid' one
$sMatch = substr(
$aMatch[2][$i],
0,
strpos($aMatch[2][$i], '@._')
) . '@._V1_UX214_AL_.jpg';
} else {
//trying the 'big' one
$sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@.jpg';
}
$sLocal = IMDBHelper::saveImageCast($sMatch, $aMatch[3][$i]);
if (file_exists(dirname(__FILE__) . '/' . $sLocal)) {
$sMatch = $sLocal;
} else {
$sMatch = IMDBHelper::cleanString($aMatch[2][$i]);
}
}
}
$aReturn[IMDBHelper::cleanString($aMatch[4][$i])] = $sMatch;
}
$bMore = (0 !== $iLimit && $bMore && (count($aMatch[4]) > $iLimit) ? '…' : '');
$bHaveMore = ($bMore && (count($aMatch[4]) > $iLimit));
$aReturn = array_replace(
$aReturn,
array_fill_keys(
array_keys($aReturn, self::$sNotFound),
'cast/not-found.jpg'
)
);
return $aReturn;
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @param int $iLimit How many cast members should be returned?
* @param bool $bMore Add … if there are more cast members than
* printed.
* @param string $sTarget Add a target to the links?
*
* @return string A list with linked cast members and their character or
* $sNotFound.
*/
public function getCastAndCharacterAsUrl($iLimit = 0, $bMore = true, $sTarget = '')
{
if (true === $this->isReady) {
$aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
$aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR);
$aReturn = [];
if (count($aMatch[2])) {
foreach ($aMatch[2] as $i => $sName) {
if (0 !== $iLimit && $i >= $iLimit) {
break;
}
$sChar = str_replace(' / ', ' and ', $aMatchChar[1][$i]);
$aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
$aMatch[1][$i]
) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
$sName
) . '</a> as ' . IMDBHelper::cleanString($sChar);
}
$bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
return IMDBHelper::arrayOutput(
$this->bArrayOutput,
$this->sSeparator,
self::$sNotFound,
$aReturn,
$bHaveMore
);
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @param int $iLimit How many cast members should be returned?
* @param bool $bMore Add … if there are more cast members than printed.
*
* @return string A list with cast members and their character or
* $sNotFound.
*/
public function getCastAndCharacter($iLimit = 0, $bMore = true)
{
if (true === $this->isReady) {
$aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
$aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR);
$aReturn = [];
if (count($aMatch[2])) {
foreach ($aMatch[2] as $i => $sName) {
if (0 !== $iLimit && $i >= $iLimit) {
break;
}
$sChar = str_replace(' / ', ' and ', $aMatchChar[1][$i]);
$aReturn[] = IMDBHelper::cleanString($sName) . ' as ' . IMDBHelper::cleanString($sChar);
}
$bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
return IMDBHelper::arrayOutput(
$this->bArrayOutput,
$this->sSeparator,
self::$sNotFound,
$aReturn,
$bHaveMore
);
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @return string The certification of the movie or $sNotFound.
*/
public function getCertification()
{
if (true === $this->isReady) {
$aReturn = [];
$sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CERTIFICATION, 1);
if (false !== $sMatch) {
$aReturn[] = explode('|', IMDBHelper::cleanString($sMatch));
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
}
}
return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
}
/**
* @return string Color or $sNotFound.
*/
public function getColor()
{