#
# http://www.qumran.org/homes/izzy/ #
# ------------------------------------------------------------------------- #
# 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: imdb.class.php,v 1.13 2007/10/05 00:07:03 izzy Exp $ */
require_once ("include/browser/browseremulator.class.php");
require_once ("include/browser/info_extractor.php");
require_once (dirname(__FILE__)."/imdb_config.php");
#===============================================[ The IMDB class itself ]===
/** Accessing IMDB information
* @package Api
* @class imdb
* @extends imdb_config
* @author Izzy (izzysoft@qumran.org)
* @copyright (c) 2002-2004 by Giorgos Giagas and (c) 2004-2007 by Itzchak Rehberg and IzzySoft
* @version $Revision: 1.13 $ $Date: 2007/10/05 00:07:03 $
*/
class imdb extends imdb_config {
var $imdbID = "";
var $page;
var $main_title = "";
var $main_year = "";
var $main_runtime = "";
var $main_runtimes;
var $main_rating = "";
var $main_votes = "";
var $main_language = "";
var $main_languages = "";
var $main_genre = "";
var $main_genres = "";
var $main_tagline = "";
var $main_plotoutline = "";
var $main_comment = "";
var $main_alttitle = "";
var $main_colors = "";
var $plot_plot = "";
var $taglines = "";
var $credits_cast = [];
var $credits_director = [];
var $credits_writing = "";
var $credits_producer = "";
var $main_director = "";
var $main_credits = "";
var $main_photo = "";
var $main_country = "";
var $main_alsoknow = [];
var $main_sound = "";
var $info_excer;
var $similiar_movies = array(array('Name' => '', 'Link' => '', 'Local' => '')); // no Thumbnail here, since it works different from last.fm, douban
var $extension = array('Title', 'Credits', 'Amazon', 'Goofs', 'Plot', 'Comments', 'Quotes', 'Taglines', 'Plotoutline', 'Trivia', 'Directed');
function debug_scalar($scalar) {
echo "$scalar
";
}
function debug_object($object) {
echo "";print_r($object);echo "
";
}
function debug_html($html) {
echo "".htmlentities($html)."
";
}
/** Get similiar movies
* @method similiar_movies
* @return list similiar_movies
*/
function similiar_movies()
{
if (!isset($this->similiar_movies))
{
if ($this->page["Title"] == "")
{
$this->openpage ("Title");
}
$similiar_movies = $this->info_excer->truncate($this->page["Title"], "
Recommendations
", "");
$similiar_movies = $this->info_excer->truncate($similiar_movies, "
", "
");
$res_where_array = array('Link' => '1', 'Name' => '3');
if($res_array = $this->info_excer->find_pattern($similiar_movies,"/((\s|.)+?)<\/a><\/td>/",true,$res_where_array))
{
$counter = 0;
foreach($res_array as $res_array_each)
{
$this->similiar_movies[$counter]['Link'] = $res_array_each[0];
$this->similiar_movies[$counter]['Name'] = $res_array_each[1];
$imdb_id = ltrim(strrchr($res_array_each[0],'tt'),'tt');
$imdb_id = preg_replace("/[^A-Za-z0-9]/", "", $imdb_id);
//die("ss" . $imdb_id);
$imdb_sim_movies = new imdb($imdb_id);
//$imdb_sim_movies->setid($imdb_id);
$target = array('Title', 'Credits', 'Plot');
$imdb_sim_movies->preparecache($target,false);
$this->similiar_movies[$counter]['Local'] = $imdb_sim_movies->photo_localurl();
$counter++;
}
}
}
return $this->similiar_movies ?? [];
}
/** Test if IMDB url is valid
* @method urlstate ()
* @param none
* @return int state (0-not valid, 1-valid)
*/
function urlstate () {
if (strlen($this->imdbID) != 7)
return 0;
else
return 1;
}
/** Test if caching IMDB page is complete
* @method cachestate ()
* @param $target array
* @return int state (0-not complete, 1-cache complete, 2-cache not enabled, 3-not valid imdb url)
*/
function cachestate ($target = "") {
if (strlen($this->imdbID) != 7){
//echo "not valid imdbID: ".$this->imdbID." ".strlen($this->imdbID);
$this->page[$wt] = "cannot open page";
return 3;
}
if ($this->usecache)
{
$ext_arr = isset($target) ? $target : $this->extension;
foreach($ext_arr as $ext)
{
if(!file_exists("$this->cachedir/$this->imdbID.$ext"))
return 0;
@$fp = fopen ("$this->cachedir/$this->imdbID.$ext", "r");
if (!$fp)
return 0;
}
return 1;
}
else
return 2;
}
/** prepare IMDB page cache
* @method preparecache
* @param $target array
*/
function preparecache ($target = "", $retrive_similiar = false) {
$ext_arr = isset($target) ? $target : $this->extension;
foreach($ext_arr as $ext)
{
$tar_ext = array($ext);
if($this->cachestate($tar_ext) == 0)
$this->openpage($ext);
}
if($retrive_similiar)
$this->similiar_movies(false);
return $ext;
}
/** Open an IMDB page
* @method openpage
* @param string wt
*/
function openpage ($wt) {
if (strlen($this->imdbID) != 7){
echo "not valid imdbID: ".$this->imdbID." ".strlen($this->imdbID);
$this->page[$wt] = "cannot open page";
return;
}
switch ($wt){
case "Title" : $urlname="/"; break;
case "Credits" : $urlname="/fullcredits"; break;
case "Plot" : $urlname="/plotsummary"; break;
case "Taglines": $urlname="/taglines"; break;
}
if ($this->usecache) {
@$fp = fopen ("$this->cachedir/$this->imdbID.$wt", "r");
if ($fp) {
$temp="";
while (!feof ($fp)) {
$temp .= fread ($fp, 1024);
}
if ($temp) {
$this->page[$wt] = $temp;
return;
}
}
} // end cache
$req = new IMDB_Request("");
$req->setURL("https://".$this->imdbsite."/title/tt".$this->imdbID.$urlname);
$response = $req->send();
$responseBody = $response->getBody();
if ($responseBody) {
$this->page[$wt] = utf8_encode($responseBody);
}
if( $this->page[$wt] ){ //storecache
if ($this->storecache) {
if (!is_dir($this->cachedir)) {
$mkdirResult = mkdir($this->cachedir, 0777, true);
}
$fp = fopen ("$this->cachedir/$this->imdbID.$wt", "w");
fputs ($fp, $this->page[$wt]);
fclose ($fp);
}
return;
}
$this->page[$wt] = "cannot open page";
//echo "page not found";
}
/** Retrieve the IMDB ID
* @method imdbid
* @return string id
*/
function imdbid () {
return $this->imdbID;
}
/** Setup class for a new IMDB id
* @method setid
* @param string id
*/
function setid ($id) {
$this->imdbID = $id;
$this->page["Title"] = "";
$this->page["Credits"] = "";
$this->page["Amazon"] = "";
$this->page["Goofs"] = "";
$this->page["Plot"] = "";
$this->page["Comments"] = "";
$this->page["Quotes"] = "";
$this->page["Taglines"] = "";
$this->page["Plotoutline"] = "";
$this->page["Trivia"] = "";
$this->page["Directed"] = "";
$this->main_title = "";
$this->main_year = "";
$this->main_runtime = "";
$this->main_rating = "";
$this->main_comment = "";
$this->main_votes = "";
$this->main_language = "";
$this->main_genre = "";
$this->main_genres = "";
$this->main_tagline = "";
$this->main_plotoutline = "";
$this->main_alttitle = "";
$this->main_colors = "";
$this->credits_cast = [];
$this->main_director = "";
$this->main_creator = "";
unset($this->similiar_movies);
$this->info_excer = new info_extractor();
}
/** Initialize class
* @constructor imdb
* @param string id
*/
function __construct ($id) {
// $this->imdb_config();
parent::__construct();
$this->setid($id);
//if ($this->storecache && ($this->cache_expire > 0)) $this->purge();
}
/** Check cache and purge outdated files
* This method looks for files older than the cache_expire set in the
* imdb_config and removes them
* @method purge
*/
function purge($explicit = false) {
if (is_dir($this->cachedir)) {
$thisdir = dir($this->cachedir);
$now = time();
while( $file=$thisdir->read() ) {
if ($file!="." && $file!="..") {
$fname = $this->cachedir ."/". $file;
if (is_dir($fname)) continue;
$mod = filemtime($fname);
if ($mod && (($now - $mod > $this->cache_expire) || $explicit == true)) unlink($fname);
}
}
}
}
/** Check cache and purge outdated single imdb title file
* This method looks for files older than the cache_expire set in the
* imdb_config and removes them
* @method purge
*/
function purge_single($explicit = false) {
if (is_dir($this->cachedir))
{
$thisdir = dir($this->cachedir);
foreach($this->extension as $ext)
{
$fname = $this->cachedir ."/". $this->imdbid() . "." . $ext;
//return $fname;
if(file_exists($fname))
{
$now = time();
$mod = filemtime($fname);
if ($mod && (($now - $mod > $this->cache_expire) || $explicit == true)) unlink($fname);
}
}
}
}
/** get the time that cache is stored
* @method getcachetime
*/
function getcachetime() {
$mod =0;
if (is_dir($this->cachedir))
{
$thisdir = dir($this->cachedir);
foreach($this->extension as $ext)
{
$fname = $this->cachedir ."/". $this->imdbid() . "." . $ext;
if(file_exists($fname))
{
if($mod > filemtime($fname) || $mod==0)
$mod = filemtime($fname);
}
}
}
return $mod;
}
/** Set up the URL to the movie title page
* @method main_url
* @return string url
*/
function main_url(){
return "https://".$this->imdbsite."/title/tt".$this->imdbid()."/";
}
/** Get movie title
* @method title
* @return string title
*/
function title () {
if ($this->main_title == "") {
if ($this->page["Title"] == "") {
$this->openpage ("Title");
}
$result = $this->retrieveFromPage('', '');
$result = strstr($result, '(', true);
$this->main_title = trim($result);
}
return $this->main_title;
}
/** Get year
* @method year
* @return string year
*/
function year () {
if ($this->main_year == "") {
if ($this->page["Title"] == "") {
$this->openpage ("Title");
}
$result = $this->retrieveFromPage('', '');
$begin = mb_strpos($result, '(', 0, 'utf-8');
$end = mb_strpos($result, ')', $begin, 'utf-8');
do_log("year, result: $result, begin: $begin, end: $end");
$result = mb_substr($result, $begin + 1, $end - $begin - 1, 'utf-8');
$this->main_year = trim($result);
}
return $this->main_year;
}
/** Get general runtime
* @method runtime_all
* @return string runtime
*/
function runtime_all () {
if ($this->main_runtime == "") {
if ($this->page["Title"] == "") {
$this->openpage ("Title");
}
$runtime_s = strpos ($this->page["Title"], "Runtime:");
$runtime_e = strpos ($this->page["Title"], "", $runtime_s);
$this->main_runtime = strip_tags(substr ($this->page["Title"], $runtime_s + 13, $runtime_e - $runtime_s - 14));
if ($runtime_s == 0)
$this->main_runtime = "";
}
return $this->main_runtime;
}
/** Get overall runtime
* @method runtime
* @return mixed string runtime (if set), NULL otherwise
*/
function runtime(){
$runarr = $this->runtimes();
if (isset($runarr[0]["time"])){
return $runarr[0]["time"];
}else{
return NULL;
}
}
/** Retrieve language specific runtimes
* @method runtimes
* @return array runtimes (array[0..n] of array[time,country,comment])
*/
function runtimes(){
if ($this->main_runtimes == "") {
if ($this->runtime_all() == ""){
return array();
}
#echo $this->runtime_all();
$run_arr= explode( "|" , $this->runtime_all());
$max = count($run_arr);
for ( $i=0; $i < $max ; $i++){
$time_e = strpos( $run_arr[$i], " min");
$country_e = strpos($run_arr[$i], ":");
if ( $country_e == 0){
$time_s = 0;
}else{
$time_s = $country_e+1;
}
$comment_s = strpos( $run_arr[$i], '(');
$comment_e = strpos( $run_arr[$i], ')');
$runtemp["time"]= substr( $run_arr[$i], $time_s, $time_e - $time_s);
$country_s = 0;
if ($country_s != $country_e){
$runtemp["country"]= substr( $run_arr[$i], $country_s, $country_e - $country_s);
}else{
$runtemp["country"]=NULL;
}
if ($comment_s != $comment_e){
$runtemp["comment"]= substr( $run_arr[$i], $comment_s + 1, $comment_e - $comment_s - 1);
}else{
$runtemp["comment"]=NULL;
}
$this->main_runtimes[$i] = $runtemp;
}
}
return $this->main_runtimes;
}
/** Get movie rating
* @method rating
* @return string rating
*/
function rating () {
if ($this->main_rating == "") {
if ($this->page["Title"] == "") {
$this->openpage ("Title");
}
$result = $this->retrieveFromPage('itemprop="ratingValue">', '');
$this->main_rating = $result;
return $this->main_rating;
}
}
/** Get movie comment
* @method comment
* @return string comment
*/
function comment () {
if ($this->main_comment == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$comment_s = strpos ($this->page["Title"], "people found the following comment useful:-");
if ( $comment_s == 0) return false;
$comment_e = strpos ($this->page["Title"], "Was the above comment useful to you?", $comment_s);
$forward_safeval = 50;
$comment_s_fix = $forward_safeval - strpos(substr($this->page["Title"], $comment_s - $forward_safeval, $comment_e - $comment_s + $forward_safeval),"") - strlen(" ");
$this->main_comment = substr ($this->page["Title"], $comment_s - $comment_s_fix, $comment_e - $comment_s + $comment_s_fix);
$this->main_comment = preg_replace("/a href\=\"\//i","a href=\"https://".$this->imdbsite."/",$this->main_comment);
$this->main_comment = preg_replace("/http:\/\/[a-zA-Z.-]+\/images\/showtimes\//i","pic/imdb_pic/",$this->main_comment);
$this->main_comment = preg_replace("/<\/?div.*>/i","",$this->main_comment);
$this->main_comment = preg_replace("/ /i","",$this->main_comment);
}
return $this->main_comment;
}
/** Return votes for this movie
* @method votes
* @return string votes
*/
function votes () {
if ($this->main_votes == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$string = utf8_decode($this->page["Title"]);
$vote_s = mb_strpos ($string, "imdbRating", 0, 'utf-8');
if ( $vote_s == 0) return false;
if (strpos ($string, "awaiting 5 votes")) return false;
$result = $this->retrieveFromPage('itemprop="ratingCount">', '');
$this->main_votes = $result;
// preg_match('/href=\"ratings\".*>([0-9,][0-9,]*)/', $this->page["Title"], $matches);
// $this->main_votes = $matches[1];
$this->main_votes = "imdbsite."/title/tt".$this->imdbID."/ratings\">" . $this->main_votes . "";
}
return $this->main_votes;
}
/** Get movies original language
* @method language
* @return string language
*/
function language () {
if ($this->main_language == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$string = utf8_decode($this->page["Title"]);
$startStr = 'Language:';
$lang_s = mb_strpos ($string, $startStr, 0, 'utf-8');
$lang_e = mb_strpos ($string, "", $lang_s);
$result = mb_substr ($string, $lang_s, $lang_e - $lang_s);
$result = str_replace($startStr, '', $result);
do_log("start: $lang_s, 'end: $lang_e, result: $result");
$result = strip_tags($result);
$this->main_language = $result;
}
return $this->main_language;
}
/** Get all langauges this movie is available in
* @method languages
* @return array languages (array[0..n] of strings)
*/
function languages () {
if ($this->main_languages == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$lang_s = 0;
$lang_e = 0;
$i = 0;
$this->main_languages = array();
while (strpos($this->page["Title"], "/Sections/Languages/", $lang_e) > $lang_s) {
$lang_s = strpos ($this->page["Title"], "/Sections/Languages/", $lang_s);
$lang_s = strpos ($this->page["Title"], ">", $lang_s);
$lang_e = strpos ($this->page["Title"], "<", $lang_s);
$this->main_languages[$i] = substr ($this->page["Title"], $lang_s + 1, $lang_e - $lang_s - 1);
$i++;
}
}
return $this->main_languages;
}
/** Get the movies main genre
* @method genre
* @return string genre
*/
function genre () {
if ($this->main_genre == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$genre_s = strpos ($this->page["Title"], "/Sections/Genres/");
if ( $genre_s === FALSE ) return FALSE;
$genre_s = strpos ($this->page["Title"], ">", $genre_s);
$genre_e = strpos ($this->page["Title"], "<", $genre_s);
$this->main_genre = substr ($this->page["Title"], $genre_s + 1, $genre_e - $genre_s - 1);
}
return $this->main_genre;
}
/** Get all genres the movie is registered for
* @method genres
* @return array genres (array[0..n] of strings)
*/
function genres () {
if ($this->main_genres == "") {
$result = $this->retrieveFromPage('Genres:', ' ');
$result = strip_tags($result);
//no need more...
return $this->main_genres = explode('|', $result);
if ($this->page["Title"] == "") $this->openpage ("Title");
$this->main_genres = array();
$genre_s = strpos($this->page["Title"],"/Sections/Genres/") -5;
if ($genre_s === FALSE) return array(); // no genre found
if ($genre_s < 0) return array(); // no genre found
$genre_e = strpos($this->page["Title"],"/rg/title-tease/",$genre_s);
$block = substr($this->page["Title"],$genre_s,$genre_e-$genre_s);
$diff = $genre_e-$genre_s;
$genre_s = 0;
$genre_e = 0;
$i = 0;
while (strpos($block, "/Sections/Genres/", $genre_e) > $genre_s) {
$genre_s = strpos ($block, "/Sections/Genres/", $genre_s);
$genre_s = strpos ($block, ">", $genre_s);
$genre_e = strpos ($block, "<", $genre_s);
$this->main_genres[$i] = substr ($block, $genre_s + 1, $genre_e - $genre_s - 1);
$i++;
}
}
return $this->main_genres;
}
/** Get colors
* @method colors
* @return array colors (array[0..1] of strings)
*/
function colors () {
if ($this->main_colors == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$color_s = 0;
$color_e = 0;
$i = 0;
while (strpos ($this->page["Title"], "/List?color-info", $color_e) > $color_s) {
$color_s = strpos ($this->page["Title"], "/List?color-info", $color_s);
$color_s = strpos ($this->page["Title"], ">", $color_s);
$color_e = strpos ($this->page["Title"], "<", $color_s);
$this->main_colors[$i] = substr ($this->page["Title"], $color_s + 1, $color_e - $color_s - 1);
$i++;
}
}
return $this->main_colors;
}
/** Get the main tagline for the movie
* @method tagline
* @return string tagline
*/
function tagline () {
if ($this->main_tagline == "") {
$result = $this->retrieveFromPage('Taglines:', '');
if (strpos($result, 'main_tagline = $result;
if ($this->page["Title"] == "") $this->openpage ("Title");
$tag_s = strpos ($this->page["Title"], "Tagline:");
if ( $tag_s == 0) return FALSE;
$tag_s = strpos ($this->page["Title"], ">", $tag_s);
$tag_e = strpos ($this->page["Title"], "<", $tag_s);
$this->main_tagline = substr ($this->page["Title"], $tag_s + 1, $tag_e - $tag_s - 1);
}
return $this->main_tagline;
}
/** Get the main Plot outline for the movie
* @method plotoutline
* @return string plotoutline
*/
function plotoutline () {
if ($this->main_plotoutline == "") {
if ($this->page["Title"] == "") $this->openpage ("Title");
$result = $this->retrieveFromPage('Storyline', '');
$result = strip_tags($result);
//no need more...
return $this->main_plotoutline = $result;
$plotoutline_s = strpos ($this->page["Title"], "Plot:");
if ( $plotoutline_s == 0) return FALSE;
$plotoutline_s = strpos ($this->page["Title"], ">", $plotoutline_s);
$plotoutline_e = strpos ($this->page["Title"], "<", $plotoutline_s);
$this->main_plotoutline = substr ($this->page["Title"], $plotoutline_s + 1, $plotoutline_e - $plotoutline_s - 1);
}
return $this->main_plotoutline;
}
/** Get the movies plot(s)
* @method plot
* @return array plot (array[0..n] of strings)
*/
function plot () {
if ($this->plot_plot == "") {
if ($this->page["Plot"] == "") $this->openpage ("Plot");
$plot_e = 0;
$i = 0;
$this->plot_plot = array();
while (($plot_s = strpos ($this->page["Plot"], "", $plot_e)) !== FALSE)
{
$plot_e = strpos ($this->page["Plot"], " ", $plot_s);
$tmplot = substr ($this->page["Plot"], $plot_s + 19, $plot_e - $plot_s - 19);
$tmplot = str_replace("href=\"/", "href=\"https://". $this->imdbsite ."/", $tmplot);
$this->plot_plot[$i] = $tmplot;
$i++;
}
}
return $this->plot_plot;
}
/** Get all available taglines for the movie
* @method taglines
* @return array taglines (array[0..n] of strings)
*/
function taglines () {
if ($this->taglines == "") {
if ($this->page["Taglines"] == "") $this->openpage ("Taglines");
$tags_e = 0;
$i = 0;
$tags_s = strpos ($this->page["Taglines"], " | ", $tags_e);
$tagend = strpos ($this->page["Taglines"], " |