-
Notifications
You must be signed in to change notification settings - Fork 3
/
imdbsearch.class.php
176 lines (160 loc) · 6.56 KB
/
imdbsearch.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
#############################################################################
# IMDBPHP (c) Giorgos Giagas & Itzchak Rehberg #
# written by Giorgos Giagas #
# extended & maintained by Itzchak Rehberg <izzysoft AT qumran DOT org> #
# http://www.izzysoft.de/ #
# ------------------------------------------------------------------------- #
# This program is free software; you can redistribute and/or modify it #
# under the terms of the GNU General Public License (see doc/LICENSE) #
#############################################################################
/* $Id: imdbsearch.class.php 323 2010-02-21 20:05:46Z izzy $ */
require_once (dirname(__FILE__)."/browseremulator.class.php");
if (defined('IMDBPHP_CONFIG')) require_once (IMDBPHP_CONFIG);
else require_once (dirname(__FILE__)."/mdb_config.class.php");
require_once (dirname(__FILE__)."/movie_base.class.php");
require_once (dirname(__FILE__)."/mdb_request.class.php");
#====================================================[ IMDB Search class ]===
/** Search the IMDB for a title and obtain the movies IMDB ID
* @package IMDB
* @class imdbsearch
* @extends mdb_config
* @author Izzy (izzysoft AT qumran DOT org)
* @copyright (c) 2002-2004 by Giorgos Giagas and (c) 2004-2008 by Itzchak Rehberg and IzzySoft
* @version $Revision: 323 $ $Date: 2010-02-21 21:05:46 +0100 (So, 21. Feb 2010) $
*/
class imdbsearch extends mdb_base {
var $page = "";
var $name = NULL;
var $resu = array();
var $url = "http://www.imdb.com/";
/** Read the config
* @constructor imdbsearch
*/
function __construct() {
parent::__construct('');
$this->search_episodes(FALSE);
}
/** Search for episodes or movies
* @method search_episodes
* @param boolean enabled TRUE: Search for episodes; FALSE: Search for movies (default)
*/
public function search_episodes($enable) {
$this->episode_search = $enable;
}
/** Set the name (title) to search for
* @method setsearchname
* @param string searchstring what to search for - (part of) the movie name
*/
public function setsearchname($name) {
$this->name = $name;
$this->page = "";
$this->url = NULL;
}
/** Set the URL (overwrite default search URL and run your own)
* This URL will be reset if you call the setsearchname() method
* @method seturl
* @param string URL to use
*/
public function seturl($url){
$this->url = $url;
}
/** Create the IMDB URL for the movie search
* @method private mkurl
* @return string url
*/
private function mkurl() {
if ($this->url !== NULL){
$url = $this->url;
}else{
if (!isset($this->maxresults)) $this->maxresults = 20;
if ($this->maxresults > 0) $query = ";mx=20";
if ($this->episode_search) $url = "http://".$this->imdbsite."/find?q=".urlencode($this->name).$query.";s=ep";
else {
switch ($this->searchvariant) {
case "moonface" : $query .= ";more=tt;nr=1"; // @moonface variant (untested)
case "sevec" : $query .= "&restrict=Movies+only&GO.x=0&GO.y=0&GO=search;tt=1"; // Sevec ori
default : $query .= ";tt=on"; // Izzy
}
$url = "http://".$this->imdbsite."/find?q=".urlencode($this->name).$query;
}
}
return $url;
}
/** Reset search results
* This empties the collected search results. Without calling this, every
* new search appends its results to the ones collected by the previous search.
* @method reset
*/
function reset() {
$this->resu = array();
}
/** Setup search results
* @method results
* @param optional string URL Replace search URL by your own
* @return array results array of objects (instances of the imdb class)
*/
public function results($url="") {
if ($this->page == "") {
if ($this->usecache && empty($url)) { // Try to read from cache
$this->cache_read(urlencode(strtolower($this->name)).'.search',$this->page);
} // end cache read
if ($this->page=="") { // not found in cache - go and get it!
if (empty($url)) $url = $this->mkurl();
mdb_base::debug_scalar("imdbsearch::results() called. Using URL $url");
$be = new MDB_Request($url);
$be->sendrequest();
$fp = $be->getResponseBody();
if ( !$fp ) {
if ($header = $be->getResponseHeader("Location")) {
mdb_base::debug_scalar("No immediate response body - we are redirected.<br>New URL: $header");
if ( preg_match('!\.imdb\.(com|de|it)/find\?!',$header) ) {
return $this->results($header);
break(4);
}
$url = explode("/",$header);
$id = substr($url[count($url)-2],2);
$this->resu[0] = new imdb($id);
return $this->resu;
} else {
mdb_base::debug_scalar('No response body, no redirect - going to Nirwana');
return NULL;
}
}
$this->page = $fp;
}
if ($this->storecache && $this->page != "cannot open page" && $this->page != "") { //store cache
$this->cache_write(urlencode(strtolower($this->name)).'.search',$this->page);
}
} // end (page="")
// now we have the search content - go and parse it!
$searchstring = array( '<A HREF="/title/tt', '<A href="/title/tt', '<a href="/Title?', '<a href="/title/tt');
$i = 0;
if ($this->maxresults > 0) $maxresults = $this->maxresults; else $maxresults = 999999;
foreach($searchstring as $srch){
$res_e = 0;
$res_s = 0;
$mids_checked = array();
$len = strlen($srch);
while ((($res_s = strpos ($this->page, $srch, $res_e)) > 10)) {
if ($i == $maxresults) break(2); // limit result count
$res_e = strpos ($this->page, "(", $res_s);
$imdb_id = substr($this->page, $res_s + $len, 7);
$ts = strpos($this->page, ">",$res_s) +1; // >movie title</a>
$te = strpos($this->page,"<",$ts);
$title = substr($this->page,$ts,$te-$ts);
if (($title == "") || (in_array($imdb_id,$mids_checked))) continue; // empty titles just come from the images
$mids_checked[] = $imdb_id;
$tmpres = new imdb ($imdb_id); // make a new imdb object by id
$tmpres->main_title = $title;
$ts = strpos($this->page,"(",$te) +1;
$te = strpos($this->page,")",$ts);
$tmpres->main_year=substr($this->page,$ts,$te-$ts);
$i++;
$this->resu[] = $tmpres;
}
}
return $this->resu;
} // end results()
} // end class imdbsearch
?>