PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/moviesquiddz/imdbphp/imdb_person.class.php

https://github.com/squiddz/moviesquiddz
PHP | 499 lines | 294 code | 33 blank | 172 comment | 83 complexity | 30ff029a6fe513039bbc189fb70bd59b 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_person.class.php 135 2008-05-20 22:48:18Z izzy $ */
  12. require_once (dirname(__FILE__)."/imdb_base.class.php");
  13. #=================================================[ The IMDB Person class ]===
  14. /** Accessing IMDB staff information
  15. * @package Api
  16. * @class imdb_person
  17. * @extends imdb_base
  18. * @author Izzy (izzysoft AT qumran DOT org)
  19. * @copyright 2008 by Itzchak Rehberg and IzzySoft
  20. * @version $Revision: 135 $ $Date: 2008-05-21 00:48:18 +0200 (Mi, 21 Mai 2008) $
  21. */
  22. class imdb_person extends imdb_base {
  23. #========================================================[ Common methods ]===
  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 "Name" : $urlname="/"; break;
  33. case "Bio" : $urlname="/bio"; break;
  34. default :
  35. $this->page[$wt] = "unknown page identifier";
  36. $this->debug_scalar("Unknown page identifier: $wt");
  37. return false;
  38. }
  39. return $urlname;
  40. }
  41. #--------------------------------------------------[ Start (over) / Reset ]---
  42. /** Reset page vars
  43. * @method private reset_vars
  44. */
  45. function reset_vars() {
  46. $this->page["Name"] = "";
  47. $this->page["Bio"] = "";
  48. // "Name" page:
  49. $this->main_photo = "";
  50. $this->fullname = "";
  51. $this->birthday = array();
  52. $this->actorsfilms = array();
  53. $this->producersfilms = array();
  54. $this->soundtrackfilms = array();
  55. $this->directorsfilms = array();
  56. $this->crewsfilms = array();
  57. $this->thanxfilms = array();
  58. $this->selffilms = array();
  59. $this->archivefilms = array();
  60. // "Bio" page:
  61. $this->birth_name = "";
  62. $this->nick_name = array();
  63. $this->bodyheight = array();
  64. $this->bio_bio = array();
  65. $this->bio_trivia = array();
  66. $this->bio_tm = array();
  67. $this->bio_salary = array();
  68. }
  69. #-----------------------------------------------------------[ Constructor ]---
  70. /** Initialize class
  71. * @constructor imdb_person
  72. * @param string id IMDBID to use for data retrieval
  73. */
  74. function imdb_person ($id) {
  75. $this->imdb_base($id);
  76. }
  77. #-----------------------------------------------[ URL to person main page ]---
  78. /** Set up the URL to the movie title page
  79. * @method main_url
  80. * @return string url full URL to the current movies main page
  81. */
  82. function main_url(){
  83. return "http://".$this->imdbsite."/name/nm".$this->imdbid()."/";
  84. }
  85. #=============================================================[ Main Page ]===
  86. #------------------------------------------------------------------[ Name ]---
  87. /** Get the name of the person
  88. * @method name
  89. * @return string name full name of the person
  90. */
  91. function name() {
  92. if (empty($this->fullname)) {
  93. if ($this->page["Name"] == "") $this->openpage ("Name","person");
  94. if (preg_match("/<title>(.*?)<\/title>/i",$this->page["Name"],$match)) {
  95. $this->fullname = trim($match[1]);
  96. }
  97. }
  98. return $this->fullname;
  99. }
  100. #------------------------------------------------------------------[ Born ]---
  101. /** Get Birthday
  102. * @method born
  103. * @return array birthday [day,month.year,place]
  104. */
  105. function born() {
  106. if (empty($this->birthday)) {
  107. if ($this->page["Name"] == "") $this->openpage ("Name","person");
  108. if (preg_match("/Date of Birth:<\/h5>\s*<a href=\"\/OnThisDay\?day\=(\d{1,2})&month\=(.*?)\">.*?<a href\=\"\/BornInYear\?(\d{4}).*?href\=\"\/BornWhere\?.*?\">(.*?)<\/a>/ms",$this->page["Name"],$match))
  109. $this->birthday = array("day"=>$match[1],"month"=>$match[2],"year"=>$match[3],"place"=>$match[4]);
  110. }
  111. return $this->birthday;
  112. }
  113. #------------------------------------------------------------------[ Died ]---
  114. /** Get Deathday
  115. * @method died
  116. * @return array deathday [day,month.year,place,cause]
  117. */
  118. function died() {
  119. if (empty($this->deathday)) {
  120. if ($this->page["Name"] == "") $this->openpage ("Name","person");
  121. if (preg_match("/Date of Death:<\/h5>\s*<a href=\"\/OnThisDay\?day\=(\d{1,2})&month\=(.*?)\">.*?<a href\=\"\/DiedInYear\?(\d{4}).*?<\/a>\s*\,\s*(.*?)\s*(\((.*?)\)|)\s*<a class/ms",$this->page["Name"],$match))
  122. $this->deathday = array("day"=>$match[1],"month"=>$match[2],"year"=>$match[3],"place"=>$match[4],"cause"=>$match[6]);
  123. }
  124. return $this->deathday;
  125. }
  126. #--------------------------------------------------------[ Photo specific ]---
  127. /** Get cover photo
  128. * @method photo
  129. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  130. * bigger variant (400x600 - FALSE)
  131. * @return mixed photo (string url if found, FALSE otherwise)
  132. */
  133. function photo($thumb=true) {
  134. if (empty($this->main_photo)) {
  135. if ($this->page["Name"] == "") $this->openpage ("Name","person");
  136. if (preg_match('/\<a name="headshot".+"(http:\/\/.+\.jpg)".+<\/a>/',$this->page["Name"],$match)) {
  137. if ($thumb) $this->main_photo = $match[1];
  138. else $this->main_photo = str_replace('_SY140_SX100', '_SY600_SX400',$match[1]);
  139. } else {
  140. return FALSE;
  141. }
  142. }
  143. return $this->main_photo;
  144. }
  145. /** Save the photo to disk
  146. * @method savephoto
  147. * @param string path where to store the file
  148. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  149. * bigger variant (400x600 - FALSE)
  150. * @return boolean success
  151. */
  152. function savephoto ($path,$thumb=true) {
  153. $req = new IMDB_Request("");
  154. $photo_url = $this->photo ($thumb);
  155. if (!$photo_url) return FALSE;
  156. $req->setURL($photo_url);
  157. $req->sendRequest();
  158. if (strpos($req->getResponseHeader("Content-Type"),'image/jpeg') === 0
  159. || strpos($req->getResponseHeader("Content-Type"),'image/gif') === 0
  160. || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0 ){
  161. $fp = $req->getResponseBody();
  162. }else{
  163. $this->debug_scalar("<BR>*photoerror* ".$photo_url.": Content Type is '".$req->getResponseHeader("Content-Type")."'<BR>");
  164. return false;
  165. }
  166. $fp2 = fopen ($path, "w");
  167. if ((!$fp) || (!$fp2)){
  168. $this->debug_scalar("image error...<BR>");
  169. return false;
  170. }
  171. fputs ($fp2, $fp);
  172. return TRUE;
  173. }
  174. /** Get the URL for the movies cover photo
  175. * @method photo_localurl
  176. * @param optional boolean thumb get the thumbnail (100x140, default) or the
  177. * bigger variant (400x600 - FALSE)
  178. * @return mixed url (string URL or FALSE if none)
  179. */
  180. function photo_localurl($thumb=true){
  181. if ($thumb) $ext = ""; else $ext = "_big";
  182. if (!is_dir($this->photodir)) {
  183. $this->debug_scalar("<BR>***ERROR*** The configured image directory does not exist!<BR>");
  184. return false;
  185. }
  186. $path = $this->photodir."nm".$this->imdbid()."${ext}.jpg";
  187. if ( @fopen($path,"r")) return $this->photoroot."nm".$this->imdbid()."${ext}.jpg";
  188. if (!is_writable($this->photodir)) {
  189. $this->debug_scalar("<BR>***ERROR*** The configured image directory lacks write permission!<BR>");
  190. return false;
  191. }
  192. if ($this->savephoto($path,$thumb)) return $this->photoroot."nm".$this->imdbid()."${ext}.jpg";
  193. return false;
  194. }
  195. #----------------------------------------------------------[ Filmographie ]---
  196. /** Get filmography
  197. * @method private filmograf
  198. * @param ref array where to store the filmography
  199. * @param string type Which filmografie to retrieve ("actor",)
  200. */
  201. function filmograf(&$res,$type) {
  202. if ($this->page["Name"] == "") $this->openpage ("Name","person");
  203. if (preg_match("/<a name=\"$type\"(.*?)<\/div>/msi",$this->page["Name"],$match)) {
  204. if (preg_match_all("/<a(.*?)href=\"\/title\/tt(\d{7})\/\">(.*?)<\/a>\s*(\((\d{4})\)|)([^<]*?\.\.\.\.\s*<a href=\"\/character\/ch(\d{7})\/\">(.*?)<\/a>|([^<]*?|\s*<small>.*?<\/small>\s*)\.\.\.\.\s*(.*?)\s*<|)/i",$match[1],$matches)) {
  205. $mc = count($matches[0]);
  206. for ($i=0;$i<$mc;++$i) {
  207. if (empty($matches[8][$i])) $matches[8][$i] = $matches[10][$i];
  208. $res[] = array("mid"=>$matches[2][$i],"name"=>$matches[3][$i],"year"=>$matches[5][$i],"chid"=>$matches[7][$i],"chname"=>$matches[8][$i]);
  209. }
  210. }
  211. }
  212. }
  213. /** Get actors filmography
  214. * @method movies_actor
  215. * @return array array[0..n][mid,name,year,chid,chname], where chid is the
  216. * character IMDB ID, and chname the character name
  217. */
  218. function movies_actor() {
  219. if (empty($this->actorsfilms)) $this->filmograf($this->actorsfilms,"actor");
  220. return $this->actorsfilms;
  221. }
  222. /** Get producers filmography
  223. * @method movies_producer
  224. * @return array array[0..n][mid,name,year]
  225. */
  226. function movies_producer() {
  227. if (empty($this->producersfilms)) $this->filmograf($this->producersfilms,"producer");
  228. return $this->producersfilms;
  229. }
  230. /** Get directors filmography
  231. * @method movies_director
  232. * @return array array[0..n][mid,name,year]
  233. */
  234. function movies_director() {
  235. if (empty($this->directorsfilms)) $this->filmograf($this->directorsfilms,"director");
  236. return $this->directorsfilms;
  237. }
  238. /** Get soundtrack filmography
  239. * @method movies_soundtrack
  240. * @return array array[0..n][mid,name,year]
  241. */
  242. function movies_soundtrack() {
  243. if (empty($this->soundtrackfilms)) $this->filmograf($this->soundtrackfilms,"soundtrack");
  244. return $this->soundtrackfilms;
  245. }
  246. /** Get "Misc Crew" filmography
  247. * @method movies_crew
  248. * @return array array[0..n][mid,name,year]
  249. */
  250. function movies_crew() {
  251. if (empty($this->crewsfilms)) $this->filmograf($this->crewsfilms,"miscellaneousX20crew");
  252. return $this->crewsfilms;
  253. }
  254. /** Get "Thanx" filmography
  255. * @method movies_thanx
  256. * @return array array[0..n][mid,name,year]
  257. */
  258. function movies_thanx() {
  259. if (empty($this->thanxfilms)) $this->filmograf($this->thanxfilms,"thanks");
  260. return $this->thanxfilms;
  261. }
  262. /** Get "Self" filmography
  263. * @method movies_self
  264. * @return array array[0..n][mid,name,year,chid,chname], where chid is the
  265. * character IMDB ID, and chname the character name
  266. */
  267. function movies_self() {
  268. if (empty($this->selffilms)) $this->filmograf($this->selffilms,"self");
  269. return $this->selffilms;
  270. }
  271. /** Get "Archive Footage" filmography
  272. * @method movies_archive
  273. * @return array array[0..n][mid,name,year,chid,chname], where chid is the
  274. * character IMDB ID, and chname the character name
  275. */
  276. function movies_archive() {
  277. if (empty($this->archivefilms)) $this->filmograf($this->archivefilms,"archive");
  278. return $this->archivefilms;
  279. }
  280. #==================================================================[ /bio ]===
  281. #------------------------------------------------------------[ Birth Name ]---
  282. /** Get the birth name
  283. * @method birthname
  284. * @return string birthname
  285. */
  286. function birthname() {
  287. if (empty($this->birth_name)) {
  288. if ($this->page["Bio"] == "") $this->openpage ("Bio","person");
  289. if (preg_match("/Birth Name<\/h5>\s*\n(.*?)\n/m",$this->page["Bio"],$match))
  290. $this->birth_name = trim($match[1]);
  291. }
  292. return $this->birth_name;
  293. }
  294. #-------------------------------------------------------------[ Nick Name ]---
  295. /** Get the nick name
  296. * @method nickname
  297. * @return array nicknames array[0..n] of strings
  298. */
  299. function nickname() {
  300. if (empty($this->nick_name)) {
  301. if ($this->page["Bio"] == "") $this->openpage ("Bio","person");
  302. if (preg_match("/Nickname<\/h5>\s*\n(.*?)\n<h5>/ms",$this->page["Bio"],$match)) {
  303. $nicks = explode("<br/>",$match[1]);
  304. foreach ($nicks as $nick) {
  305. $nick = trim($nick);
  306. if (!empty($nick)) $this->nick_name[] = $nick;
  307. }
  308. }
  309. }
  310. return $this->nick_name;
  311. }
  312. #-----------------------------------------------------------[ Body Height ]---
  313. /** Get the body height
  314. * @method height
  315. * @return array [imperial,metric] height in feet and inch (imperial) an meters (metric)
  316. */
  317. function height() {
  318. if (empty($this->bodyheight)) {
  319. if ($this->page["Bio"] == "") $this->openpage ("Bio","person");
  320. if (preg_match("/Height<\/h5>\s*\n(.*?)\s*\((.*?)\)/m",$this->page["Bio"],$match)) {
  321. $this->bodyheight["imperial"] = trim($match[1]);
  322. $this->bodyheight["metric"] = trim($match[2]);
  323. }
  324. }
  325. return $this->bodyheight;
  326. }
  327. #----------------------------------------------------------------[ Spouse ]---
  328. /** Get spouse(s)
  329. * @method spouse
  330. * @return array [0..n] of array spouses [string imdb, string name, array from,
  331. * array to, string comment, string children], where from/to are array
  332. * [day,month,year], comment usually is "divorced" (ouch), children is
  333. * the number of children
  334. */
  335. function spouse() {
  336. if (empty($this->spouses)) {
  337. if ($this->page["Bio"] == "") $this->openpage ("Bio","person");
  338. $pos_s = strpos($this->page["Bio"],"<h5>Spouse</h5>");
  339. $pos_e = strpos($this->page["Bio"],"</table>",$pos_s);
  340. $block = substr($this->page["Bio"],$pos_s,$pos_e - $pos_s +8);
  341. if (@preg_match_all("/<tr>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>/ms",$block,$matches)) { // table lines
  342. $mc = count($matches[0]);
  343. for ($i=0;$i<$mc;++$i) {
  344. unset($tmp);
  345. if (preg_match("/href\=\"\/name\/nm(\d{7})\/\">(.*?)<\/a>/i",$matches[1][$i],$match)) { // col#1: MID + name
  346. $tmp["imdb"] = trim($match[1]);
  347. $tmp["name"] = trim($match[2]);
  348. } else {
  349. $tmp["name"] = trim($matches[1][$i]);
  350. }
  351. # if (preg_match("/href\=\"\/OnThisDay\?day\=(\d{1,2}).{1,5}month\=(.*?)\".*\"\/MarriedInYear\?(\d{4})\"/",$matches[2][$i],$match)) { // col#2: date (from)
  352. # if (preg_match("/href\=\"\/OnThisDay\?day\=(\d{1,2}).{1,5}month\=(.*?)\".*\"\/MarriedInYear\?(\d{4})\">\d{4}<\/a>(.* href\=\"\/OnThisDay\?day=(\d{1,2}).{1,5}month=(.*?)\".*<\/a>\s*(\d{4})|)/",$matches[2][$i],$match)) { // col#2: date from + to
  353. # if (preg_match("/href\=\"\/OnThisDay\?day\=(\d{1,2}).{1,5}month\=(.*?)\".*\"\/MarriedInYear\?(\d{4})\">\d{4}<\/a>(.* href\=\"\/OnThisDay\?day=(\d{1,2}).{1,5}month=(.*?)\".*<\/a>\s*(\d{4})\)|)\s*\((.*?)\)/",$matches[2][$i],$match)) { // col#2: date, comment
  354. if (preg_match("/href\=\"\/OnThisDay\?day\=(\d{1,2}).{1,5}month\=(.*?)\".*\"\/MarriedInYear\?(\d{4})\">\d{4}<\/a>(.* href\=\"\/OnThisDay\?day=(\d{1,2}).{1,5}month=(.*?)\".*<\/a>\s*(\d{4})\)|)\s*\((.*?)\)(\s*(\d+) child|)/",$matches[2][$i],$match)) { // col#2: date, children
  355. $tmp["from"] = array("day"=>$match[1],"month"=>$match[2],"year"=>$match[3]);
  356. $tmp["to"] = array("day"=>$match[5],"month"=>$match[6],"year"=>$match[7]);
  357. $tmp["comment"] = $match[8];
  358. $tmp["children"] = $match[10];
  359. }
  360. $this->spouses[] = $tmp;
  361. }
  362. }
  363. }
  364. return $this->spouses;
  365. }
  366. #---------------------------------------------------------------[ MiniBio ]---
  367. /** Get the person's mini bio
  368. * @method bio
  369. * @return array bio array [0..n] of array[string desc, array author[url,name]]
  370. */
  371. function bio () {
  372. if (empty($this->bio_bio)) {
  373. if ( $this->page["Bio"] == "" ) $this->openpage ("Bio","person");
  374. if ( $this->page["Bio"] == "cannot open page" ) return array(); // no such page
  375. if (@preg_match_all('|<h5>Mini Biography</h5>\s*(.+)\s+.+\s+(.+)|',$this->page["Bio"],$matches)) {
  376. for ($i=0;$i<count($matches[0]);++$i) {
  377. $bio_bio["desc"] = str_replace("href=\"/name/nm","href=\"http://".$this->imdbsite."/name/nm",
  378. str_replace("href=\"/title/tt","href=\"http://".$this->imdbsite."/title/tt",
  379. str_replace('/SearchBios','http://'.$this->imdbsite.'/SearchBios',$matches[1][$i])));
  380. $author = 'Written by '.(str_replace('/SearchBios','http://'.$this->imdbsite.'/SearchBios',$matches[2][$i]));
  381. if (@preg_match("/href\=\"(.*?)\">(.*?)<\/a>/",$author,$match)) {
  382. $bio_bio["author"]["url"] = $match[1][$i];
  383. $bio_bio["author"]["name"] = $match[2][$i];
  384. }
  385. $this->bio_bio[] = $bio_bio;
  386. unset($bio_bio,$author);
  387. }
  388. }
  389. }
  390. return $this->bio_bio;
  391. }
  392. #-----------------------------------------[ Helper to Trivia, Quotes, ... ]---
  393. /** Parse Trivia, Quotes, etc (same structs)
  394. * @method private parparse
  395. * @param string name
  396. * @param ref array res
  397. */
  398. function parparse($name,&$res) {
  399. if ( $this->page["Bio"] == "" ) $this->openpage ("Bio","person");
  400. $pos_s = strpos($this->page["Bio"],"<h5>$name</h5>");
  401. $pos_e = strpos($this->page["Bio"],"<br",$pos_s);
  402. $block = substr($this->page["Bio"],$pos_s,$pos_e - $pos_s);
  403. if (preg_match_all("/<p>(.*?)<\/p>/ms",$block,$matches))
  404. foreach ($matches[1] as $match)
  405. $res[] = str_replace('href="/name/nm', 'href="http://'.$this->imdbsite.'/name/nm',
  406. str_replace('href="/title/tt','href="http://'.$this->imdbsite.'/title/tt',$match));
  407. }
  408. #----------------------------------------------------------------[ Trivia ]---
  409. /** Get the Trivia
  410. * @method trivia
  411. * @return array trivia array[0..n] of string
  412. */
  413. function trivia() {
  414. if (empty($this->bio_trivia)) $this->parparse("Trivia",$this->bio_trivia);
  415. return $this->bio_trivia;
  416. }
  417. #----------------------------------------------------------------[ Quotes ]---
  418. /** Get the Personal Quotes
  419. * @method quotes
  420. * @return array quotes array[0..n] of string
  421. */
  422. function quotes() {
  423. if (empty($this->bio_quotes)) $this->parparse("Personal Quotes",$this->bio_quotes);
  424. return $this->bio_quotes;
  425. }
  426. #------------------------------------------------------------[ Trademarks ]---
  427. /** Get the "trademarks" of the person
  428. * @method trademark
  429. * @return array trademarks array[0..n] of strings
  430. */
  431. function trademark() {
  432. if (empty($this->bio_tm)) $this->parparse("Trade Mark",$this->bio_tm);
  433. return $this->bio_tm;
  434. }
  435. #----------------------------------------------------------------[ Salary ]---
  436. /** Get the salary list
  437. * @method salary
  438. * @return array salary array[0..n] of array movie[strings imdb,name,year], string salary
  439. */
  440. function salary() {
  441. if (empty($this->bio_salary)) {
  442. if ( $this->page["Bio"] == "" ) $this->openpage ("Bio","person");
  443. $pos_s = strpos($this->page["Bio"],"<h5>Salary</h5>");
  444. $pos_e = strpos($this->page["Bio"],"</table",$pos_s);
  445. $block = substr($this->page["Bio"],$pos_s,$pos_e - $pos_s);
  446. if (preg_match_all("/<tr.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>/ms",$block,$matches)) { // for each table row
  447. $mc = count($matches[0]);
  448. for ($i=0;$i<$mc;++$i) {
  449. if (preg_match("/\/title\/tt(\d{7})\/\">(.*?)<\/a>\s*\((\d{4})\)/",$matches[1][$i],$match)) {
  450. $movie["imdb"] = $match[1];
  451. $movie["name"] = $match[2];
  452. $movie["year"] = $match[3];
  453. } else {
  454. $movie["name"] = $matches[1][$i];
  455. }
  456. $this->bio_salary[] = array("movie"=>$movie,"salary"=>$matches[2][$i]);
  457. }
  458. }
  459. }
  460. return $this->bio_salary;
  461. }
  462. } // end class imdb_person
  463. ?>