Guest User

Untitled

a guest
Dec 14th, 2010
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.82 KB | None | 0 0
  1. <?php
  2. /**
  3. * PHP IMDB.com SCRAPER
  4. *
  5. * This class can be used to retrieve data from IMDB.com with PHP.
  6. *
  7. * The technique used is called “web scraping”
  8. * (see http://en.wikipedia.org/wiki/Web_scraping for details).
  9. * Which means: If IMDB changes *anything* on their HTML, the script is going to
  10. * fail (even a single space might be enough).
  11. *
  12. * You might not know, but there is an IMDB API available. The problem?
  13. * You will have to pay at least $15.000 to use it. Great, thank you.
  14. *
  15. *
  16. * If you want to thank me for my work and the support, feel free to do this
  17. * through PayPal (use [email protected] as payment destination) or just
  18. * buy me a book at Amazon (http://www.amazon.de/wishlist/3IAUEEEY6GD20)
  19. * – thank you! :-)
  20. *
  21. *
  22. * @link http://fabian-beiner.de
  23. * @copyright 2010 Fabian Beiner
  24. * @author Fabian Beiner ([email protected])
  25. * @license MIT License
  26. *
  27. * @version 5.2.4 (December 4th, 2010)
  28. */
  29.  
  30. class IMDBException extends Exception {}
  31.  
  32. class IMDB {
  33. // Please set this to 'true' for debugging purposes only.
  34. const IMDB_DEBUG = false;
  35. // Define a timeout for the request of the IMDB page.
  36. const IMDB_TIMEOUT = 15;
  37.  
  38. // Regular expressions, I would not touch them. :)
  39. const IMDB_BUDGET = '~Budget:</h4> (.*)\(estimated\)~Ui';
  40. const IMDB_CAST = '~<td class="name">\s+<a\s+href="/name/nm(\d+)/">(.*)</a>\s+</td~Ui';
  41. //const IMDB_CHAR = '~<td class="character">\s+<div>\s+(.*)\s+</div>\s+</td~Ui';
  42. const IMDB_CHAR = '~<td class="character">(.*)</td~Ui';
  43. const IMDB_COUNTRY = '~<a href="/country/(\w+)">(.*)</a>~Ui';
  44. const IMDB_CREATOR = '~<h4 class="inline">\s+(Creator|Creators):\s+</h4>(.*)</div><div~Ui';
  45. const IMDB_DIRECTOR = '~<h4 class="inline">\s+(Director|Directors):\s+</h4>(.*)</div><div~Ui';
  46. const IMDB_GENRE = '~<a href="/genre/(.*)"~Ui';
  47. const IMDB_LANGUAGES = '~<a href="/language/(\w+)">(.*)</a>~Ui';
  48. const IMDB_LOCATION = '~<h4 class="inline">Filming Locations:</h4> <a href="/search/title\?locations=(.*)">(.*)</a>~Ui';
  49. const IMDB_MPAA = '~<h4>Motion Picture Rating \(<a href="/mpaa">MPAA</a>\)</h4>(.*) <span~Ui';
  50. const IMDB_NAME = '~href="/name/nm(\d+)/">(.*)</a>~Ui';
  51. const IMDB_PLOT = '~<h2>Storyline</h2><p>(.*)(<em class="nobr">|</p>)~Ui';
  52. const IMDB_POSTER = '~href="/media/(.*)"\s+><img src="(.*)"~Ui';
  53. const IMDB_RATING = '~<span class="rating-rating">(\d+\.\d+)<span>~Ui';
  54. const IMDB_REDIRECT = '~Location:\s(.*)~';
  55. const IMDB_RELEASE_DATE = '~Release Date:</h4>(.*)(<span|</div>)~Ui';
  56. const IMDB_RUNTIME = '~(\d+)\smin~Uis';
  57. const IMDB_SEASONS = '~<h4 class="inline">Season: </h4><span class="see-more inline">(.*)</div><div~Ui';
  58. const IMDB_SEARCH = '~<b>Media from&nbsp;<a href="/title/tt(\d+)/"~i';
  59. const IMDB_TAGLINE = '~<h4 class="inline">Taglines:</h4>(.*)(<[^>]+>)~Ui';
  60. const IMDB_TITLE = '~<title>(.*) \((.*)\).*~Ui';
  61. const IMDB_TITLE_ORIG = '~<span class="title-extra">(.*) <i>\(original title\)</i></span>~Ui';
  62. const IMDB_URL = '~http://(.*\.|.*)imdb.com/(t|T)itle(\?|/)(..\d+)~i';
  63. const IMDB_VOTES = '~>(\d+|\d+,\d+) votes</a>\)~Ui';
  64. const IMDB_WRITER = '~<h4 class="inline">\s+(Writer|Writers):(.*)</div><div~Ui';
  65.  
  66. // cURL cookie file
  67. private $_fCookie = false;
  68. // IMDB url
  69. private $_strUrl = NULL;
  70. // IMDB source
  71. private $_strSource = NULL;
  72. // IMDB cache
  73. private $_intCache = 0;
  74. // IMDB posters directory
  75. private $_bolPoster = NULL;
  76. // IMDB cache directory
  77. private $_bolCache = NULL;
  78. // Movie found?
  79. public $isReady = false;
  80.  
  81. /**
  82. * IMDB constructor.
  83. *
  84. * @param string $strSearch The movie name / IMDB url
  85. * @param integer $intCache The maximum age (in minutes) of a cache
  86. */
  87. public function __construct($strSearch, $intCache = 1440) {
  88. // Cookie path.
  89. if (function_exists('sys_get_temp_dir')) {
  90. $this->_fCookie = tempnam(sys_get_temp_dir(), 'imdb');
  91. if (IMDB::IMDB_DEBUG) echo '<b>- Path to cookie:</b> ' . $this->_fCookie . '<br>';
  92. }
  93. // Posters and cache directory existant?
  94. if (!file_exists(getcwd() . '/posters/')) {
  95. if (mkdir(getcwd() . '/posters/', 0777)) {
  96. $this->_bolPoster = true;
  97. }
  98. }
  99. elseif (is_writeable(getcwd() . '/posters/')) {
  100. $this->_bolPoster = true;
  101. }
  102. else {
  103. throw new IMDBException(getcwd() . '/posters/ is not writable!');
  104. }
  105. if (!file_exists(getcwd() . '/cache/')) {
  106. if (mkdir(getcwd() . '/cache/', 0777)) {
  107. $this->_bolCache = true;
  108. }
  109. }
  110. elseif (is_writeable(getcwd() . '/cache/')) {
  111. $this->_bolCache = true;
  112. }
  113. else {
  114. throw new IMDBException(getcwd() . '/cache/ is not writable!');
  115. }
  116. // cURL.
  117. if (!function_exists('curl_init')) {
  118. throw new IMDBException('You need PHP with cURL enabled to use this script!');
  119. }
  120. // Debug only.
  121. if (IMDB::IMDB_DEBUG) {
  122. error_reporting(E_ALL);
  123. ini_set('display_errors', '1');
  124. echo '<b>- Running:</b> IMDB::fetchUrl<br>';
  125. }
  126. // Set global cache and fetch the data.
  127. $this->_intCache = (int)$intCache;
  128. IMDB::fetchUrl($strSearch);
  129. }
  130.  
  131. /**
  132. * Regular expressions helper function.
  133. *
  134. * @param string $strContent The content to search in
  135. * @param string $strRegex The regular expression
  136. * @param integer $intIndex The index to return
  137. * @return string The match found
  138. * @return array The matches found
  139. */
  140. private function matchRegex($strContent, $strRegex, $intIndex = null) {
  141. $arrMatches = null;
  142. preg_match_all($strRegex, $strContent, $arrMatches);
  143. if ($arrMatches === FALSE) return;
  144. if ($intIndex != null && is_int($intIndex)) {
  145. if ($arrMatches[$intIndex]) {
  146. return $arrMatches[$intIndex][0];
  147. }
  148. return;
  149. }
  150. return $arrMatches;
  151. }
  152.  
  153. /**
  154. * Returns a shortened text.
  155. *
  156. * @param string $strText The text to shorten
  157. * @param integer $intLength The new length of the text
  158. */
  159. public function makeShort($strText, $intLength = 100) {
  160. $strText = trim($strText) . ' ';
  161. $strText = substr($strText, 0, $intLength);
  162. $strText = substr($strText, 0, strrpos($strText, ' '));
  163. return $strText . '&hellip;';
  164. }
  165.  
  166. /**
  167. * Fetch data from the given url.
  168. *
  169. * @param string $strSearch The movie name / IMDB url
  170. * @param string $strSave The path to the file
  171. * @return boolean
  172. */
  173. private function fetchUrl($strSearch) {
  174. // Remove whitespaces.
  175. $strSearch = trim($strSearch);
  176.  
  177. // Check for a valid IMDB URL and use it, if available.
  178. if ($strId = IMDB::matchRegex($strSearch, IMDB::IMDB_URL, 4)) {
  179. $this->_strUrl = 'http://www.imdb.com/title/tt' . preg_replace('~[\D]~', '', $strId) . '/';
  180. $this->isReady = true;
  181. }
  182. // Otherwise try to find one.
  183. else {
  184. $this->_strUrl = 'http://www.imdb.com/find?s=all&q=' . str_replace(' ', '+', $strSearch);
  185. // Check for cached redirects of this search.
  186. $fRedirect = getcwd() . '/cache/' . md5($this->_strUrl);
  187. if (file_exists($fRedirect) && is_readable($fRedirect)) {
  188. $fRedirect = file_get_contents($fRedirect);
  189. if (IMDB::IMDB_DEBUG) echo '<b>- Found an old redirect:</b> ' . $fRedirect . '<br>';
  190. $this->_strUrl = $fRedirect;
  191. $this->isReady = true;
  192. }
  193. }
  194.  
  195. $fCache = getcwd() . '/cache/' . md5($this->_strUrl) . '.cache';
  196.  
  197. // Check if there is a cache we can use.
  198. $bolNewRequest = false;
  199. if (file_exists($fCache)) {
  200. $intChanged = filemtime($fCache);
  201. $intNow = time();
  202. $intDiff = $intNow - $intChanged;
  203. $intCache = $this->_intCache * 60;
  204. if ($intCache >= $intDiff) {
  205. $bolNewRequest = true;
  206. }
  207. }
  208.  
  209. if ($bolNewRequest) {
  210. if (IMDB::IMDB_DEBUG) echo '<b>- Using cache for ' . $strSearch . ' from . ' . $fCache . '</b><br>';
  211. $this->_strSource = file_get_contents($fCache);
  212. return true;
  213. }
  214. // Check if cURL is installed.
  215. else {
  216. // Initialize and run the request.
  217. if (IMDB::IMDB_DEBUG) echo '<b>- Run cURL on:</b> ' . $this->_strUrl . '<br>';
  218. $oCurl = curl_init($this->_strUrl);
  219. curl_setopt_array($oCurl, array (
  220. CURLOPT_VERBOSE => FALSE,
  221. CURLOPT_HEADER => TRUE,
  222. CURLOPT_FRESH_CONNECT => TRUE,
  223. CURLOPT_RETURNTRANSFER => TRUE,
  224. CURLOPT_TIMEOUT => IMDB::IMDB_TIMEOUT,
  225. CURLOPT_CONNECTTIMEOUT => 0,
  226. CURLOPT_REFERER => 'http://www.google.com',
  227. CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)',
  228. CURLOPT_FOLLOWLOCATION => FALSE,
  229. CURLOPT_COOKIEFILE => $this->_fCookie
  230. ));
  231. $strOutput = curl_exec($oCurl);
  232. $this->_strSource = $strOutput;
  233.  
  234. // Check if the request actually worked.
  235. if ($strOutput === FALSE) {
  236. if (IMDB::IMDB_DEBUG) echo '<b>! cURL error:</b> ' . $_strUrl . '<br>';
  237. if (file_exists($fCache)) {
  238. $this->_strSource = file_get_contents($fCache);
  239. return true;
  240. }
  241. return;
  242. }
  243.  
  244. // Get returned information.
  245. $arrInfo = curl_getinfo($oCurl);
  246. curl_close($oCurl);
  247.  
  248. // Check if there is a redirect given (IMDB sometimes does not return 301 for this...).
  249. if ($strMatch = $this->matchRegex($strOutput, IMDB::IMDB_REDIRECT, 1)) {
  250. if (IMDB::IMDB_DEBUG) echo '<b>- Found a redirect:</b> ' . $strMatch . '<br>';
  251. // Try to save the redirect for later usage.
  252. $fRedirect = getcwd() . '/cache/' . md5($this->_strUrl);
  253. if (IMDB::IMDB_DEBUG) echo '<b>- Saved a new redirect:</b> ' . $fRedirect . '<br>';
  254. file_put_contents($fRedirect, $strMatch);
  255. // Run the cURL request again with the new url.
  256. IMDB::fetchUrl($strMatch);
  257. }
  258. // Lets assume the first search result is what we want. :)
  259. elseif ($strMatch = $this->matchRegex($strOutput, IMDB::IMDB_SEARCH, 1)) {
  260. $strMatch = 'http://www.imdb.com/title/tt' . $strMatch . '/';
  261. if (IMDB::IMDB_DEBUG) echo '<b>- Using the first search result:</b> ' . $strMatch . '<br>';
  262. // Try to save the redirect for later usage.
  263. $fRedirect = getcwd() . '/cache/' . md5($this->_strUrl);
  264. if (IMDB::IMDB_DEBUG) echo '<b>- Saved a new redirect:</b> ' . $fRedirect . '<br>';
  265. file_put_contents($fRedirect, $strMatch);
  266. // Run the cURL request again with the new url.
  267. IMDB::fetchUrl($strMatch);
  268. }
  269. // If it's not a redirect and the HTTP response is not 200 or 302, abort.
  270. elseif ($arrInfo['http_code'] != 200 && $arrInfo['http_code'] != 302) {
  271. if (IMDB::IMDB_DEBUG) echo '<b>- Wrong HTTP code received, aborting:</b> ' . $arrInfo['http_code'] . '<br>';
  272. return;
  273. }
  274.  
  275. // Set the global source.
  276. $this->_strSource = preg_replace('~(\r|\n|\r\n)~', '', $this->_strSource);
  277.  
  278. // Save cache.
  279. if (IMDB::IMDB_DEBUG) echo '<b>- Saved a new cache:</b> ' . $fCache . '<br>';
  280. file_put_contents($fCache, $this->_strSource);
  281.  
  282. return true;
  283. }
  284. return;
  285. }
  286.  
  287. /**
  288. * Save the image locally.
  289. *
  290. * @param string $_strUrl The URL to the image on imdb
  291. * @return string The local path to the image
  292. */
  293. private function saveImage($_strUrl) {
  294. $_strUrl = trim($_strUrl);
  295.  
  296. if (preg_match('/imdb-share-logo.gif/', $_strUrl)) {
  297. if (file_exists('posters/not-found.jpg')) {
  298. return 'posters/not-found.jpg';
  299. }
  300. return $_strUrl;
  301. }
  302.  
  303. $strFilename = getcwd() . '/posters/' . md5(IMDB::getTitle()) . '.jpg';
  304. if (file_exists($strFilename)) {
  305. return 'posters/' . md5(IMDB::getTitle()) . '.jpg';
  306. }
  307. if (function_exists('curl_init')) {
  308. $oCurl = curl_init($_strUrl);
  309. curl_setopt_array($oCurl, array (
  310. CURLOPT_VERBOSE => FALSE,
  311. CURLOPT_HEADER => FALSE,
  312. CURLOPT_RETURNTRANSFER => TRUE,
  313. CURLOPT_TIMEOUT => IMDB::IMDB_TIMEOUT,
  314. CURLOPT_CONNECTTIMEOUT => IMDB::IMDB_TIMEOUT,
  315. CURLOPT_REFERER => $_strUrl,
  316. CURLOPT_BINARYTRANSFER => TRUE));
  317. $sOutput = curl_exec($oCurl);
  318. curl_close($oCurl);
  319. $oFile = fopen($strFilename, 'x');
  320. fwrite($oFile, $sOutput);
  321. fclose($oFile);
  322. return 'posters/' . md5(IMDB::getTitle()) . '.jpg';
  323. } else {
  324. $oImg = imagecreatefromjpeg($_strUrl);
  325. imagejpeg($oImg, $strFilename);
  326. return 'posters/' . md5(IMDB::getTitle()) . '.jpg';
  327. }
  328. return $_strUrl;
  329. }
  330.  
  331. /**
  332. * Returns the budget.
  333. *
  334. * @return string The movie budget.
  335. */
  336. public function getBudget() {
  337. if ($this->isReady) {
  338. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_BUDGET, 1)) {
  339. return $strReturn;
  340. }
  341. return 'n/A';
  342. }
  343. return 'n/A';
  344. }
  345.  
  346. /**
  347. * Returns the cast.
  348. *
  349. * @return array The movie cast (default limited to 20).
  350. */
  351. public function getCast($intLimit = 20, $bolMore = true) {
  352. if ($this->isReady) {
  353. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_CAST);
  354. if (count($arrReturned[2])) {
  355. foreach ($arrReturned[2] as $i => $strName) {
  356. if ($i >= $intLimit) break;
  357. $arrReturn[] = $strName;
  358. }
  359. return implode(' / ', $arrReturn) . ($bolMore && (count($arrReturned[2]) > $intLimit) ? '&hellip;' : '');
  360. }
  361. return 'n/A';
  362. }
  363. return 'n/A';
  364. }
  365.  
  366. /**
  367. * Returns the cast as URL.
  368. *
  369. * @return array The movie cast as URL (default limited to 20).
  370. */
  371. public function getCastAsUrl($intLimit = 20, $bolMore = true) {
  372. if ($this->isReady) {
  373. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_CAST);
  374. if (count($arrReturned[2])) {
  375. foreach ($arrReturned[2] as $i => $strName) {
  376. if ($i >= $intLimit) break;
  377. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  378. }
  379. return implode(' / ', $arrReturn) . ($bolMore && (count($arrReturned[2]) > $intLimit) ? '&hellip;' : '');
  380. }
  381. return 'n/A';
  382. }
  383. return 'n/A';
  384. }
  385.  
  386. /**
  387. * Returns the cast and character.
  388. *
  389. * @return array The movie cast and character (default limited to 20).
  390. */
  391. public function getCastAndCharacter($intLimit = 20, $bolMore = true) {
  392. if ($this->isReady) {
  393. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_CAST);
  394. $arrChar = $this->matchRegex($this->_strSource, IMDB::IMDB_CHAR);
  395. if (count($arrReturned[2])) {
  396. foreach ($arrReturned[2] as $i => $strName) {
  397. if ($i >= $intLimit) break;
  398. $arrChar[1][$i] = trim(preg_replace('~\((.*)\)~Ui', '', strip_tags($arrChar[1][$i])));
  399. if ($arrChar[1][$i]) {
  400. $arrReturn[] = $strName . ' as ' . $arrChar[1][$i];
  401. }
  402. else {
  403. $arrReturn[] = $strName;
  404. }
  405. }
  406. return implode(' / ', $arrReturn) . ($bolMore && (count($arrReturned[2]) > $intLimit) ? '&hellip;' : '');
  407. }
  408. return 'n/A';
  409. }
  410. return 'n/A';
  411. }
  412.  
  413. /**
  414. * Returns the cast and character as URL .
  415. *
  416. * @return array The movie cast and character as URL (default limited to 20).
  417. */
  418. public function getCastAndCharacterAsUrl($intLimit = 20, $bolMore = true) {
  419. if ($this->isReady) {
  420. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_CAST);
  421. $arrChar = $this->matchRegex($this->_strSource, IMDB::IMDB_CHAR);
  422. if (count($arrReturned[2])) {
  423. foreach ($arrReturned[2] as $i => $strName) {
  424. if ($i >= $intLimit) break;
  425. $arrChar[1][$i] = trim(preg_replace('~\((.*)\)~Ui', '', $arrChar[1][$i]));
  426. //print_r($arrChar[1][$i]);
  427. preg_match_all('~<a href="/character/ch(\d+)/">(.*)</a>~Ui', $arrChar[1][$i], $arrMatches);
  428. if ($arrMatches[1][0] && $arrMatches[2][0]) {
  429. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a> as <a href="http://www.imdb.com/character/ch' . $arrMatches[1][0] . '/">' . $arrMatches[2][0] . '</a>';
  430. }
  431. else {
  432. if ($arrChar[1][$i]) {
  433. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a> as ' . strip_tags($arrChar[1][$i]);
  434. }
  435. else {
  436. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  437. }
  438. }
  439. }
  440. return implode(' / ', $arrReturn) . ($bolMore && (count($arrReturned[2]) > $intLimit) ? '&hellip;' : '');
  441. }
  442. return 'n/A';
  443. }
  444. return 'n/A';
  445. }
  446.  
  447.  
  448. /**
  449. * Returns the countr(y|ies).
  450. *
  451. * @return array The movie countr(y|ies).
  452. */
  453. public function getCountry() {
  454. if ($this->isReady) {
  455. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_COUNTRY);
  456. if (count($arrReturned[2])) {
  457. foreach ($arrReturned[2] as $strName) {
  458. $arrReturn[] = $strName;
  459. }
  460. return implode(' / ', $arrReturn);
  461. }
  462. return 'n/A';
  463. }
  464. return 'n/A';
  465. }
  466.  
  467. /**
  468. * Returns the countr(y|ies) as URL
  469. *
  470. * @return array The movie countr(y|ies) as URL.
  471. */
  472. public function getCountryAsUrl() {
  473. if ($this->isReady) {
  474. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_COUNTRY);
  475. if (count($arrReturned[2])) {
  476. foreach ($arrReturned[2] as $i => $strName) {
  477. $arrReturn[] = '<a href="http://www.imdb.com/country/' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  478. }
  479. return implode(' / ', $arrReturn);
  480. }
  481. return 'n/A';
  482. }
  483. return 'n/A';
  484. }
  485.  
  486. /**
  487. * Returns the director(s).
  488. *
  489. * @return array The movie director(s).
  490. */
  491. public function getDirector() {
  492. if ($this->isReady) {
  493. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_DIRECTOR, 2);
  494. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  495. if (count($arrReturned[2])) {
  496. foreach ($arrReturned[2] as $i => $strName) {
  497. $arrReturn[] = $strName;
  498. }
  499. return implode(' / ', $arrReturn);
  500. }
  501. return 'n/A';
  502. }
  503. return 'n/A';
  504. }
  505.  
  506. /**
  507. * Returns the director(s) as URL.
  508. *
  509. * @return array The movie director(s) as URL.
  510. */
  511. public function getDirectorAsUrl() {
  512. if ($this->isReady) {
  513. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_DIRECTOR, 2);
  514. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  515. if (count($arrReturned[2])) {
  516. foreach ($arrReturned[2] as $i => $strName) {
  517. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  518. }
  519. return implode(' / ', $arrReturn);
  520. }
  521. return 'n/A';
  522. }
  523. return 'n/A';
  524. }
  525.  
  526.  
  527. /**
  528. * Returns the creator(s).
  529. *
  530. * @return array The movie creator(s).
  531. */
  532. public function getCreator() {
  533. if ($this->isReady) {
  534. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_CREATOR, 2);
  535. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  536. if (count($arrReturned[2])) {
  537. foreach ($arrReturned[2] as $i => $strName) {
  538. $arrReturn[] = $strName;
  539. }
  540. return implode(' / ', $arrReturn);
  541. }
  542. return 'n/A';
  543. }
  544. return 'n/A';
  545. }
  546.  
  547. /**
  548. * Returns the creator(s) as URL.
  549. *
  550. * @return array The movie creator(s) as URL.
  551. */
  552. public function getCreatorAsUrl() {
  553. if ($this->isReady) {
  554. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_CREATOR, 2);
  555. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  556. if (count($arrReturned[2])) {
  557. foreach ($arrReturned[2] as $i => $strName) {
  558. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  559. }
  560. return implode(' / ', $arrReturn);
  561. }
  562. return 'n/A';
  563. }
  564. return 'n/A';
  565. }
  566.  
  567. /**
  568. * Returns the genre(s).
  569. *
  570. * @return array The movie genre(s).
  571. */
  572. public function getGenre() {
  573. if ($this->isReady) {
  574. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_GENRE);
  575. if (count($arrReturned[1])) {
  576. foreach ($arrReturned[1] as $strName) {
  577. $arrReturn[] = $strName;
  578. }
  579. return implode(' / ', $arrReturn);
  580. }
  581. return 'n/A';
  582. }
  583. return 'n/A';
  584. }
  585.  
  586. /**
  587. * Returns the genres as URL.
  588. *
  589. * @return array The movie genre as URL.
  590. */
  591. public function getGenreAsUrl() {
  592. if ($this->isReady) {
  593. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_GENRE);
  594. if (count($arrReturned[1])) {
  595. foreach ($arrReturned[1] as $i => $strName) {
  596. $arrReturn[] = '<a href="http://www.imdb.com/genre/' . $strName . '/">' . $strName . '</a>';
  597. }
  598. return implode(' / ', $arrReturn);
  599. }
  600. return 'n/A';
  601. }
  602. return 'n/A';
  603. }
  604.  
  605. /**
  606. * Returns the language(s).
  607. *
  608. * @return string The movie language(s).
  609. */
  610. public function getLanguages() {
  611. if ($this->isReady) {
  612. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_LANGUAGES);
  613. if (count($arrReturned[2])) {
  614. foreach ($arrReturned[2] as $strName) {
  615. $arrReturn[] = $strName;
  616. }
  617. return implode(' / ', $arrReturn);
  618. }
  619. return 'n/A';
  620. }
  621. return 'n/A';
  622. }
  623.  
  624. /**
  625. * Returns the language(s) as URL.
  626. *
  627. * @return string The movie language(s) as URL.
  628. */
  629. public function getLanguagesAsUrl() {
  630. if ($this->isReady) {
  631. $arrReturned = $this->matchRegex($this->_strSource, IMDB::IMDB_LANGUAGES);
  632. if (count($arrReturned[2])) {
  633. foreach ($arrReturned[2] as $i => $strName) {
  634. $arrReturn[] = '<a href="http://www.imdb.com/language/' . $arrReturned[1][$i] . '">' . $strName . '</a>';
  635. }
  636. return implode(' / ', $arrReturn);
  637. }
  638. return 'n/A';
  639. }
  640. return 'n/A';
  641. }
  642.  
  643. /**
  644. * Returns the movie location.
  645. *
  646. * @return string The location of the movie.
  647. */
  648. public function getLocation() {
  649. if ($this->isReady) {
  650. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_LOCATION, 2)) {
  651. return $strReturn;
  652. }
  653. return 'n/A';
  654. }
  655. return 'n/A';
  656. }
  657.  
  658. /**
  659. * Returns the movie location as URL.
  660. *
  661. * @return string The location of the movie as URL.
  662. */
  663. public function getLocationAsUrl() {
  664. if ($this->isReady) {
  665. $strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_LOCATION, 2);
  666. if ($strReturn) {
  667. return '<a href="http://www.imdb.com/search/title?locations=' . urlencode($strReturn) . '">' . $strReturn . '</a>';
  668. }
  669. return 'n/A';
  670. }
  671. return 'n/A';
  672. }
  673.  
  674.  
  675. /**
  676. * Returns the MPAA.
  677. *
  678. * @return string The movie MPAA.
  679. */
  680. public function getMpaa() {
  681. if ($this->isReady) {
  682. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_MPAA, 1)) {
  683. return $strReturn;
  684. }
  685. return 'n/A';
  686. }
  687. return 'n/A';
  688. }
  689.  
  690. /**
  691. * Returns the plot.
  692. *
  693. * @return string The movie plot.
  694. */
  695. public function getPlot($intLimit = 0) {
  696. if ($this->isReady) {
  697. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_PLOT, 1)) {
  698. if ($intLimit) {
  699. return $this->makeShort($strReturn, $intLimit);
  700. }
  701. return $strReturn;
  702. }
  703. return 'n/A';
  704. }
  705. return 'n/A';
  706. }
  707.  
  708. /**
  709. * Download the poster, cache it and return the local path to the image.
  710. *
  711. * @return string The path to the poster (either local or online).
  712. */
  713. public function getPoster() {
  714. if ($this->isReady) {
  715. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_POSTER, 2)) {
  716. if ($strLocal = $this->saveImage($strReturn)) {
  717. return $strLocal;
  718. }
  719. return $strReturn;
  720. }
  721. return 'n/A';
  722. }
  723. return 'n/A';
  724. }
  725.  
  726. /**
  727. * Returns the rating.
  728. *
  729. * @return string The movie rating.
  730. */
  731. public function getRating() {
  732. if ($this->isReady) {
  733. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_RATING, 1)) {
  734. return $strReturn;
  735. }
  736. return 'n/A';
  737. }
  738. return 'n/A';
  739. }
  740.  
  741. /**
  742. * Returns the release date.
  743. *
  744. * @return string The movie release date.
  745. */
  746. public function getReleaseDate() {
  747. if ($this->isReady) {
  748. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_RELEASE_DATE, 1)) {
  749. return str_replace('(', ' (', $strReturn);
  750. }
  751. return 'n/A';
  752. }
  753. return 'n/A';
  754. }
  755.  
  756. /**
  757. * Returns the runtime.
  758. *
  759. * @return string The movie runtime.
  760. */
  761. public function getRuntime() {
  762. if ($this->isReady) {
  763. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_RUNTIME, 1)) {
  764. return $strReturn;
  765. }
  766. return 'n/A';
  767. }
  768. return 'n/A';
  769. }
  770.  
  771. /**
  772. * Returns the tagline.
  773. *
  774. * @return string The movie tagline.
  775. */
  776. public function getTagline() {
  777. if ($this->isReady) {
  778. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_TAGLINE, 1)) {
  779. return $strReturn;
  780. }
  781. return 'n/A';
  782. }
  783. return 'n/A';
  784. }
  785.  
  786. /**
  787. * Return the title.
  788. *
  789. * @return string The movie title.
  790. */
  791. public function getTitle() {
  792. if ($this->isReady) {
  793. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_TITLE_ORIG, 1)) {
  794. return $strReturn;
  795. }
  796. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_TITLE, 1)) {
  797. return $strReturn;
  798. }
  799. return 'n/A';
  800. }
  801. return 'n/A';
  802. }
  803.  
  804. /**
  805. * Returns the URL.
  806. *
  807. * @return string The movie URL.
  808. */
  809. public function getUrl() {
  810. return $this->_strUrl;
  811. }
  812.  
  813. /**
  814. * Returns the votes.
  815. *
  816. * @return string The votes of the movie.
  817. */
  818. public function getVotes() {
  819. if ($this->isReady) {
  820. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_VOTES, 1)) {
  821. return $strReturn;
  822. }
  823. return 'n/A';
  824. }
  825. return 'n/A';
  826. }
  827.  
  828. /**
  829. * Returns the writer(s).
  830. *
  831. * @return array The movie writer(s).
  832. */
  833. public function getWriter() {
  834. if ($this->isReady) {
  835. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_WRITER, 2);
  836. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  837. if (count($arrReturned[2])) {
  838. foreach ($arrReturned[2] as $i => $strName) {
  839. $arrReturn[] = $strName;
  840. }
  841. return implode(' / ', $arrReturn);
  842. }
  843. return 'n/A';
  844. }
  845. return 'n/A';
  846. }
  847.  
  848. /**
  849. * Returns the writer(s) as URL.
  850. *
  851. * @return array The movie writer(s) as URL.
  852. */
  853. public function getWriterAsUrl() {
  854. if ($this->isReady) {
  855. $strContainer = $this->matchRegex($this->_strSource, IMDB::IMDB_WRITER, 2);
  856. $arrReturned = $this->matchRegex($strContainer, IMDB::IMDB_NAME);
  857. if (count($arrReturned[2])) {
  858. foreach ($arrReturned[2] as $i => $strName) {
  859. $arrReturn[] = '<a href="http://www.imdb.com/name/nm' . $arrReturned[1][$i] . '/">' . $strName . '</a>';
  860. }
  861. return implode(' / ', $arrReturn);
  862. }
  863. return 'n/A';
  864. }
  865. return 'n/A';
  866. }
  867.  
  868. /**
  869. * Returns the movie year.
  870. *
  871. * @return string The year of the movie.
  872. */
  873. public function getYear() {
  874. if ($this->isReady) {
  875. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_TITLE, 2)) {
  876. return substr(preg_replace('~[\D]~', '', $strReturn), 0, 4);
  877. }
  878. return 'n/A';
  879. }
  880. return 'n/A';
  881. }
  882.  
  883. /**
  884. * Returns the seasons.
  885. *
  886. * @return string The movie seasons.
  887. */
  888. public function getSeasons() {
  889. if ($this->isReady) {
  890. if ($strReturn = $this->matchRegex($this->_strSource, IMDB::IMDB_SEASONS)) {
  891. $strReturn = strip_tags(implode($strReturn[1]));
  892. $strFind = array('&raquo;', '&nbsp;', 'Full episode list', ' ');
  893. $strReturn = str_replace($strFind, '', $strReturn);
  894. $arrReturn = explode('|', $strReturn);
  895. unset($arrReturn[(count($arrReturn)-1)]);
  896. if ($arrReturn) {
  897. return implode(' / ', $arrReturn);
  898. }
  899. }
  900. return 'n/A';
  901. }
  902. return 'n/A';
  903. }
  904. }
Advertisement
Add Comment
Please, Sign In to add comment