PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/moviesquiddz/imdbphp/imdb.class.php

https://github.com/squiddz/moviesquiddz
PHP | 1011 lines | 741 code | 45 blank | 225 comment | 183 complexity | 98db94b5c0f3864a25030e3f17c14895 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. #############################################################################
  3. # IMDBPHP (c) Giorgos Giagas & Itzchak Rehberg #
  4. # written by Giorgos Giagas #
  5. # extended & maintained by Itzchak Rehberg <izzysoft AT qumran DOT org> #
  6. # http://www.izzysoft.de/ #
  7. # ------------------------------------------------------------------------- #
  8. # This program is free software; you can redistribute and/or modify it #
  9. # under the terms of the GNU General Public License (see doc/LICENSE) #
  10. #############################################################################
  11. /* $Id: imdb.class.php 132 2008-05-20 21:16:42Z izzy $ */
  12. require_once (dirname(__FILE__)."/imdb_base.class.php");
  13. #=================================================[ The IMDB class itself ]===
  14. /** Accessing IMDB information
  15. * @package Api
  16. * @class imdb
  17. * @extends imdb_base
  18. * @author Georgos Giagas
  19. * @author Izzy (izzysoft AT qumran DOT org)
  20. * @copyright (c) 2002-2004 by Giorgos Giagas and (c) 2004-2008 by Itzchak Rehberg and IzzySoft
  21. * @version $Revision: 132 $ $Date: 2008-05-20 23:16:42 +0200 (Di, 20 Mai 2008) $
  22. */
  23. class imdb extends imdb_base {
  24. #-------------------------------------------------------------[ Open Page ]---
  25. /** Define page urls
  26. * @method private set_pagename
  27. * @param string wt internal name of the page
  28. * @return string urlname page URL
  29. */
  30. function set_pagename($wt) {
  31. switch ($wt){
  32. case "Title" : $urlname="/"; break;
  33. case "Credits" : $urlname="/fullcredits"; break;
  34. case "CrazyCredits": $urlname="/crazycredits"; break;
  35. case "Plot" : $urlname="/plotsummary"; break;
  36. case "Taglines" : $urlname="/taglines"; break;
  37. case "Episodes" : $urlname="/episodes"; break;
  38. case "Quotes" : $urlname="/quotes"; break;
  39. case "Trailers" : $urlname="/trailers"; break;
  40. case "Goofs" : $urlname="/goofs"; break;
  41. case "Trivia" : $urlname="/trivia"; break;
  42. case "Soundtrack" : $urlname="/soundtrack"; break;
  43. case "MovieConnections" : $urlname="/movieconnections"; break;
  44. case "ExtReviews" : $urlname="/externalreviews"; break;
  45. default :
  46. $this->page[$wt] = "unknown page identifier";
  47. $this->debug_scalar("Unknown page identifier: $wt");
  48. return false;
  49. }
  50. return $urlname;
  51. }
  52. #--------------------------------------------------[ Start (over) / Reset ]---
  53. /** Reset page vars
  54. * @method private reset_vars
  55. */
  56. function reset_vars() {
  57. $this->page["Title"] = "";
  58. $this->page["Credits"] = "";
  59. $this->page["CrazyCredits"] = "";
  60. $this->page["Amazon"] = "";
  61. $this->page["Goofs"] = "";
  62. $this->page["Trivia"] = "";
  63. $this->page["Plot"] = "";
  64. $this->page["Comments"] = "";
  65. $this->page["Quotes"] = "";
  66. $this->page["Taglines"] = "";
  67. $this->page["Plotoutline"] = "";
  68. $this->page["Trivia"] = "";
  69. $this->page["Directed"] = "";
  70. $this->page["Episodes"] = "";
  71. $this->page["Quotes"] = "";
  72. $this->page["Trailers"] = "";
  73. $this->page["MovieConnections"] = "";
  74. $this->page["ExtReviews"] = "";
  75. $this->akas = array();
  76. $this->countries = array();
  77. $this->crazy_credits = array();
  78. $this->credits_cast = array();
  79. $this->credits_composer = array();
  80. $this->credits_director = array();
  81. $this->credits_producer = array();
  82. $this->credits_writing = array();
  83. $this->extreviews = array();
  84. $this->goofs = array();
  85. $this->langs = array();
  86. $this->main_comment = "";
  87. $this->main_genre = "";
  88. $this->main_language = "";
  89. $this->main_photo = "";
  90. $this->main_thumb = "";
  91. $this->main_plotoutline = "";
  92. $this->main_rating = -1;
  93. $this->main_runtime = "";
  94. $this->main_title = "";
  95. $this->main_votes = -1;
  96. $this->main_year = -1;
  97. $this->main_tagline = "";
  98. $this->moviecolors = array();
  99. $this->movieconnections = array();
  100. $this->moviegenres = array();
  101. $this->moviequotes = array();
  102. $this->movieruntimes = array();
  103. $this->mpaas = array();
  104. $this->mpaa_justification = "";
  105. $this->plot_plot = array();
  106. $this->seasoncount = -1;
  107. $this->season_episodes = array();
  108. $this->sound = array();
  109. $this->soundtracks = array();
  110. $this->split_comment = array();
  111. $this->split_plot = array();
  112. $this->taglines = array();
  113. $this->trailers = array();
  114. $this->trivia = array();
  115. }
  116. #-----------------------------------------------------------[ Constructor ]---
  117. /** Initialize class
  118. * @constructor imdb
  119. * @param string id IMDBID to use for data retrieval
  120. */
  121. function imdb ($id) {
  122. $this->imdb_base($id);
  123. }
  124. #-----------------------------------------------[ URL to movies main page ]---
  125. /** Set up the URL to the movie title page
  126. * @method main_url
  127. * @return string url full URL to the current movies main page
  128. */
  129. function main_url(){
  130. return "http://".$this->imdbsite."/title/tt".$this->imdbid()."/";
  131. }
  132. #-------------------------------------------[ Movie title (name) and year ]---
  133. /** Setup title and year properties
  134. * @method private title_year
  135. */
  136. function title_year() {
  137. if ($this->page["Title"] == "") $this->openpage ("Title");
  138. if (@preg_match("/\<title\>(.*) \((\d{4}).*\)\<\/title\>/",$this->page["Title"],$match)) {
  139. $this->main_title = $match[1];
  140. $this->main_year = $match[2];
  141. }
  142. }
  143. /** Get movie title
  144. * @method title
  145. * @return string title movie title (name)
  146. */
  147. function title () {
  148. if ($this->main_title == "") $this->title_year();
  149. return $this->main_title;
  150. }
  151. /** Get year
  152. * @method year
  153. * @return string year
  154. */
  155. function year () {
  156. if ($this->main_year == -1) $this->title_year();
  157. return $this->main_year;
  158. }
  159. #---------------------------------------------------------------[ Runtime ]---
  160. /** Get general runtime
  161. * @method private runtime_all
  162. * @return string runtime complete runtime string, e.g. "150 min / USA:153 min (director's cut)"
  163. */
  164. function runtime_all() {
  165. if ($this->main_runtime == "") {
  166. if ($this->page["Title"] == "") $this->openpage ("Title");
  167. if (@preg_match("/Runtime:\<\/h5\>\n(.*?)\n/m",$this->page["Title"],$match))
  168. $this->main_runtime = $match[1];
  169. }
  170. return $this->main_runtime;
  171. }
  172. /** Get overall runtime (first one mentioned on title page)
  173. * @method runtime
  174. * @return mixed string runtime in minutes (if set), NULL otherwise
  175. */
  176. function runtime() {
  177. if (empty($this->movieruntimes)) $runarr = $this->runtimes();
  178. else $runarr = $this->movieruntimes;
  179. if (isset($runarr[0]["time"])) return $runarr[0]["time"];
  180. return NULL;
  181. }
  182. /** Retrieve language specific runtimes
  183. * @method runtimes
  184. * @return array runtimes (array[0..n] of array[time,country,comment])
  185. */
  186. function runtimes(){
  187. if (empty($this->movieruntimes)) {
  188. if ($this->runtime_all() == "") return array();
  189. if (preg_match_all("/[\/ ]*((\D*?):|)([\d]+?) min( \((.*?)\)|)/",$this->main_runtime,$matches))
  190. for ($i=0;$i<count($matches[0]);++$i) $this->movieruntimes[] = array("time"=>$matches[3][$i],"country"=>$matches[2][$i],"comment"=>$matches[5][$i]);
  191. }
  192. return $this->movieruntimes;
  193. }
  194. #----------------------------------------------------------[ Movie Rating ]---
  195. /** Setup votes
  196. * @method private rate_vote
  197. */
  198. function rate_vote() {
  199. if ($this->page["Title"] == "") $this->openpage ("Title");
  200. if (@preg_match("/\<b\>(.*?)\/(.*?)\<\/b\>\s*\n\s*<small\>\(\<a href\=\"ratings\"\>([\d\,]+)/m",$this->page["Title"],$match)) {
  201. $this->main_rating = $match[1];
  202. $this->main_votes = $match[3];
  203. } else {
  204. $this->main_rating = 0;
  205. $this->main_votes = 0;
  206. }
  207. }
  208. /** Get movie rating
  209. * @method rating
  210. * @return string rating current rating as given by IMDB site
  211. */
  212. function rating () {
  213. if ($this->main_rating == -1) $this->rate_vote();
  214. return $this->main_rating;
  215. }
  216. /** Return votes for this movie
  217. * @method votes
  218. * @return string votes count of votes for this movie
  219. */
  220. function votes () {
  221. if ($this->main_votes == -1) $this->rate_vote();
  222. return $this->main_votes;
  223. }
  224. #------------------------------------------------------[ Movie Comment(s) ]---
  225. /** Get movie main comment (from title page)
  226. * @method comment
  227. * @return string comment full text of movie comment from the movies main page
  228. */
  229. function comment () {
  230. if ($this->main_comment == "") {
  231. if ($this->page["Title"] == "") $this->openpage ("Title");
  232. if (@preg_match("/\<div class\=\"comment\"(.*?)(\<b\>.*?)\<div class\=\"yn\"/ms",$this->page["Title"],$match))
  233. $this->main_comment = preg_replace("/a href\=\"\//i","a href=\"http://".$this->imdbsite."/",$match[2]);
  234. }
  235. return $this->main_comment;
  236. }
  237. /** Get movie main comment (from title page - split-up variant)
  238. * @method comment_split
  239. * @return array comment array[string title, string date, array author, string comment]; author: array[string url, string name]
  240. */
  241. function comment_split() {
  242. if (empty($this->split_comment)) {
  243. if ($this->main_comment == "") $comm = $this->comment();
  244. if (@preg_match("/<b>(.*?)<\/b>, (.*)<br>.*?<a href=\"(.*)\">(.*?)<\/a>.*<p>(.*?)<\/p>/ms",$this->main_comment,$match))
  245. $this->split_comment = array("title"=>$match[1],"date"=>$match[2],"author"=>array("url"=>$match[3],"name"=>$match[4]),"comment"=>trim($match[5]));
  246. }
  247. return $this->split_comment;
  248. }
  249. #--------------------------------------------------------[ Language Stuff ]---
  250. /** Get movies original language
  251. * @method language
  252. * @return string language
  253. * @brief There is not really a main language on the IMDB sites (yet), so this
  254. * simply returns the first one
  255. */
  256. function language () {
  257. if ($this->main_language == "") {
  258. if (empty($this->langs)) $langs = $this->languages();
  259. $this->main_language = $this->langs[0];
  260. }
  261. return $this->main_language;
  262. }
  263. /** Get all langauges this movie is available in
  264. * @method languages
  265. * @return array languages (array[0..n] of strings)
  266. */
  267. function languages () {
  268. if (empty($this->langs)) {
  269. if ($this->page["Title"] == "") $this->openpage ("Title");
  270. if (preg_match_all("/\/Sections\/Languages\/.*?>\s*(.*?)\s*</m",$this->page["Title"],$matches))
  271. $this->langs = $matches[1];
  272. }
  273. return $this->langs;
  274. }
  275. #--------------------------------------------------------------[ Genre(s) ]---
  276. /** Get the movies main genre
  277. * Since IMDB.COM does not really now a "Main Genre", this simply means the
  278. * first mentioned genre will be returned.
  279. * @method genre
  280. * @return string genre first of the genres listed on the movies main page
  281. * @brief There is not really a main genre on the IMDB sites (yet), so this
  282. * simply returns the first one
  283. */
  284. function genre () {
  285. if (empty($this->main_genre)) {
  286. if (empty($this->moviegenres)) $genres = $this->genres();
  287. if (!empty($genres)) $this->main_genre = $this->moviegenres[0];
  288. }
  289. return $this->main_genre;
  290. }
  291. /** Get all genres the movie is registered for
  292. * @method genres
  293. * @return array genres (array[0..n] of strings)
  294. */
  295. function genres () {
  296. if (empty($this->moviegenres)) {
  297. if ($this->page["Title"] == "") $this->openpage ("Title");
  298. if (preg_match_all("/\<a href\=\"\/Sections\/Genres\/[\w\-]+\/\"\>(.*?)\<\/a\>/",$this->page["Title"],$matches))
  299. $this->moviegenres = $matches[1];
  300. }
  301. return $this->moviegenres;
  302. }
  303. #----------------------------------------------------------[ Color format ]---
  304. /** Get colors
  305. * @method colors
  306. * @return array colors (array[0..1] of strings)
  307. */
  308. function colors () {
  309. if (empty($this->moviecolors)) {
  310. if ($this->page["Title"] == "") $this->openpage ("Title");
  311. if (preg_match_all("/\/List\?color-info.*?>\s*(.*?)</",$this->page["Title"],$matches))
  312. $this->moviecolors = $matches[1];
  313. }
  314. return $this->moviecolors;
  315. }
  316. #---------------------------------------------------------------[ Tagline ]---
  317. /** Get the main tagline for the movie
  318. * @method tagline
  319. * @return string tagline
  320. */
  321. function tagline () {
  322. if ($this->main_tagline == "") {
  323. if ($this->page["Title"] == "") $this->openpage ("Title");
  324. if (@preg_match("/Tagline:\<\/h5\>\s*\n(.*?)(<\/div|<a class=\"tn15more)/ms",$this->page["Title"],$match)) {
  325. $this->main_tagline = trim($match[1]);
  326. }
  327. }
  328. return $this->main_tagline;
  329. }
  330. #---------------------------------------------------------------[ Seasons ]---
  331. /** Get the number of seasons or 0 if not a series
  332. * @method seasons
  333. * @return int seasons number of seasons
  334. */
  335. function seasons() {
  336. if ( $this->seasoncount == -1 ) {
  337. if ( $this->page["Title"] == "" ) $this->openpage("Title");
  338. if ( preg_match_all('|<a href="episodes#season-\d+">(\d+)</a>|Ui',$this->page["Title"],$matches) ) {
  339. $this->seasoncount = count($matches[0]);
  340. } else {
  341. $this->seasoncount = 0;
  342. }
  343. }
  344. return $this->seasoncount;
  345. }
  346. #--------------------------------------------------------[ Plot (Outline) ]---
  347. /** Get the main Plot outline for the movie
  348. * @method plotoutline
  349. * @return string plotoutline
  350. */
  351. function plotoutline () {
  352. if ($this->main_plotoutline == "") {
  353. if ($this->page["Title"] == "") $this->openpage ("Title");
  354. if (@preg_match("/Plot:\<\/h5\>\s*\n(.*?)(<\/div|\||<a class=\"tn15more)/ms",$this->page["Title"],$match)) {
  355. $this->main_plotoutline = trim($match[1]);
  356. }
  357. }
  358. return $this->main_plotoutline;
  359. }
  360. #--------------------------------------------------------[ Photo specific ]---
  361. /** Setup cover photo (thumbnail and big variant)
  362. * @method thumbphoto
  363. * @return boolean success (TRUE if found, FALSE otherwise)
  364. */
  365. function thumbphoto() {
  366. if ($this->page["Title"] == "") $this->openpage ("Title");
  367. preg_match("/\<a name=\"poster\"(.*?)\<img (.*?) src\=\"(.*?)\"/",$this->page["Title"],$match);
  368. if (empty($match[3])) return FALSE;
  369. $this->main_thumb = $match[3];
  370. $this->main_photo = str_replace('_SY140_SX100', '_SY600_SX400',$match[3]);
  371. return true;
  372. }
  373. /** Get cover photo
  374. * @method photo
  375. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  376. * bigger variant (400x600 - FALSE)
  377. * @return mixed photo (string url if found, FALSE otherwise)
  378. */
  379. function photo($thumb=true) {
  380. if (empty($this->main_photo)) $this->thumbphoto();
  381. if (empty($this->main_photo)) return false;
  382. if ($thumb) return $this->main_thumb;
  383. return $this->main_photo;
  384. }
  385. /** Save the photo to disk
  386. * @method savephoto
  387. * @param string path where to store the file
  388. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  389. * bigger variant (400x600 - FALSE)
  390. * @return boolean success
  391. */
  392. function savephoto ($path,$thumb=true) {
  393. $req = new IMDB_Request("");
  394. $photo_url = $this->photo ($thumb);
  395. if (!$photo_url) return FALSE;
  396. $req->setURL($photo_url);
  397. $req->sendRequest();
  398. if (strpos($req->getResponseHeader("Content-Type"),'image/jpeg') === 0
  399. || strpos($req->getResponseHeader("Content-Type"),'image/gif') === 0
  400. || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0 ){
  401. $fp = $req->getResponseBody();
  402. }else{
  403. $this->debug_scalar("<BR>*photoerror* ".$photo_url.": Content Type is '".$req->getResponseHeader("Content-Type")."'<BR>");
  404. return false;
  405. }
  406. $fp2 = fopen ($path, "w");
  407. if ((!$fp) || (!$fp2)){
  408. $this->debug_scalar("image error...<BR>");
  409. return false;
  410. }
  411. fputs ($fp2, $fp);
  412. return TRUE;
  413. }
  414. /** Get the URL for the movies cover photo
  415. * @method photo_localurl
  416. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  417. * bigger variant (400x600 - FALSE)
  418. * @return mixed url (string URL or FALSE if none)
  419. */
  420. function photo_localurl($thumb=true){
  421. if ($thumb) $ext = ""; else $ext = "_big";
  422. if (!is_dir($this->photodir)) {
  423. $this->debug_scalar("<BR>***ERROR*** The configured image directory does not exist!<BR>");
  424. return false;
  425. }
  426. $path = $this->photodir.$this->imdbid()."${ext}.jpg";
  427. if ( @fopen($path,"r")) return $this->photoroot.$this->imdbid()."${ext}.jpg";
  428. if (!is_writable($this->photodir)) {
  429. $this->debug_scalar("<BR>***ERROR*** The configured image directory lacks write permission!<BR>");
  430. return false;
  431. }
  432. if ($this->savephoto($path,$thumb)) return $this->photoroot.$this->imdbid()."${ext}.jpg";
  433. return false;
  434. }
  435. #-------------------------------------------------[ Country of Production ]---
  436. /** Get country of production
  437. * @method country
  438. * @return array country (array[0..n] of string)
  439. */
  440. function country () {
  441. if (empty($this->countries)) {
  442. if ($this->page["Title"] == "") $this->openpage ("Title");
  443. $this->countries = array();
  444. if (preg_match_all("/\/Sections\/Countries\/\w+\/\"\>\s*(.*?)<\/a/m",$this->page["Title"],$matches))
  445. for ($i=0;$i<count($matches[0]);++$i) $this->countries[$i] = $matches[1][$i];
  446. }
  447. return $this->countries;
  448. }
  449. #------------------------------------------------------------[ Movie AKAs ]---
  450. /** Get movies alternative names
  451. * @method alsoknow
  452. * @return array aka array[0..n] of array[title,year,country,comment]; searching
  453. * on akas.imdb.com will add "lang" (2-char language code) to the array
  454. * for localized names, "comment" will hold additional countries listed
  455. * along for these as well as comments: As these things are quite mixed
  456. * up on the imdb sites, it's hard to tell what is an additional country
  457. * and what is a comment...
  458. */
  459. function alsoknow () {
  460. if (empty($this->akas)) {
  461. if ($this->page["Title"] == "") $this->openpage ("Title");
  462. $ak_s = strpos ($this->page["Title"], "Also Known As:</h5>");
  463. if ($ak_s>0) $ak_s += 19;
  464. if ($ak_s == 0) $ak_s = strpos ($this->page["Title"], "Alternativ:");
  465. if ($ak_s == 0) return array();
  466. $alsoknow_end = strpos ($this->page["Title"], "</div>", $ak_s);
  467. $alsoknow_all = substr($this->page["Title"], $ak_s, $alsoknow_end - $ak_s);
  468. $aka_arr = explode("<br>",str_replace("\n","",$alsoknow_all));
  469. foreach ($aka_arr as $aka) {
  470. $aka = trim($aka);
  471. if (strpos('class="tn15more"',$aka)>0) break; // end of list
  472. if (empty($aka)) continue;
  473. if (!preg_match("/\(/",$aka)) $this->akas[] = array("title"=>$aka,"year"=>"","country"=>"","comment"=>"");
  474. elseif (preg_match("/<i class=\"transl\">([^\[\(]+?) (\(\d{4}\) |)(\([^\[]+)\s*\[(.*?)\]<\/i>/",$aka,$match)) { // localized variants on akas.imdb.com
  475. if (preg_match_all("/\((.*?)\)/",$match[3],$countries)) {
  476. $country = $countries[1][0]; $comment = "";
  477. for ($i=1;$i<count($countries[0]);++$i) $comment .= ", ".$countries[1][$i];
  478. } else $country = $comment = "";
  479. $this->akas[] = array("title"=>$match[1],"year"=>$match[2],"country"=>$country,"comment"=>substr($comment,2),"lang"=>$match[4]);
  480. } elseif (preg_match("/(.*?) (\(\d{4}\) |)\((.*?)\)(.*?(\(.*\))|)/",$aka,$match)) {
  481. if (preg_match_all("/\((.*?)\)/",$match[5],$comments)) {
  482. $comm = $comments[1][0];
  483. for ($i=1;$i<count($comments[0]);++$i) $comm .= ", ".$comments[1][$i];
  484. } else $comm = "";
  485. $this->akas[] = array("title"=>$match[1],"year"=>$match[2],"country"=>$match[3],"comment"=>$comm);
  486. }
  487. }
  488. }
  489. return $this->akas;
  490. }
  491. #---------------------------------------------------------[ Sound formats ]---
  492. /** Get sound formats
  493. * @method sound
  494. * @return array sound (array[0..n] of strings)
  495. */
  496. function sound () {
  497. if (empty($this->sound)) {
  498. if ($this->page["Title"] == "") $this->openpage ("Title");
  499. if (preg_match_all("/\/List\?sound.*?>\s*(.*?)</",$this->page["Title"],$matches))
  500. $this->sound = $matches[1];
  501. }
  502. return $this->sound;
  503. }
  504. #-------------------------------------------------------[ MPAA / PG / FSK ]---
  505. /** Get the MPAA data (also known as PG or FSK)
  506. * @method mpaa
  507. * @return array mpaa (array[country]=rating)
  508. */
  509. function mpaa () {
  510. if (empty($this->mpaas)) {
  511. if ($this->page["Title"] == "") $this->openpage ("Title");
  512. if (preg_match_all("/\/List\?certificates.*?>\s*(.*?):(.*?)</",$this->page["Title"],$matches)) {
  513. $cc = count($matches[0]);
  514. for ($i=0;$i<$cc;++$i) $this->mpaas[$matches[1][$i]] = $matches[2][$i];
  515. }
  516. }
  517. return $this->mpaas;
  518. }
  519. #----------------------------------------------------[ MPAA justification ]---
  520. /** Find out the reason for the MPAA rating
  521. * @method mpaa_reason
  522. * @return string reason why the movie was rated such
  523. */
  524. function mpaa_reason () {
  525. if (empty($this->mpaa_justification)) {
  526. if ($this->page["Title"] == "") $this->openpage ("Title");
  527. if (preg_match("/href=\"\/mpaa\"\>.*<\/h5\>\s*(.*?)\s*<\/div/",$this->page["Title"],$match))
  528. $this->mpaa_justification = trim($match[1]);
  529. }
  530. return $this->mpaa_justification;
  531. }
  532. #-----------------------------------------------------[ /plotsummary page ]---
  533. /** Get the movies plot(s)
  534. * @method plot
  535. * @return array plot (array[0..n] of strings)
  536. */
  537. function plot () {
  538. if (empty($this->plot_plot)) {
  539. if ( $this->page["Plot"] == "" ) $this->openpage ("Plot");
  540. if ( $this->page["Plot"] == "cannot open page" ) return array(); // no such page
  541. if (preg_match_all("/p class=\"plotpar\">(.*?)<\/p>/",str_replace("\n"," ",$this->page["Plot"]),$matches))
  542. for ($i=0;$i<count($matches[0]);++$i)
  543. $this->plot_plot[$i] = preg_replace('/<a href=\"\/SearchPlotWriters/i','<a href="http://'.$this->imdbsite.'/SearchPlotWriters/',$matches[1][$i]);
  544. }
  545. return $this->plot_plot;
  546. }
  547. /** Get the movie plot(s) - split-up variant
  548. * @method plot_split
  549. * @return array array[0..n] of array[string plot,array author] - where author consists of string name and string url
  550. */
  551. function plot_split() {
  552. if (empty($this->split_plot)) {
  553. if (empty($this->plot_plot)) $plots = $this->plot();
  554. for ($i=0;$i<count($this->plot_plot);++$i) {
  555. if (preg_match("/(.*?)<i>.*<a href=\"(.*?)\">(.*?)<\/a>/",$this->plot_plot[$i],$match))
  556. $this->split_plot[] = array("plot"=>$match[1],"author"=>array("name"=>$match[3],"url"=>$match[2]));
  557. }
  558. }
  559. return $this->split_plot;
  560. }
  561. #--------------------------------------------------------[ /taglines page ]---
  562. /** Get all available taglines for the movie
  563. * @method taglines
  564. * @return array taglines (array[0..n] of strings)
  565. */
  566. function taglines () {
  567. if (empty($this->taglines)) {
  568. if ( $this->page["Taglines"] == "" ) $this->openpage ("Taglines");
  569. if ( $this->page["Taglines"] == "cannot open page" ) return array(); // no such page
  570. if (preg_match_all("/<p>(.*?)<\/p><hr/",$this->page["Taglines"],$matches))
  571. $this->taglines = $matches[1];
  572. }
  573. return $this->taglines;
  574. }
  575. #-----------------------------------------------------[ /fullcredits page ]---
  576. /** Get rows for a given table on the page
  577. * @method private get_table_rows
  578. * @param string html
  579. * @param string table_start
  580. * @return mixed rows (FALSE if table not found, array[0..n] of strings otherwise)
  581. * @see used by the methods director, cast, writing, producer, composer
  582. */
  583. function get_table_rows ( $html, $table_start ){
  584. $row_s = strpos ( $html, ">".$table_start."<");
  585. $row_e = $row_s;
  586. if ( $row_s == 0 ) return FALSE;
  587. $endtable = strpos($html, "</table>", $row_s);
  588. if (preg_match_all("/<tr>(.*?)<\/tr>/",substr($html,$row_s,$endtable - $row_s),$matches)) {
  589. $mc = count($matches[1]);
  590. for ($i=0;$i<$mc;++$i) if ( strncmp( trim($matches[1][$i]), "<td valign=",10) == 0 ) $rows[] = $matches[1][$i];
  591. }
  592. return $rows;
  593. }
  594. /** Get rows for the cast table on the page
  595. * @method private get_table_rows_cast
  596. * @param string html
  597. * @param string table_start
  598. * @return mixed rows (FALSE if table not found, array[0..n] of strings otherwise)
  599. * @see used by the method cast
  600. */
  601. function get_table_rows_cast ( $html, $table_start ){
  602. $row_s = strpos ( $html, '<table class="cast">');
  603. $row_e = $row_s;
  604. if ( $row_s == 0 ) return FALSE;
  605. $endtable = strpos($html, "</table>", $row_s);
  606. if (preg_match_all("/<tr.*?(<td class=\"nm\".*?)<\/tr>/",substr($html,$row_s,$endtable - $row_s),$matches))
  607. return $matches[1];
  608. return array();
  609. }
  610. /** Get content of table row cells
  611. * @method private get_row_cels
  612. * @param string row (as returned by imdb::get_table_rows)
  613. * @return array cells (array[0..n] of strings)
  614. * @see used by the methods director, cast, writing, producer, composer
  615. */
  616. function get_row_cels ( $row ){
  617. if (preg_match_all("/<td.*?>(.*?)<\/td>/",$row,$matches)) return $matches[1];
  618. return array();
  619. }
  620. /** Get the IMDB ID from a names URL
  621. * @method private get_imdbname
  622. * @param string href url to the staff members IMDB page
  623. * @return string IMDBID of the staff member
  624. * @see used by the methods director, cast, writing, producer, composer
  625. */
  626. function get_imdbname( $href){
  627. if ( strlen( $href) == 0) return $href;
  628. $name_s = 17;
  629. $name_e = strpos ( $href, '"', $name_s);
  630. if ( $name_e != 0) return substr( $href, $name_s, $name_e -1 - $name_s);
  631. else return $href;
  632. }
  633. /** Get the director(s) of the movie
  634. * @method director
  635. * @return array director (array[0..n] of arrays[imdb,name,role])
  636. */
  637. function director () {
  638. if (empty($this->credits_director)) {
  639. if ( $this->page["Credits"] == "" ) $this->openpage ("Credits");
  640. if ( $this->page["Credits"] == "cannot open page" ) return array(); // no such page
  641. }
  642. $director_rows = $this->get_table_rows($this->page["Credits"], "Directed by");
  643. for ( $i = 0; $i < count ($director_rows); $i++){
  644. $cels = $this->get_row_cels ($director_rows[$i]);
  645. if (!isset ($cels[0])) return array();
  646. $dir["imdb"] = $this->get_imdbname($cels[0]);
  647. $dir["name"] = strip_tags($cels[0]);
  648. $role = trim(strip_tags($cels[2]));
  649. if ( $role == "") $dir["role"] = NULL;
  650. else $dir["role"] = $role;
  651. $this->credits_director[$i] = $dir;
  652. }
  653. return $this->credits_director;
  654. }
  655. /** Get the actors
  656. * @method cast
  657. * @return array cast (array[0..n] of arrays[imdb,name,role])
  658. */
  659. function cast () {
  660. if (empty($this->credits_cast)) {
  661. if ( $this->page["Credits"] == "" ) $this->openpage ("Credits");
  662. if ( $this->page["Credits"] == "cannot open page" ) return array(); // no such page
  663. }
  664. $cast_rows = $this->get_table_rows_cast($this->page["Credits"], "Cast");
  665. for ( $i = 0; $i < count ($cast_rows); $i++){
  666. $cels = $this->get_row_cels ($cast_rows[$i]);
  667. if (!isset ($cels[0])) return array();
  668. $dir["imdb"] = $this->get_imdbname($cels[0]);
  669. $dir["name"] = strip_tags($cels[0]);
  670. $role = strip_tags($cels[2]);
  671. if ( $role == "") $dir["role"] = NULL;
  672. else $dir["role"] = $role;
  673. $this->credits_cast[$i] = $dir;
  674. }
  675. return $this->credits_cast;
  676. }
  677. /** Get the writer(s)
  678. * @method writing
  679. * @return array writers (array[0..n] of arrays[imdb,name,role])
  680. */
  681. function writing () {
  682. if (empty($this->credits_writing)) {
  683. if ( $this->page["Credits"] == "" ) $this->openpage ("Credits");
  684. if ( $this->page["Credits"] == "cannot open page" ) return array(); // no such page
  685. }
  686. $this->credits_writing = array();
  687. $writing_rows = $this->get_table_rows($this->page["Credits"], "Writing credits");
  688. for ( $i = 0; $i < count ($writing_rows); $i++){
  689. $cels = $this->get_row_cels ($writing_rows[$i]);
  690. if ( count ( $cels) > 2){
  691. $wrt["imdb"] = $this->get_imdbname($cels[0]);
  692. $wrt["name"] = strip_tags($cels[0]);
  693. $role = strip_tags($cels[2]);
  694. if ( $role == "") $wrt["role"] = NULL;
  695. else $wrt["role"] = $role;
  696. $this->credits_writing[$i] = $wrt;
  697. }
  698. }
  699. return $this->credits_writing;
  700. }
  701. /** Obtain the producer(s)
  702. * @method producer
  703. * @return array producer (array[0..n] of arrays[imdb,name,role])
  704. */
  705. function producer () {
  706. if (empty($this->credits_producer)) {
  707. if ( $this->page["Credits"] == "" ) $this->openpage ("Credits");
  708. if ( $this->page["Credits"] == "cannot open page" ) return array(); // no such page
  709. }
  710. $this->credits_producer = array();
  711. $producer_rows = $this->get_table_rows($this->page["Credits"], "Produced by");
  712. for ( $i = 0; $i < count ($producer_rows); $i++){
  713. $cels = $this->get_row_cels ($producer_rows[$i]);
  714. if ( count ( $cels) > 2){
  715. $wrt["imdb"] = $this->get_imdbname($cels[0]);
  716. $wrt["name"] = strip_tags($cels[0]);
  717. $role = strip_tags($cels[2]);
  718. if ( $role == "") $wrt["role"] = NULL;
  719. else $wrt["role"] = $role;
  720. $this->credits_producer[$i] = $wrt;
  721. }
  722. }
  723. return $this->credits_producer;
  724. }
  725. /** Obtain the composer(s) ("Original Music by...")
  726. * @method composer
  727. * @return array composer (array[0..n] of arrays[imdb,name,role])
  728. */
  729. function composer () {
  730. if (empty($this->credits_composer)) {
  731. if ( $this->page["Credits"] == "" ) $this->openpage ("Credits");
  732. if ( $this->page["Credits"] == "cannot open page" ) return array(); // no such page
  733. }
  734. $this->credits_composer = array();
  735. $composer_rows = $this->get_table_rows($this->page["Credits"], "Original Music by");
  736. for ( $i = 0; $i < count ($composer_rows); $i++){
  737. $cels = $this->get_row_cels ($composer_rows[$i]);
  738. if ( count ( $cels) > 2){
  739. $wrt["imdb"] = $this->get_imdbname($cels[0]);
  740. $wrt["name"] = strip_tags($cels[0]);
  741. $role = strip_tags($cels[2]);
  742. if ( $role == "") $wrt["role"] = NULL;
  743. else $wrt["role"] = $role;
  744. $this->credits_composer[$i] = $wrt;
  745. }
  746. }
  747. return $this->credits_composer;
  748. }
  749. #----------------------------------------------------[ /crazycredits page ]---
  750. /** Get the Crazy Credits
  751. * @method crazy_credits
  752. * @return array crazy_credits (array[0..n] of string)
  753. */
  754. function crazy_credits() {
  755. if (empty($this->crazy_credits)) {
  756. if (empty($this->page["CrazyCredits"])) $this->openpage("CrazyCredits");
  757. if ( $this->page["CrazyCredits"] == "cannot open page" ) return array(); // no such page
  758. $tag_s = strpos ($this->page["CrazyCredits"],"<li><tt>");
  759. $tag_e = strpos ($this->page["CrazyCredits"],"</ul>",$tag_s);
  760. $cred = str_replace ("<br>"," ",substr ($this->page["CrazyCredits"],$tag_s, $tag_e - $tag_s));
  761. $cred = str_replace (" "," ",str_replace ("\n"," ",$cred));
  762. if (preg_match_all("/<li><tt>(.*?)<\/tt><\/li>/",$cred,$matches))
  763. $this->crazy_credits = $matches[1];
  764. }
  765. return $this->crazy_credits;
  766. }
  767. #--------------------------------------------------------[ /episodes page ]---
  768. /** Get the series episode(s)
  769. * @method episodes
  770. * @return array episodes (array[0..n] of array[0..m] of array[imdbid,title,airdate,plot])
  771. */
  772. function episodes() {
  773. if ( $this->seasons() == 0 ) return null;
  774. if ( empty($this->season_episodes) ) {
  775. if ( $this->page["Episodes"] == "" ) $this->openpage("Episodes");
  776. if ( $this->page["Episodes"] == "cannot open page" ) return array(); // no such page
  777. if ( preg_match_all('|<h4>Season (\d+), Episode (\d+): <a href="/title/tt(\d{7})/">(.*)</a></h4><b>Original Air Date: (.*)</b><br>(.*)<br/>\s*<br/>|Ui',$this->page["Episodes"],$matches) ) {
  778. for ( $i = 0 ; $i < count($matches[0]); $i++ ) {
  779. $this->season_episodes[$matches[1][$i]][$matches[2][$i]] = array("imdbid" => $matches[3][$i],"title" => $matches[4][$i], "airdate" => $matches[5][$i], "plot" => $matches[6][$i]);
  780. }
  781. }
  782. }
  783. return $this->season_episodes;
  784. }
  785. #-----------------------------------------------------------[ /goofs page ]---
  786. /** Get the goofs
  787. * @method goofs
  788. * @return array goofs (array[0..n] of array[type,content]
  789. */
  790. function goofs() {
  791. if (empty($this->goofs)) {
  792. if (empty($this->page["Goofs"])) $this->openpage("Goofs");
  793. if ($this->page["Goofs"] == "cannot open page") return array(); // no such page
  794. $tag_s = strpos($this->page["Goofs"],'<ul class="trivia">');
  795. $tag_e = strrpos($this->page["Goofs"],'<ul class="trivia">'); // maybe more than one
  796. $tag_e = strrpos($this->page["Goofs"],"</ul>");
  797. $goofs = substr($this->page["Goofs"],$tag_s,$tag_e - $tag_s);
  798. if (preg_match_all("/<li><b>(.*?)<\/b>(.*?)<br><br><\/li>/",$goofs,$matches)) {
  799. $gc = count($matches[1]);
  800. for ($i=0;$i<$gc;++$i) $this->goofs[] = array("type"=>$matches[1][$i],"content"=>$matches[2][$i]);
  801. }
  802. }
  803. return $this->goofs;
  804. }
  805. #----------------------------------------------------------[ /quotes page ]---
  806. /** Get the quotes for a given movie
  807. * @method quotes
  808. * @return array quotes (array[0..n] of string)
  809. */
  810. function quotes() {
  811. if ( empty($this->moviequotes) ) {
  812. if ( $this->page["Quotes"] == "" ) $this->openpage("Quotes");
  813. if ( $this->page["Quotes"] == "cannot open page" ) return array(); // no such page
  814. if (preg_match_all("/<a name=\"qt.*?<\/a>\s*(.*?)<hr/",str_replace("\n"," ",$this->page["Quotes"]),$matches))
  815. foreach ($matches[1] as $match) $this->moviequotes[] = str_replace('href="/name/','href="http://'.$this->imdbsite.'/name/',$match);
  816. }
  817. return $this->moviequotes;
  818. }
  819. #--------------------------------------------------------[ /trailers page ]---
  820. /** Get the trailer URLs for a given movie
  821. * @method trailers
  822. * @return array trailers (array[0..n] of string)
  823. */
  824. function trailers() {
  825. if ( empty($this->trailers) ) {
  826. if ( $this->page["Trailers"] == "" ) $this->openpage("Trailers");
  827. if ( $this->page["Trailers"] == "cannot open page" ) return array(); // no such page
  828. $tag_s = strpos($this->page["Trailers"], '<div class="video-gallery">');
  829. if (!empty($tag_s)) { // trailers on the IMDB site itself
  830. $tag_e = strpos($this->page["Trailers"],"</a>\n</div",$tag_s);
  831. $trail = substr($this->page["Trailers"], $tag_s, $tag_e - $tag_s +1);
  832. if (preg_match_all("/<a href=\"(\/rg\/VIDEO_TITLE.*?)\">/",$trail,$matches))
  833. for ($i=0;$i<count($matches[0]);++$i) $this->trailers[] = "http://".$this->imdbsite.$matches[1][$i];
  834. }
  835. $tag_s = strpos($this->page["Trailers"], "<h3>Trailers on Other Sites</h3>");
  836. if (empty($tag_s)) return FALSE;
  837. $tag_e = strpos($this->page["Trailers"], "<h3>Related Links</h3>", $tag_s);
  838. $trail = substr($this->page["Trailers"], $tag_s, $tag_e - $tag_s);
  839. if (preg_match_all("/<a href=\"(.*?)\">/",$trail,$matches))
  840. $this->trailers = array_merge($this->trailers,$matches[1]);
  841. }
  842. return $this->trailers;
  843. }
  844. #----------------------------------------------------------[ /trivia page ]---
  845. /** Get the trivia info
  846. * @method trivia
  847. * @return array trivia (array[0..n] string
  848. */
  849. function trivia() {
  850. if (empty($this->trivia)) {
  851. if (empty($this->page["Trivia"])) $this->openpage("Trivia");
  852. if ($this->page["Trivia"] == "cannot open page") return array(); // no such page
  853. $tag_s = strpos($this->page["Trivia"],'<ul class="trivia">');
  854. $tag_e = strrpos($this->page["Trivia"],'<ul class="trivia">'); // maybe more than one
  855. $tag_e = strrpos($this->page["Trivia"],"</ul>");
  856. $goofs = substr($this->page["Trivia"],$tag_s,$tag_e - $tag_s);
  857. if (preg_match_all("/<li>(.*?)<br><br><\/li>/",$goofs,$matches)) {
  858. $gc = count($matches[1]);
  859. for ($i=0;$i<$gc;++$i) $this->trivia[] = str_replace('href="/','href="http://'.$this->imdbsite."/",$matches[1][$i]);
  860. }
  861. }
  862. return $this->trivia;
  863. }
  864. #------------------------------------------------------[ /soundtrack page ]---
  865. /** Get the soundtrack listing
  866. * @method soundtrack
  867. * @return array soundtracks (array[0..n] of array(soundtrack,array[0..n] of credits)
  868. * @brief Usually, the credits array should hold [0] written by, [1] performed by.
  869. * But IMDB does not always stick to that - so in many cases it holds
  870. * [0] performed by, [1] courtesy of
  871. */
  872. function soundtrack() {
  873. if (empty($this->soundtracks)) {
  874. if (empty($this->page["Soundtrack"])) $this->openpage("Soundtrack");
  875. if ($this->page["Soundtrack"] == "cannot open page") return array(); // no such page
  876. if (preg_match_all("/<li>(.*?)<\/b><br>(.*?)<br>(.*?)<br>.*?<\/li>/",str_replace("\n"," ",$this->page["Soundtrack"]),$matches)) {
  877. $mc = count($matches[0]);
  878. for ($i=0;$i<$mc;++$i) $this->soundtracks[] = array("soundtrack"=>$matches[1][$i],"credits"=>array(
  879. str_replace('href="/','href="http://'.$this->imdbsite.'/',$matches[2][$i]),
  880. str_replace('href="/','href="http://'.$this->imdbsite.'/',$matches[3][$i])));
  881. }
  882. }
  883. return $this->soundtracks;
  884. }
  885. #-------------------------------------------------[ /movieconnection page ]---
  886. /** Parse connection block (used by method movieconnection only)
  887. * @method private parseConnection
  888. * @param string conn connection type
  889. * @return array [0..n] of array mid,name,year,comment - or empty array if not found
  890. */
  891. function parseConnection($conn) {
  892. $tag_s = strpos($this->page["MovieConnections"],"<h5>$conn</h5>");
  893. if (empty($tag_s)) return array(); // no such feature
  894. $tag_e = strpos($this->page["MovieConnections"],"<h5>",$tag_s+4);
  895. if (empty($tag_e)) $tag_e = strpos($this->page["MovieConnections"],"<hr/><h3>",$tag_s);
  896. $block = substr($this->page["MovieConnections"],$tag_s,$tag_e-$tag_s);
  897. if (preg_match_all("/\<a href=\"(.*?)\"\>(.*?)\<\/a\> \((\d{4})\)(.*\<br\/\>\&nbsp;-\&nbsp;(.*))?/",$block,$matches)) {
  898. $mc = count($matches[0]);
  899. for ($i=0;$i<$mc;++$i) {
  900. $mid = substr($matches[1][$i],9,strlen($matches[1][$i])-10); // isolate imdb id from url
  901. $arr[] = array("mid"=>$mid, "name"=>$matches[2][$i], "year"=>$matches[3][$i], "comment"=>$matches[5][$i]);
  902. }
  903. }
  904. return $arr;
  905. }
  906. /** Get connected movie information
  907. * @method movieconnection
  908. * @return array connections (versionOf, editedInto, followedBy, spinOff,
  909. * spinOffFrom, references, referenced, features, featured, spoofs,
  910. * spoofed - each an array of mid, name, year, comment or an empty
  911. * array if no connections of that type)
  912. */
  913. function movieconnection() {
  914. if (empty($this->movieconnections)) {
  915. if (empty($this->page["MovieConnections"])) $this->openpage("MovieConnections");
  916. if ($this->page["MovieConnections"] == "cannot open page") return array(); // no such page
  917. $this->movieconnections["versionOf"] = $this->parseConnection("Version of");
  918. $this->movieconnections["editedInto"] = $this->parseConnection("Edited into");
  919. $this->movieconnections["followedBy"] = $this->parseConnection("Followed By");
  920. $this->movieconnections["spinOffFrom"] = $this->parseConnection("Spin off from");
  921. $this->movieconnections["spinOff"] = $this->parseConnection("Spin off");
  922. $this->movieconnections["references"] = $this->parseConnection("References");
  923. $this->movieconnections["referenced"] = $this->parseConnection("Referenced in");
  924. $this->movieconnections["features"] = $this->parseConnection("Features");
  925. $this->movieconnections["featured"] = $this->parseConnection("Featured in");
  926. $this->movieconnections["spoofs"] = $this->parseConnection("Spoofs");
  927. $this->movieconnections["spoofed"] = $this->parseConnection("Spoofed in");
  928. }
  929. return $this->movieconnections;
  930. }
  931. #------------------------------------------------[ /externalreviews page ]---
  932. /** Get list of external reviews (if any)
  933. * @method extReviews
  934. * @return array [0..n] of array [url, desc] (or empty array if no data)
  935. */
  936. function extReviews() {
  937. if (empty($this->extreviews)) {
  938. if (empty($this->page["ExtReviews"])) $this->openpage("ExtReviews");
  939. if ($this->page["ExtReviews"] == "cannot open page") return array(); // no such page
  940. if (preg_match_all("/\<li\>\<a href=\"(.*?)\"\>(.*?)\<\/a\>/",$this->page["ExtReviews"],$matches)) {
  941. $mc = count($matches[0]);
  942. for ($i=0;$i<$mc;++$i) {
  943. $this->extreviews[$i] = array("url"=>$matches[1][$i], "desc"=>$matches[2][$i]);
  944. }
  945. }
  946. }
  947. return $this->extreviews;
  948. }
  949. } // end class imdb
  950. ?>