/
Title.php
executable file
·3011 lines (2791 loc) · 118 KB
/
Title.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
#############################################################################
# IMDBPHP (c) Giorgos Giagas & Itzchak Rehberg #
# written by Giorgos Giagas #
# extended & maintained by Itzchak Rehberg <izzysoft AT qumran DOT org> #
# http://www.izzysoft.de/ #
# ------------------------------------------------------------------------- #
# This program is free software; you can redistribute and/or modify it #
# under the terms of the GNU General Public License (see doc/LICENSE) #
#############################################################################
namespace Imdb;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
/**
* A title on IMDb
* @author Georgos Giagas
* @author Izzy (izzysoft AT qumran DOT org)
* @copyright (c) 2002-2004 by Giorgos Giagas and (c) 2004-2009 by Itzchak Rehberg and IzzySoft
*/
class Title extends MdbBase
{
const MOVIE = 'Movie';
const TV_SERIES = 'TV Series';
const TV_EPISODE = 'TV Episode';
const TV_MINI_SERIES = 'TV Mini-Series';
const TV_MOVIE = 'TV Movie';
const TV_SPECIAL = 'TV Special';
const TV_SHORT = 'TV Short';
const GAME = 'Video Game';
const VIDEO = 'Video';
const SHORT = 'Short';
protected $akas = array();
protected $awards = array();
protected $countries = array();
protected $castlist = array(); // pilot only
protected $crazy_credits = array();
protected $credits_cast = array();
protected $credits_cinematographer = array();
protected $credits_composer = array();
protected $credits_director = array();
protected $credits_producer = array();
protected $credits_writing = array();
protected $extreviews = array();
protected $goofs = array();
protected $langs = array();
protected $langs_full = array();
protected $aspectratio = "";
protected $main_comment = "";
protected $main_genre = "";
protected $main_keywords = array();
protected $all_keywords = array();
protected $main_language = "";
protected $main_poster = "";
protected $main_poster_thumb = "";
protected $main_pictures = array();
protected $main_plotoutline = "";
protected $main_runtime = "";
protected $main_movietype = "";
protected $main_title = "";
protected $original_title = "";
protected $main_year = -1;
protected $main_endyear = -1;
protected $main_yearspan = array();
protected $main_tagline = "";
protected $main_storyline = "";
protected $main_prodnotes = array();
protected $main_movietypes = array();
protected $main_top250 = -1;
protected $moviecolors = array();
protected $movieconnections = array();
protected $moviegenres = array();
protected $moviequotes = array();
protected $movierecommendations = array();
protected $movieruntimes = array();
protected $mpaas = array();
protected $mpaas_hist = array();
protected $mpaa_justification = "";
protected $plot_plot = array();
protected $synopsis_wiki = "";
protected $release_info = array();
protected $seasoncount = -1;
protected $season_episodes = array();
protected $sound = array();
protected $soundtracks = array();
protected $split_comment = array();
protected $split_plot = array();
protected $split_moviequotes = array();
protected $taglines = array();
protected $trailers = array();
protected $video_sites = array();
protected $soundclip_sites = array();
protected $photo_sites = array();
protected $misc_sites = array();
protected $trivia = array();
protected $compcred_prod = array();
protected $compcred_dist = array();
protected $compcred_special = array();
protected $compcred_other = array();
protected $parental_guide = array();
protected $official_sites = array();
protected $locations = array();
protected $budget = null;
protected $filmingDates = null;
protected $moviealternateversions = array();
protected $isSerial = null;
protected $episodeSeason = null;
protected $episodeEpisode = null;
protected $jsonLD = null;
protected $pageUrls = array(
"AlternateVersions" => '/alternateversions',
"Awards" => "/awards",
"CompanyCredits" => "/companycredits",
"CrazyCredits" => "/crazycredits",
"Credits" => "/fullcredits",
"Episodes" => "/episodes",
"ExtReviews" => "/externalreviews",
"Goofs" => "/trivia?tab=gf",
"Keywords" => "/keywords",
"Locations" => "/locations",
"MovieConnections" => "/movieconnections",
"OfficialSites" => "/officialsites",
"ParentalGuide" => "/parentalguide",
"Plot" => "/plotsummary",
"Quotes" => "/quotes",
"ReleaseInfo" => "/releaseinfo",
"Soundtrack" => "/soundtrack",
"Synopsis" => "/plotsummary",
"Taglines" => "/taglines",
"Technical" => "/technical",
"Title" => "/",
"Trailers" => "/videogallery/content_type-trailer",
"Trivia" => "/trivia",
"VideoSites" => "/externalsites",
);
/**
* Create an imdb object populated with id, title, year, and movie type
* @param string $id imdb ID
* @param string $title film title
* @param int $year
* @param string $type
* @param Config $config
* @param LoggerInterface $logger OPTIONAL override default logger
* @param CacheInterface $cache OPTIONAL override default cache
* @return Title
*/
public static function fromSearchResult(
$id,
$title,
$year,
$type,
Config $config = null,
LoggerInterface $logger = null,
CacheInterface $cache = null
) {
$imdb = new Title($id, $config, $logger, $cache);
$imdb->main_title = $title;
$imdb->main_year = (int)$year;
$imdb->main_movietype = $type;
return $imdb;
}
/**
* @param string $id IMDb ID. e.g. 285331 for https://www.imdb.com/title/tt0285331/
* @param Config $config OPTIONAL override default config
* @param LoggerInterface $logger OPTIONAL override default logger `\Imdb\Logger` with a custom one
* @param CacheInterface $cache OPTIONAL override the default cache with any PSR-16 cache. None of the caching config in `\Imdb\Config` have any effect except cache_expire
*/
public function __construct(
$id,
Config $config = null,
LoggerInterface $logger = null,
CacheInterface $cache = null
) {
parent::__construct($config, $logger, $cache);
$this->setid($id);
}
#-------------------------------------------------------------[ Open Page ]---
protected function buildUrl($page = null)
{
return "https://" . $this->imdbsite . "/title/tt" . $this->imdbID . $this->getUrlSuffix($page);
}
/**
* @param string $pageName internal name of the page
* @return string
*/
protected function getUrlSuffix($pageName)
{
if (isset($this->pageUrls[$pageName])) {
return $this->pageUrls[$pageName];
}
if (preg_match('!^Episodes-(-?\d+)$!', $pageName, $match)) {
return '/episodes?season=' . $match[1];
}
throw new \Exception("Could not find URL for page $pageName");
}
/**
* Get the URL for this title's page
* @return string
*/
public function main_url()
{
return "https://" . $this->imdbsite . "/title/tt" . $this->imdbid() . "/";
}
/**
* Setup title and year properties
*/
protected function title_year()
{
$this->getPage("Title");
if (@preg_match('!<title>(IMDb\s*-\s*)?(?<ititle>.*)(\s*-\s*IMDb)?</title>!', $this->page["Title"], $imatch)) {
$ititle = $imatch['ititle'];
if (preg_match('!(?<title>.*) \((?<movietype>.*)(?<year>\d{4}|\?{4})(( |–)(?<endyear>\d{4}|)).*\)(.*)!',
$ititle, $match)) { // serial
$this->main_movietype = trim($match['movietype']);
$this->main_year = $match['year'];
$this->main_endyear = $match['endyear'] ? $match['endyear'] : '0';
$this->main_title = htmlspecialchars_decode($match['title']);
} elseif (preg_match('!(?<title>.*) \((?<movietype>.*)(?<year>\d{4}|\?{4}).*\)(.*)!', $ititle, $match)) {
$this->main_movietype = trim($match['movietype']);
$this->main_year = $match['year'];
$this->main_endyear = $match['year'];
$this->main_title = htmlspecialchars_decode($match['title']);
} elseif (preg_match('!(?<title>.*) \((?<movietype>.*)\)(.*)!', $ititle,
$match)) { // not yet released, but have been given a movietype.
$this->main_movietype = trim($match['movietype']);
$this->main_title = htmlspecialchars_decode($match['title']);
$this->main_year = '0';
$this->main_endyear = '0';
} elseif (preg_match('!<title>(?<title>.*) - IMDb</title>!', $this->page["Title"],
$match)) { // not yet released, so no dates etc.
$this->main_title = htmlspecialchars_decode($match['title']);
$this->main_year = '0';
$this->main_endyear = '0';
}
if (preg_match('!class="originalTitle">(.+?)<span!s', $this->page["Title"], $otitle)) {
$this->original_title = trim($otitle[1]);
}
if ($this->main_year == "????") {
$this->main_year = "";
}
}
}
/** Get movie type
* @return string movietype (TV Series, Movie, TV Episode, TV Special, TV Movie, TV Mini-Series, Video Game, TV Short, Video)
* @see IMDB page / (TitlePage)
* @brief This is faster than movietypes() as it is retrieved already together with the title.
* If no movietype had been defined explicitly, it returns 'Movie' -- so this is always set.
*/
public function movietype()
{
if (empty($this->main_movietype)) {
if (empty($this->main_title)) {
$this->title_year();
} // Most types are shown in the <title> tag
if (!empty($this->main_movietype)) {
return $this->main_movietype;
}
// TV Special isn't shown in the page title but is mentioned next to the release date
if (preg_match('/title="See more release dates" >TV Special/', $this->getPage("Title"), $match)) {
$this->main_movietype = 'TV Special';
}
if (empty($this->main_movietype)) {
$this->main_movietype = 'Movie';
}
}
return $this->main_movietype;
}
/** Get movie title
* @return string title movie title (name)
* @see IMDB page / (TitlePage)
*/
public function title()
{
if ($this->main_title == "") {
$this->title_year();
}
return $this->main_title;
}
/** Get movie original title
* @return string title original movie title (name), if available
* @see IMDB page / (TitlePage)
*/
public function orig_title()
{
if ($this->original_title == "") {
$this->title_year();
}
return $this->original_title;
}
/** Get year
* @return string year
* @see IMDB page / (TitlePage)
*/
public function year()
{
if ($this->main_year == -1) {
$this->title_year();
}
return $this->main_year;
}
/** Get end-year
* Usually this returns the same value as year() -- except for those cases where production spanned multiple years, usually for series
* @return string year
* @see IMDB page / (TitlePage)
*/
public function endyear()
{
if ($this->main_endyear == -1) {
$this->title_year();
}
return $this->main_endyear;
}
/** Get range of years for e.g. series spanning multiple years
* @return array yearspan [start,end] (if there was no range, start==end)
* @see IMDB page / (TitlePage)
*/
function yearspan()
{
if (empty($this->main_yearspan)) {
$this->main_yearspan = array('start' => $this->year(), 'end' => $this->endyear());
}
return $this->main_yearspan;
}
/** Get movie types (if any specified)
* @return array [0..n] of strings (or empty array if no movie types specified)
* @see IMDB page / (TitlePage)
*/
public function movieTypes()
{
if (empty($this->main_movietypes)) {
$this->getPage("Title");
if (@preg_match("/\<title\>(.*)\<\/title\>/", $this->page["Title"], $match)) {
if (preg_match_all('|\(([^\)]*)\)|', $match[1], $matches)) {
for ($i = 0; $i < count($matches[0]); ++$i) {
if (!preg_match('|^\d{4}$|', $matches[1][$i])) {
$this->main_movietypes[] = $matches[1][$i];
}
}
}
}
}
return $this->main_movietypes;
}
#---------------------------------------------------------------[ Runtime ]---
/**
* Get general runtime
* @return string runtime complete runtime string, e.g. "150 min / USA:153 min (director's cut)"
*/
protected function runtime_all()
{
if ($this->main_runtime == "") {
$this->getPage("Title");
if (@preg_match('!Runtime:</h4>\s*(.+?)\s*</div!ms', $this->page["Title"], $match)) {
$this->main_runtime = $match[1];
}
}
if ($this->main_runtime == "") {
$this->getPage("Technical");
if (@preg_match('!Runtime.*?<td>(.+?)</td!ms', $this->page["Technical"], $match)) {
$this->main_runtime = $match[1];
}
}
return $this->main_runtime;
}
/**
* Get overall runtime (first one mentioned on title page)
* @return int|null runtime in minutes (if set), NULL otherwise
* @see IMDB page / (TitlePage)
*/
public function runtime()
{
$jsonValue = isset($this->jsonLD()->duration) ? $this->jsonLD()->duration : (isset($this->jsonLD()->timeRequired) ? $this->jsonLD()->timeRequired : null);
if (isset($jsonValue) && preg_match('/PT(?:(\d+)H)?(?:(\d+)M)?/', $jsonValue, $matches)) {
$h = isset($matches[1]) ? intval($matches[1]) * 60 : 0;
$m = isset($matches[2]) ? intval($matches[2]) : 0;
return $h + $m;
}
// Fallback in case new json format aren't available
$runtimes = $this->runtimes();
if (!empty($runtimes[0]['time'])) {
return intval($runtimes[0]['time']);
}
return null;
}
/**
* Retrieve all runtimes and their descriptions
* @return array runtimes (array[0..n] of array[time,annotations]) where annotations is an array of comments meant to describe this cut
* @see IMDB page / (TitlePage)
*/
public function runtimes()
{
if (empty($this->movieruntimes)) {
$this->movieruntimes = array();
$rt = $this->runtime_all();
foreach (preg_split('!(\||<br>)!', strip_tags($rt, '<br>')) as $runtimestring) {
if (preg_match_all("/(\d+\s+hr\s+\d+\s+min)?\((\d+)\s+min\)|(\d+)\s+min/", $runtimestring, $matches,
PREG_SET_ORDER, 0)) {
$runtime = isset($matches[1][2]) ? $matches[1][2] : (isset($matches[0][3]) ? $matches[0][3] : 0);
$annotations = array();
if (preg_match_all("/\((?!\d+\s+min)(.+?)\)/", $runtimestring, $matches)) {
$annotations = $matches[1];
}
$this->movieruntimes[] = array(
"time" => $runtime,
"country" => '',
"comment" => '',
"annotations" => $annotations
);
}
}
}
return $this->movieruntimes;
}
#----------------------------------------------------------[ Aspect Ratio ]---
/**
* Aspect Ratio of movie screen
* @return string ratio e.g. "2.35 : 1" or "" if there is no aspect ratio on imdb
* @see IMDB page / (TitlePage)
*/
public function aspect_ratio()
{
if (empty($this->aspectratio)) {
$page = $this->getPage("Title");
if (preg_match('!<h4 class="inline">Aspect Ratio:</h4>\s*(.*?)\s+</div>!ims', $page, $match)) {
$this->aspectratio = $match[1];
}
}
return $this->aspectratio;
}
#----------------------------------------------------------[ Movie Rating ]---
/** Get movie rating
* @return string rating current rating as given by IMDB site
* @see IMDB page / (TitlePage)
*/
public function rating()
{
return isset($this->jsonLD()->aggregateRating->ratingValue) ? $this->jsonLD()->aggregateRating->ratingValue : '';
}
/**
* Return number of votes for this movie
* @return int
* @see IMDB page / (TitlePage)
*/
public function votes()
{
return isset($this->jsonLD()->aggregateRating->ratingCount) ? $this->jsonLD()->aggregateRating->ratingCount : 0;
}
/**
* Rating out of 100 on metacritic
* @return int|null
*/
public function metacriticRating()
{
$page = $this->getPage('Title');
if (preg_match('!"metacriticScore.+>\n.+?(\d+)!im', $page, $match)) {
return (int)$match[1];
}
return null;
}
/**
* Number of reviews on metacritic
* @return null
* @deprecated since version 3.1.3
*/
public function metacriticNumReviews()
{
return null;
}
#------------------------------------------------------[ Movie Comment(s) ]---
/** Get movie main comment (from title page)
* @return string comment full text of movie comment from the movies main page
* @see IMDB page / (TitlePage)
*/
public function comment()
{
// this stuff whent into a frame in 2011! _ajax/iframe?component=footer
if ($this->main_comment == "") {
$this->getPage("Title");
if (@preg_match('!<div class\="user-comments">\s*(.*?)\s*<hr\s*/>\s*<div class\="yn"!ms',
$this->page["Title"], $match)) {
$this->main_comment = preg_replace("/a href\=\"\//i", "a href=\"https://" . $this->imdbsite . "/",
$match[1]);
}
$this->main_comment = str_replace("https://i.media-imdb.com/images/showtimes",
$this->imdb_img_url . "/showtimes", $this->main_comment);
}
return $this->main_comment;
}
/** Get movie main comment (from title page - split-up variant)
* @return array comment array[string title, string date, array author, string comment]; author: array[string url, string name]
* @see IMDB page / (TitlePage)
*/
public function comment_split()
{
if (empty($this->split_comment)) {
if ($this->main_comment == "") {
$comm = $this->comment();
}
if (@preg_match('!<strong[^>]*>(.*?)</strong>.*?<div class="comment-meta">\s*(.*?)\s*\|\s*by\s*(.*?</a>).*?<p[^>]*>(.*?)\s*</div!ims',
$this->main_comment, $match)) {
@preg_match('!href="(.*?)"[^>]*><span[^>]*>(.*)</span!i', $match[3], $author);
$this->split_comment = array(
"title" => $match[1],
"date" => $match[2],
"author" => array("url" => $author[1], "name" => $author[2]),
"comment" => trim($match[4])
);
} elseif (@preg_match('!<div class="comment-meta">\s*<meta itemprop="datePublished" content=".+?">\s*(.{10,20})\s*\|\s*by\s*(.*?)\s*–.*?<div>\s*(.*?)\s*</div>!ims',
$this->main_comment, $match)) {
@preg_match('!href="(.*?)">(.*)</a!i', $match[2], $author);
$this->split_comment = array(
'title' => '',
'date' => $match[1],
'author' => array("url" => $author[1], "name" => $author[2]),
"comment" => trim($match[3])
);
}
}
return $this->split_comment;
}
#-------------------------------------------------------[ Recommendations ]---
/**
* Get recommended movies (People who liked this...also liked)
* @return array recommendations (array[title,imdbid,year,endyear,rating,votes])
* @see IMDB page / (TitlePage)
*/
public function movie_recommendations()
{
if (empty($this->movierecommendations)) {
$doc = new \DOMDocument();
@$doc->loadHTML($this->getPage("Title"));
$xp = new \DOMXPath($doc);
$cells = $xp->query("//div[@id=\"title_recs\"]//div[@class=\"rec-title\"]");
/** @var \DOMElement $cell */
foreach ($cells as $cell) {
if (preg_match('!tt(\d+)!', $cell->getElementsByTagName('a')->item(0)->getAttribute('href'), $ref)) {
$movie['title'] = trim($cell->getElementsByTagName('a')->item(0)->nodeValue);
$movie['imdbid'] = $ref[1];
$span = $xp->query($cell->getNodePath() . '//span[@class="nobr"]')->item(0)->nodeValue;
$years = preg_replace('/[^0-9]/', '', $span);
if (strlen($years) > 4) {
$movie['year'] = substr($years, 0, 4);
$movie['endyear'] = substr($years, 4);
} else {
$movie['year'] = $years;
$movie['endyear'] = "";
}
if (preg_match('/([0-9.,]{1,3})\/10\s*\(([0-9\s.,]+)/iu', $cell->parentNode->getElementsByTagName('div')->item(3)->getAttribute('title'),
$rating)) {
$movie['rating'] = str_replace(',', '.', $rating[1]);
$movie['votes'] = preg_replace('/[^0-9]/', '', $rating[2]);
} else {
$movie['rating'] = -1;
$movie['votes'] = -1;
}
$this->movierecommendations[] = $movie;
}
}
}
return $this->movierecommendations;
}
#--------------------------------------------------------------[ Keywords ]---
/** Get the keywords for the movie
* @return array keywords
* @see IMDB page / (TitlePage)
*/
public function keywords()
{
if (empty($this->main_keywords)) {
$this->getPage("Title");
if (preg_match_all('!href="/search/keyword[^>]+?>\s*<span[^>]*?>(.*?)</span></a>!', $this->page["Title"],
$matches)) {
$this->main_keywords = $matches[1];
}
}
return $this->main_keywords;
}
#--------------------------------------------------------[ Language Stuff ]---
/** Get movies original language
* @return string language
* @brief There is not really a main language on the IMDB sites (yet), so this
* simply returns the first one
* @see IMDB page / (TitlePage)
*/
public function language()
{
if (empty($this->main_language)) {
if (empty($this->langs)) {
$this->langs = $this->languages();
}
if (!empty($this->langs)) {
$this->main_language = $this->langs[0];
}
}
return $this->main_language;
}
/** Get all languages this movie is available in
* @return array languages (array[0..n] of strings)
* @see IMDB page / (TitlePage)
*/
public function languages()
{
if (empty($this->langs)) {
if (preg_match_all('!href="/search/title\?.+?primary_language=([^&]*)[^>]*>\s*(.*?)\s*</a>(\s+\((.*?)\)|)!m',
$this->getPage("Title"), $matches)) {
$this->langs = $matches[2];
$mc = count($matches[2]);
for ($i = 0; $i < $mc; $i++) {
$this->langs_full[] = array(
'name' => $matches[2][$i],
'code' => $matches[1][$i],
'comment' => trim($matches[4][$i])
);
}
}
}
return $this->langs;
}
/** Get all languages this movie is available in, including details
* @return array languages (array[0..n] of array[string name, string code, string comment], code being the ISO-Code)
* @see IMDB page / (TitlePage)
*/
public function languages_detailed()
{
if (empty($this->langs_full)) {
$this->languages();
}
return $this->langs_full;
}
#--------------------------------------------------------------[ Genre(s) ]---
/** Get the movies main genre
* Since IMDB.COM does not really now a "Main Genre", this simply means the
* first mentioned genre will be returned.
* @return string genre first of the genres listed on the movies main page
* @brief There is not really a main genre on the IMDB sites (yet), so this
* simply returns the first one
* @see IMDB page / (TitlePage)
*/
public function genre()
{
if (empty($this->main_genre)) {
if (empty($this->moviegenres)) {
$this->genres();
}
if (!empty($this->moviegenres)) {
$this->main_genre = $this->moviegenres[0];
}
}
return $this->main_genre;
}
/** Get all genres the movie is registered for
* @return array genres (array[0..n] of strings)
* @see IMDB page / (TitlePage)
*/
public function genres()
{
if (empty($this->moviegenres)) {
$genres = isset($this->jsonLD()->genre) ? $this->jsonLD()->genre : array();
if (!is_array($genres)) {
$genres = (array)$genres;
}
$this->moviegenres = $genres;
}
if (empty($this->moviegenres)) {
if (@preg_match('!Genres:</h4>(.*?)</div!ims', $this->page["Title"], $match)) {
if (@preg_match_all('!href="[^>]+?>\s*(.*?)\s*<!', $match[1], $matches)) {
$this->moviegenres = $matches[1];
}
}
}
return $this->moviegenres;
}
#----------------------------------------------------------[ Color format ]---
/**
* Get the colours this movie was shot in.
* e.g. Color, Black and White
* @return array colors (array[0..1] of strings)
* @see IMDB page / (TitlePage)
*/
public function colors()
{
if (empty($this->moviecolors)) {
$this->getPage("Title");
if (preg_match_all("|/search/title\?colors=[^>]+?>\s?(.*?)</a|", $this->page["Title"], $matches)) {
$this->moviecolors = $matches[1];
}
}
return $this->moviecolors;
}
#---------------------------------------------------------------[ Creator ]---
/**
* Get the creator(s) of a TV Show
* @return array creator (array[0..n] of array[name,imdb])
* @see IMDB page / (TitlePage)
*/
public function creator()
{
$result = array();
if ($this->jsonLD()->{'@type'} === 'TVSeries' && isset($this->jsonLD()->creator) && is_array($this->jsonLD()->creator)) {
foreach ($this->jsonLD()->creator as $creator) {
if ($creator->{'@type'} === 'Person') {
$result[] = array(
'name' => $creator->name,
'imdb' => rtrim(str_replace('/name/nm', '', $creator->url), '/')
);
}
}
}
return $result;
}
#---------------------------------------------------------------[ Tagline ]---
/** Get the main tagline for the movie
* @return string tagline
* @see IMDB page / (TitlePage)
*/
public function tagline()
{
if ($this->main_tagline == "") {
$this->getPage("Title");
if (@preg_match('!Taglines:</h4>\s*(.*?)\s*<!ims', $this->page["Title"], $match)) {
$this->main_tagline = trim($match[1]);
}
}
return $this->main_tagline;
}
#---------------------------------------------------------------[ Seasons ]---
/** Get the number of seasons or 0 if not a series
* @return int seasons number of seasons
* @see IMDB page / (TitlePage)
*/
public function seasons()
{
if ($this->seasoncount == -1) {
$this->getPage("Title");
if (preg_match_all('|href="/title/tt\d{7,8}/episodes\?season=\d+.*?"\s*>(\d+)</a>|Ui', $this->page["Title"],
$matches)) {
$this->seasoncount = $matches[1][0];
} else {
$this->seasoncount = 0;
}
if (preg_match_all('|href="/title/tt\d{7,8}/episodes\?season\=unknown"\s*>unknown</a>|Ui',
$this->page["Title"], $matches)) {
$this->seasoncount += count($matches[0]);
}
}
return $this->seasoncount;
}
/**
* Is this title serialised (a tv show)?
* This could be the show page or an episode
* @return boolean
* @see IMDB page / (TitlePage)
*/
public function is_serial()
{
if (isset($this->isSerial)) {
return $this->isSerial;
}
return $this->isSerial = (bool)preg_match('|href="/title/tt\d{7,8}/episodes\?|i', $this->getPage("Title"));
}
// @TODO do this properly and make this public. Perhaps it could just come from self::movietype() ?
protected function isEpisode()
{
return $this->is_serial();
}
/**
* Title of the episode
* @return string
*/
public function episodeTitle()
{
if (!$this->isEpisode()) {
return "";
}
return $this->jsonLD()->name;
}
private function populateEpisodeSeasonEpisode()
{
if (!isset($this->episodeEpisode) || !isset($this->episodeSeason)) {
if (preg_match("@<div class=\"bp_heading\">Season (\d+) <span class=\"ghost\">\|</span> Episode (\d+)</div>@",
$this->getPage("Title"), $matches)) {
$this->episodeSeason = (int)$matches[1];
$this->episodeEpisode = (int)$matches[2];
} else {
$this->episodeSeason = 0;
$this->episodeEpisode = 0;
}
}
}
/**
* @return int 0 if not available
*/
public function episodeSeason()
{
if (!$this->isEpisode()) {
return 0;
}
$this->populateEpisodeSeasonEpisode();
return $this->episodeSeason;
}
/**
* @return int 0 if not available
*/
public function episodeEpisode()
{
if (!$this->isEpisode()) {
return 0;
}
$this->populateEpisodeSeasonEpisode();
return $this->episodeEpisode;
}
/**
* The date when this episode aired for the first time
* @return string An ISO 8601 date e.g. 2015-01-01. Will be an empty string if not available
*/
public function episodeAirDate()
{
if (!$this->isEpisode()) {
return "";
}
if (!isset($this->jsonLD()->datePublished)) {
return '';
}
return $this->jsonLD()->datePublished;
}
/**
* Extra information about this episode (if this title is an episode)
* @return array [imdbid,seriestitle,episodetitle,season,episode,airdate]
* e.g.
* <pre>
* array (
* 'imdbid' => '0303461', // ImdbID of the show
* 'seriestitle' => 'Firefly', // Title of the show
* 'episodetitle' => 'The Train Job',// Title of this episode
* 'season' => 1,
* 'episode' => 1,
* 'airdate' => '2002-09-20',
* )
* </pre>
* @see IMDB page / (TitlePage)
*/
public function get_episode_details()
{
if (!$this->isEpisode()) {
return array();
}
$seriesRegex = '!<div class="titleParent">\s*<a\s+href="/title/tt(?<seriesimdbid>\d{7,8})[^"]+"\s*title="(?<seriestitle>[^"]+)"!ims';
if (preg_match($seriesRegex, $this->getPage("Title"), $match)) {
return array(
"imdbid" => $match['seriesimdbid'],
"seriestitle" => $match['seriestitle'],
"episodetitle" => $this->episodeTitle(),
"season" => $this->episodeSeason(),
"episode" => $this->episodeEpisode(),
"airdate" => $this->episodeAirDate()
);
} else {
return array(); // no success
}
}
#--------------------------------------------------------[ Plot (Outline) ]---
/** Get the main Plot outline for the movie
* @param boolean $fallback Fallback to storyline if we could not catch plotoutline
* @return string plotoutline
* @see IMDB page / (TitlePage)
*/
public function plotoutline($fallback = false)
{
if ($this->main_plotoutline == "") {
$page = $this->getPage("Title");
if (preg_match('!class="summary_text">\s*(.*?)\s*</div>!ims', $page, $match)) {
$this->main_plotoutline = trim($match[1]);
} elseif ($fallback) {
$this->main_plotoutline = $this->storyline();
}
}
$this->main_plotoutline = preg_replace('!\s*<a href="/title/tt\d{7,8}/(plotsummary|synopsis)[^>]*>See full (summary|synopsis).*$!i',
'', $this->main_plotoutline);
$this->main_plotoutline = preg_replace('#<a href="[^"]+"\s+>Add a Plot</a> »#', '',
$this->main_plotoutline);
return $this->main_plotoutline;
}
/** Get the Storyline for the movie
* @return string storyline
* @see IMDB page / (TitlePage)
*/
public function storyline()
{
if ($this->main_storyline == "") {
$page = $this->getPage("Title");
if (@preg_match('~Storyline</h2>.*?<div.*?<p>.*?<span>(.*?)</span>.*?</p>~ims', $page, $match)) {
$this->main_storyline = trim($match[1]);
}
}
return $this->main_storyline;
}
#--------------------------------------------------------[ Photo specific ]---
/** Setup cover photo (thumbnail and big variant)
* @return boolean success (TRUE if found, FALSE otherwise)
* @see IMDB page / (TitlePage)
*/
private function populatePoster()
{
if (isset($this->jsonLD()->image)) {
$this->main_poster = $this->jsonLD()->image;
}
preg_match('!<img [^>]+title="[^"]+Poster"[^>]+src="([^"]+)"[^>]+/>!ims', $this->getPage("Title"), $match);
if (!empty($match[1])) {
$this->main_poster_thumb = $match[1];
}
}
/**
* Get the poster/cover image URL
* @param boolean $thumb get the thumbnail (182x268) or the full sized image
* @return string|false photo (string URL if found, FALSE otherwise)
* @see IMDB page / (TitlePage)
*/
public function photo($thumb = true)
{
if (empty($this->main_poster)) {