Location: PHPKode > projects > TVEz - Media Library > tvez-0.2.1/html/admin/imdb_lib.php
<?PHP

# ------------------------------------------------------------------------------
#
# Contains all the php IMDB funcionality
#
# ------------------------------------------------------------------------------
#
# Copyright (C) 2003 Christian Eheim and Alex Pachikov
#
# This file is part of TVEz (tvez.sourceforge.net).
#
# TVEz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# TVEz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TVEz; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ------------------------------------------------------------------------------
#
# Created on 12/07/2003 by Christian Eheim (hide@address.com)
#
# LAST MODIFIED:
# $Date: 2004/09/12 04:06:32 $
# $Revision: 1.8 $
# $Author: eheim $
#
# ------------------------------------------------------------------------------

################################################################################
# Parses the source of an IMDB movie page
################################################################################
function parse_imdb_source($source) {

	#########################################################
	# ID
	#########################################################
	if ( preg_match("/<input type=\"hidden\" name=\"arg\" value=\"([0-9]{7})\">/i",$source,$magicNum) )
		$movie['id'] = $magicNum[1];

	#########################################################
	# Title and Year
	#########################################################
	if ( preg_match("/<TITLE>(.*) \(([0-9\?]{4})(\/I)?\)( \([A-Z]*\))?<\/TITLE>/i", $source, $titles) ) {
		$movie['title'] = $titles[1];
		$movie['year'] = $titles[2];
	}

	#########################################################
	# Image
	#########################################################
	if ( preg_match("/<IMG BORDER=\"0\" ALT=\"cover\" SRC=\"(.*)\" height/i",$source,$images) ) {
		$movie['image'] = $images[1];
		$ai = split("\.", $images[1]);
		$movie['imgext'] = end($ai);
	}

	#########################################################
	# Amazon link
	#########################################################
	if ( preg_match("/><a href=\"(\/r\/[^\"]*)\" title=\"DVD/i", $source, $amazon) )
		$movie['amazon'] = $amazon[1];
	else if ( preg_match("/><a href=\"(\/r\/[^\"]*)\" title=\"VHS/", $source, $amazon) )
		$movie['amazon'] = $amazon[1];
	else
		$movie['amazon'] = "";

	#########################################################
	# Director
	#
	# <b class="blackcatheader">Directed by</b><br>
	# <a href="/name/nm0001716/">Tony Scott</a><br>
	#########################################################
	if ( preg_match("/<B CLASS=\"blackcatheader\">Directed by<\/B><BR>\\n(.*)/i",$source,$allDirecs) ) {
		$mat = spliti("<br>", $allDirecs[1]);
		foreach ($mat as $direc)
			if ( preg_match("/<A HREF=\"\/Name.*\">(.*)<\/A>/i", $direc, $alls) )
				$directors .= $alls[1]."/";
		$directors = substr($directors,0,-1);
		$movie['director'] = $directors;
	}

	# At this point we are getting rid of new lines
	$source = preg_replace("/\\n/","",$source);

	#########################################################
    # Writers
	#########################################################
	if ( preg_match("/<b class=\"blackcatheader\">Writing credits(.*)<b class=\"ch\">Genre/",$source,$aw) ) {
		$awri = spliti("<br>", $aw[1]);
		$tester = array();
		$writer = "";
		foreach ($awri as $wri) {
			if ( preg_match("/<A HREF=\"\/Name[^\"]*\">([^<]*)<\/A>/i", $wri, $mw) ) {
				if (!isset($tester[$mw[1]])) {
					$mw[1] = preg_replace("/\(.*\)/","",$mw[1]);
					$writer .= $mw[1]."/";
					$tester[$mw[1]] = 1;
				}
			}
		}
		$writer = substr($writer,0,-1);
		$movie['writers'] = $writer;
	} 

	#########################################################
	# Genres
	#########################################################
	if ( preg_match("/<B CLASS=\"ch\">Genre:<\/B>\s*(.*)<b class=\"ch\">Tagline/i", $source, $gg) ) {
		$genre = "";
		$ggs = spliti(" \/ ", $gg[1]);
		foreach ($ggs as $g) {
			if ( preg_match("/<A HREF=\"\/Sections\/Genres\/[^\"]*\">([^<]*)<\/A>/i", $g, $genres) )
				//$genres[1] = preg_replace("/\(.*\)/","",$genres[1]);
				$genre .= $genres[1]."/";
		}
		$genre = substr($genre,0,-1);
		$movie['genres'] = $genre;
	}

	#########################################################
	# Actors
	#########################################################
	if ( preg_match("/<B CLASS=\"blackcatheader\">C[^<]*<\/B><\/TD><\/TR>(.*)>&nbsp;&nbsp;</i", $source, $aa) ) {
		$aact = spliti("</TR><TR>", $aa[1]);
		$actors = "";
		foreach ($aact as $act) {
			if ( preg_match("/<A HREF=\"\/Name[^\"]*\">([^<]*)<\/A>[^\.]*\.\.\.\. <\/td><td valign=\"top\">([^<]*)<\/td>/i", $act, $a) ) {
				$a[2] = preg_replace("/\//","|",$a[2]);
				$actors .= $a[1]."...".$a[2]."/";
			}
		}
		$actors = substr($actors,0,-1);
		$movie['actors'] = $actors;
	}

	#########################################################
	# Runtime
	#########################################################
	if ( preg_match("/<B CLASS=\"ch\">Runtime:<\/B> *(?:[A-Za-z]*:)?([0-9]*) min/i", $source, $rt) ) {
			$movie['time'] = $rt[1];
	}

	#########################################################
	# AKA
	#########################################################
	if ( preg_match("/<B CLASS=\"ch\">Also Known As<\/b>:<\/b>(.*)<B CLASS=\"ch\">Runtime/i", $source, $aaka) ) {
		$aka = preg_replace("/\//", "-", $aaka[1]);
		$aka = preg_replace("/ <br>/", "/", $aka);
		$aka = preg_replace("/<br>/", "", $aka);
		$aka = preg_replace("/<b class.*$/i", "", $aka);
		$aka = preg_replace("/\/$/", "", $aka);
		$movie['aka'] = $aka;
	}

	#########################################################
	# Country, Language
	#########################################################
	if ( preg_match("/<B CLASS=\"ch\">Country:<\/B>\s*<A HREF=\"\/Sections\/Countries\/\w+\/\">(\w+)[<]*<\/A>/i", $source, $cl) ) {
		$movie['country'] = $cl[1];
	}
	if ( preg_match("/<B CLASS=\"ch\">Language:<\/B>\s*<A HREF=\"\/Sections\/Languages\/\w+\/\">(\w+)[<]*<\/A>/i", $source, $cl) ) {
		$movie['language'] = $cl[1];
	}

	#########################################################
	# Get todays date
	#########################################################
	$movie['date'] = $today = date("Y-m-d");

	return $movie;
}

################################################################################
# Get the plot from IMDB for a movie id
################################################################################
function get_imdb_plot($id) {
	global $IMDB_HTTP;
	
	$plotpage = http_request($IMDB_HTTP, 80, 15, "/title/tt$id/plotsummary");

	$plotpage = preg_replace("/\n/", " ", $plotpage);
	$plotpage = preg_replace("/\|/", "/", $plotpage);
	if ( preg_match("/.*<P CLASS=\"plotpar\">(.*)<\/P> <P ALIGN=\"CENTER\">Summary written by/i", $plotpage, $plot) )
		return $plot[1];
	else
		return null;
}

################################################################################
# Search IMDB
################################################################################
function search_imdb($search, $path=null) {
	global $IMDB_HTTP;

	$search = split("/", $search);
	$search = end($search);
	# Remove extension
	$search = preg_replace("/\..{2,4}$/", "", $search);
	# Remove dash separation and pick the end
	$search = preg_replace("/^.* - /", "", $search);
	# Replace multi part info
	$search = preg_replace("/\(.*\)/", "", $search);
	# Substitute periods with spaces
	$search = preg_replace("/\./", "%20", $search);
	$search = preg_replace("/'/", "\'", $search);
	$search = preg_replace("/ /", "%20", $search);
																			
	$text = http_request($IMDB_HTTP, 80, 15, "/find?q=$search");

	if ( !preg_match("/main content/i", $text) && !preg_match("/Email this page to a friend/i", $text) ) {
		$lines = split("\n", $text);
		$found = 0;
		foreach ($lines as $line) {
			if (preg_match("/<A HREF=\"\/title\/tt([0-9]{7})\/\">(.*)<\/A>/i", $line, $matches) ) {
				$ids[$matches[1]] = $matches[2];
				$found++;
			}
		}
		$s = urldecode($search);
		if ($found > 0)
			print_multiple_form($ids,$s,$path);
		else
			exit_error(localize_string("Did not find any movies for '==1=='", $s));
		exit;
	}
	else
		return array($text,$path);
}

################################################################################
# Prints the form to select from multiple matches
################################################################################
function print_multiple_form($ids,$search,$path) {

	global $IMDB_HTTP;
	echo '
		<form method="POST">
				<h2 align="center">'.localize_string("Multiple matches found for '==1=='", $search).'</h2>
		<table class="multi" cellpadding="2" cellspacing="0" align="center">
			';
	$num = 1;
	foreach ($ids as $id => $title) {
		if ($num%2==0) $class="multilight"; else $class="multidark";
		$encpath = urlencode($path);
		echo "
			<tr class=\"$class\">
				<td align=right><a href=\"?content=admin&page=movies&path=$encpath&id=$id&queryimdb=1\">$num</a>:</td>
				<td>($id)</td>
				<td>[<a href=$IMDB_HTTP/title/tt$id target=_blank title=\"IMDB\">?</a>]</td>
				<td>$title</td>
			</tr>";
		$num++;
	}
	echo '
		</table>
		<br>
		<table align="center">
			<tr><td>
			<input type="hidden" name="path" value="'.$path.'">
			'.localize_string("Choose a number from above").'
			<select name=number onchange="javascript:this.form.submit()">
			<option value=\'\'> - </option>
			';
			$num = 1;
    foreach ($ids as $id => $title) {
		echo "<option value='$id'>$num</option>";
        $num++;
    }
			echo '
			</select>
			<br>'.localize_string("or enter the 7-digit IMDB ID").'
			<input type="text" name="id" size="7" maxlength="7">
			<input type="hidden" name="queryimdb" value="1">
			<input type="submit" value="Go">
			</td></tr>
		</table>
		<input type="hidden" name="content" value="admin">
		<input type="hidden" name="page" value="movies">
		</form>
';

}

?>
Return current item: TVEz - Media Library