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

/library/imdb/imdbsearch.class.php

https://github.com/hegylako/openTracker
PHP | 168 lines | 101 code | 16 blank | 51 comment | 36 complexity | e22002eaa00435528c08d6cb5c10d151 MD5 | raw file
Possible License(s): MIT
  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: imdbsearch.class.php 388 2010-06-03 11:28:49Z izzy $ */
  12. require_once (dirname(__FILE__)."/browseremulator.class.php");
  13. if (defined('IMDBPHP_CONFIG')) require_once (IMDBPHP_CONFIG);
  14. else require_once (dirname(__FILE__)."/mdb_config.class.php");
  15. require_once (dirname(__FILE__)."/movie_base.class.php");
  16. require_once (dirname(__FILE__)."/mdb_request.class.php");
  17. #====================================================[ IMDB Search class ]===
  18. /** Search the IMDB for a title and obtain the movies IMDB ID
  19. * @package openTracker
  20. * @class imdbsearch
  21. * @extends mdb_config
  22. * @author Izzy (izzysoft AT qumran DOT org)
  23. * @copyright (c) 2002-2004 by Giorgos Giagas and (c) 2004-2008 by Itzchak Rehberg and IzzySoft
  24. * @version $Revision: 388 $ $Date: 2010-06-03 13:28:49 +0200 (Do, 03. Jun 2010) $
  25. */
  26. class imdbsearch extends mdb_base {
  27. var $page = "";
  28. var $name = NULL;
  29. var $resu = array();
  30. var $url = "http://www.imdb.com/";
  31. /** Read the config
  32. * @constructor imdbsearch
  33. */
  34. function __construct() {
  35. parent::__construct('');
  36. $this->search_episodes(FALSE);
  37. $this->last_results = 0;
  38. }
  39. /** Search for episodes or movies
  40. * @method search_episodes
  41. * @param boolean enabled TRUE: Search for episodes; FALSE: Search for movies (default)
  42. */
  43. public function search_episodes($enable) {
  44. $this->episode_search = $enable;
  45. }
  46. /** Set the name (title) to search for
  47. * @method setsearchname
  48. * @param string searchstring what to search for - (part of) the movie name
  49. */
  50. public function setsearchname($name) {
  51. $this->name = $name;
  52. $this->page = "";
  53. $this->url = NULL;
  54. }
  55. /** Set the URL (overwrite default search URL and run your own)
  56. * This URL will be reset if you call the setsearchname() method
  57. * @method seturl
  58. * @param string URL to use
  59. */
  60. public function seturl($url){
  61. $this->url = $url;
  62. }
  63. /** Create the IMDB URL for the movie search
  64. * @method private mkurl
  65. * @return string url
  66. */
  67. private function mkurl() {
  68. if ($this->url !== NULL){
  69. $url = $this->url;
  70. }else{
  71. if (!isset($this->maxresults)) $this->maxresults = 20;
  72. if ($this->maxresults > 0) $query = ";mx=20";
  73. if ($this->episode_search) $url = "http://".$this->imdbsite."/find?q=".urlencode($this->name).$query.";s=ep";
  74. else {
  75. switch ($this->searchvariant) {
  76. case "moonface" : $query .= ";more=tt;nr=1"; // @moonface variant (untested)
  77. case "sevec" : $query .= "&restrict=Movies+only&GO.x=0&GO.y=0&GO=search;tt=1"; // Sevec ori
  78. default : $query .= ";tt=on"; // Izzy
  79. }
  80. $url = "http://".$this->imdbsite."/find?q=".urlencode($this->name).$query;
  81. }
  82. }
  83. return $url;
  84. }
  85. /** Reset search results
  86. * This empties the collected search results. Without calling this, every
  87. * new search appends its results to the ones collected by the previous search.
  88. * @method reset
  89. */
  90. function reset() {
  91. $this->resu = array();
  92. }
  93. /** Setup search results
  94. * @method results
  95. * @param optional string URL Replace search URL by your own (Default: empty string)
  96. * @param optional boolean series whether to include TV series in search results (default: TRUE)
  97. * @return array results array of objects (instances of the imdb class)
  98. */
  99. public function results($url="",$series=TRUE) {
  100. if ($this->page == "") {
  101. if ($this->usecache && empty($url)) { // Try to read from cache
  102. $this->cache_read(urlencode(strtolower($this->name)).'.search',$this->page);
  103. } // end cache read
  104. if ($this->page=="") { // not found in cache - go and get it!
  105. if (empty($url)) $url = $this->mkurl();
  106. mdb_base::debug_scalar("imdbsearch::results() called. Using URL $url");
  107. $be = new MDB_Request($url);
  108. $be->sendrequest();
  109. $fp = $be->getResponseBody();
  110. if ( !$fp ) {
  111. if ($header = $be->getResponseHeader("Location")) {
  112. mdb_base::debug_scalar("No immediate response body - we are redirected.<br>New URL: $header");
  113. if ( preg_match('!\.imdb\.(com|de|it)/find\?!',$header) ) {
  114. return $this->results($header);
  115. break(4);
  116. }
  117. $url = explode("/",$header);
  118. $id = substr($url[count($url)-2],2);
  119. $this->resu[0] = new imdb($id);
  120. return $this->resu;
  121. } else {
  122. mdb_base::debug_scalar('No response body, no redirect - going to Nirwana');
  123. return NULL;
  124. }
  125. }
  126. $this->page = $fp;
  127. }
  128. if ($this->storecache && $this->page != "cannot open page" && $this->page != "") { //store cache
  129. $this->cache_write(urlencode(strtolower($this->name)).'.search',$this->page);
  130. }
  131. } // end (page="")
  132. // now we have the search content - go and parse it!
  133. if ($this->maxresults > 0) $maxresults = $this->maxresults; else $maxresults = 999999;
  134. if ( preg_match_all('!href="/title/tt(\d{7})/"[^>]*>(.*?)</a>\s*(\((\d{4})(/.+?|)\)|)[^<]*(<small>(.*?)</small>|)!ims',$this->page,$matches) ) {
  135. $this->last_results = count($matches[0]);
  136. $mids_checked = array();
  137. for ($i=0;$i<$this->last_results;++$i) {
  138. if (count($this->resu) == $maxresults) break; // limit result count
  139. if ( empty($matches[2][$i]) || substr(trim($matches[2][$i]),0,4)=='<img' || in_array($matches[1][$i],$mids_checked) ) continue; // empty titles just come from the images
  140. if ( !$series && (preg_match('!&#x22;.+&#x22;!',($matches[2][$i])) || strpos(strtoupper($matches[7][$i]),'TV SERIES')!==FALSE) ) continue; // skip series if commanded so
  141. if ( !preg_match('!onclick!i',$matches[0][$i]) ) continue; // just mentioned something in the AKAs listing
  142. $mids_checked[] = $matches[1][$i];
  143. $tmpres = new imdb($matches[1][$i]); // make a new imdb object by id
  144. $tmpres->main_title = $matches[2][$i];
  145. $tmpres->main_year = $matches[4][$i];
  146. $this->resu[] = $tmpres;
  147. }
  148. }
  149. return $this->resu;
  150. } // end results()
  151. } // end class imdbsearch
  152. ?>