Description<\/td> | <\/td> | (.*?)<\/td><\/tr>/i',$resp['data'],$ary)){
$title = trim($ary[1]);
if ($title == '(unknown)')
return FALSE;
else{
$end = strpos($title,'(');
if ($end === FALSE)
return $title;
else{
$title = substr($title,0,$end);
return $title;
}
}
}
return FALSE;
}
function UPCtitle($UPC,$media,$data,$getamazon) {
global $config;
global $CLIENTERROR;
global $data;
$data = array(); //result
$ary = array(); //temp
$asin = array(); //temp
//fetch mainpage
//$resp = httpClient($config['amazonServer'].'/onca/xml3?t=cinemapremier-20&dev-t=D1NKQX9WE8M20P&UpcSearch='.$UPC.'&mode='.strtolower($media).'&type=heavy&f=xml'); # added trailing / to avoid redirect
$string = $config['amazonServer'].'/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1AP035109NRXSS8XWZR2&Operation=ItemLookup&ItemId='.$UPC.'&SearchIndex='.strtoupper($media).'&IdType=UPC&ResponseGroup=Large';
//echo $string;
$resp = httpClient($string); # added trailing / to avoid redirect
//print_r($resp);
if(!$resp['success'])
{
$CLIENTERROR .= $resp['error']."\n";
return false;
}
if (preg_match('/(.*?)<\/Title>/i',$resp['data'],$ary))
{
if ($getamazon)
{
$data = parseamazondata($resp);
return FALSE;
}
else
{
if (preg_match('/(.*?)<\/Asin>/i',$resp['data'],$asin))
{
$data['asin'] = $asin[1];
}
$end = strpos($ary[1],'(');
if ($end === FALSE)
return trim($ary[1]); //return title fround at amazon
else
return trim(substr($ary[1],0,$end)); //return title fround at amazon with (...) removed from title
}
}
return UPCdbtitle($UPC); // return title found at upcdatabase.com or 0 if no title found
}
function amazonTitle($title) {
global $config;
global $CLIENTERROR;
global $data;
$data = array(); //result
$ary = array(); //temp
$asin = array(); //temp
//fetch mainpage
//$resp = httpClient($config['amazonServer'].'/onca/xml3?t=cinemapremier-20&dev-t=D1NKQX9WE8M20P&UpcSearch='.$UPC.'&mode='.strtolower($media).'&type=heavy&f=xml'); # added trailing / to avoid redirect
//$string = $config['amazonServer'].'/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1AP035109NRXSS8XWZR2&Operation=ItemSearch&Title='.$title.'&SearchIndex=Video';
$title = str_replace(' ','+',$title);
$string="http://www.amazon.com/gp/search/ref=sr_adv_d/?search-alias=dvd&field-title=".$title;
//echo $string;
$resp = httpClient($string); # added trailing / to avoid redirect
//print_r($resp);
if(!$resp['success'])
{
$CLIENTERROR .= $resp['error']."\n";
return false;
}
// had to []* for " around the title
// #2 if (preg_match_all('/HREF="\/title\/tt([0-9]+)\/\?[a-zA-Z0-9=;_-]+">([]*.+?[]*)<\/A>/i',$resp['data'],$data,PREG_SET_ORDER)){
// #1 if (preg_match_all('/HREF="\/title\/tt([0-9]+)\/\?[a-zA-Z0-9=;_-]+">(.+?)<\/A>/i',$resp['data'],$data,PREG_SET_ORDER)){
//
// Open Water 2 - Adrift (Widescreen Edition)
if (preg_match_all('/a href="http:\/\/www.amazon.com\/.*?\/dp\/(.*?)\/ref.*?(.*?)<\/span><\/a>/i',$resp['data'],$data,PREG_SET_ORDER))
{
//echo '';
//print_r($data);
//echo ' ';
$i = 0;
foreach ($data as $row)
{
$i++;
$info['id'] =$row[1];
//echo 'info'.$info['id'];
$info['title']=$i.'. '.$row[2];
$ary[]=$info;
}
return $ary;
}
else
{
return false;
}
}
/************************************************************************************
* Split the response data from Amazon into fields
**************************************************************************************/
function parseamazondata ($resp) {
global $config;
if (preg_match('/(.*?)<\/Asin>/i',$resp['data'],$ary)){
$data['asin'] = $ary[1];
}
if (preg_match('/(.*?)<\/Upc>/i',$resp['data'],$ary)){
$data['upc'] = $ary[1];
}
//Titles
if (preg_match('/(.*?)<\/Title>/i',$resp['data'],$ary)){
$end = strpos($ary[1],'(');
if ($end === FALSE)
$data['title'] = trim($ary[1]);
else
$data['title'] = trim(substr($ary[1],0,$end));
$quote = '"';
$sub = substr($data['title'],0,5);
if ($sub == $quote)
$data['title'] = substr($data['title'],5);
$sub = substr($data['title'],count($data['title'])-6,5);
if ($sub == $quote)
$data['title'] = substr($data['title'],0,-5);
/*
* Check if the title starts with 'The' or 'A'
*/
$the = 'The ';
$sub = substr($data['title'],0,4);
if ($sub == $the)
$data['title'] = substr($data['title'],4).', The';
else {
$a = 'A ';
$sub = substr($data['title'],0,2);
if ($sub == $a)
$data['title'] = substr($data['title'],2).', A';
}
}
//Year
if (preg_match('/(.*?)<\/TheatricalReleaseDate>/i',$resp['data'],$ary)){
$end=strpos($ary[1],'-');
if ($end === FALSE)
$data['year'] = trim($ary[1]);
else
$data['year'] = trim(substr($ary[1],0,$end));
//if ($config['append'] == 1)
$data['title'] .= ' ('.$data['year'].')';
}
//Cover-URL
if (preg_match('/.*?(.*?)<\/URL>/i',$resp['data'],$ary)){
$data['coverurl'] = trim($ary[1]);
$Dat = GetFileName( $data['coverurl'], true); //get the file-name of the URL i.e. 13m.jpg
$savDat = $Dat;
$Dat = str_replace('%','_',$Dat);
if (!is_dir($config['cover_image'])){ // check if directory to save the images exists
//echo ' Error: DVD cover image directory does not exist. Path defined in config.inc.php.';
//echo ' Defaulted directory is your root.';
$config['cover_image'] = "";
}
$FileName = $config['cover_image'].$Dat;
$savFileName = $config['cover_image'].$savDat;
if (file_exists($FileName)){ // if we already have the cover
$data['coverurl'] = $FileName;
}
else if (DownloadFile($data['coverurl'], $savFileName)){
if (file_exists($savFileName)) // if we were able to download and save the cover, use it
{
// rename file to not have any possible '%' in the name
rename($savFileName, $FileName);
$data['coverurl'] = $FileName;
}
else // otherwise, use the default no cover image
$data['coverurl'] = $config['cover_image'].'no_cover.jpg';
}
}
//MPAA Rating
if (preg_match('/(.*?)<\/AudienceRating>/i',$resp['data'],$ary)){
$data['mpaa'] = trim($ary[1]); //$data[mpaa] has the entire description
$data['mpaashort'] = explode(" ",$data['mpaa']);
$data['mpaashort'][1] =$data['mpaashort'][0]; // $data[mpaashort][1] needs to have the rating to insert it into the database
}
//Director
if (preg_match('/(.*?)<\/Director>/i',$resp['data'],$ary)){
$data['director'] = trim($ary[1]);
}
//Rating
if (preg_match('/(.*?)<\/AverageRating>/i',$resp['data'],$ary)){
$rating = trim($ary[1]) * 2; //Amazon is on a scale of 1-5 and imdb is 1-10 so I improvise
$data['userrating'] = $rating;
}
//Genres (as Array)
if (preg_match_all('/.*?(.*?)<\/Name>/i',$resp['data'],$ary,PREG_PATTERN_ORDER))
{
$amazonGenres = array ("Classics","Educational","Fitness & Yoga","Special Interes","Sports","Suspense","Television");
$omitArray = array ("Genres","Custom Stores","Specialty Stores","DVD","Video","General","Actors & Actresses","Directors","Today's Deals in DVD","Studio Specials",
"Titles","Refinements","Widescreen","Full Screen");
$genrearray = array();
For($i=0; isset($ary[0][$i]); $i++)
{
$notgenre = strpos($ary[1][$i], ',');
if ($notgenre !== false)
continue; // if it has a comma, it is a name and not a genre so ignore it
$notgenre = strpos($ary[1][$i], '(');
if ($notgenre !== false)
continue; // if it has a (, ignore it
if (in_array(trim($ary[1][$i]), $omitArray))
continue; // do not add if it is in the omit array
$genre = trim(substr($ary[1][$i],0,15));
//echo ' '.$genre;
switch ($genre)
{
case "Westerns":
$genre = "Western";
break;
case "Action & Advent":
$genre = "Action";
break;
case "Kids & Family":
$genre = "Family";
break;
case "Military & War":
$genre = "War";
break;
case "Musicals & Perf":
$genre = "Musical";
break;
case "Music Video & C":
$genre = "Music";
break;
case "Mystery & Suspe":
$genre = "Mystery";
break;
case "Science Fiction":
$genre = "Sci-Fi";
break;
case "Musicals & Perf":
$genre = "Musical";
break;
}
if (in_array($genre, $genrearray))
continue; // do not add duplicates
$genrearray[] = $genre;
}
$data[numofgenres] = 0;
foreach($genrearray as $genre)
{
/*
* if genre is not already in config list, then add it to additional genres (genres_labels) in the options table
*/
$genrefound = FALSE;
foreach ($config['genres_labels'] as $val)
{
if (strtoupper($genre) == trim(strtoupper($val)))
{
$genrefound = TRUE;
break;
}
} // end for each
if (!$genrefound)
{
if (in_array($genre,$amazonGenres))
{
$result = mysql_query ("SELECT genres_labels FROM $config[options]"); // retrieve all configuration options from the database
$row = mysql_fetch_array( $result);
$genretypes = $row['genres_labels'];
if ($genretypes) // if there are other genres in the table
$genretypes .= "\r\n".$genre;
else
$genretypes .= $genre;
$sql = "UPDATE $config[options] SET ";
$sql .= "genres_labels=".escit($genretypes); // text area of media types
$result = mysql_query("$sql");
$count = count($config['genres_labels']);
array_push($config['genres_labels'],$genre);
array_push($config['genres_values'],$genre);
}
else continue;
}
$data['genres'][] = $genre;
$data[numofgenres]++;
} // end for each
} // end if genre
//Cast
if (preg_match_all('/(.*?)<\/Actor>/i',$resp['data'],$ary,PREG_PATTERN_ORDER)){
$data['numofactors']=0;
while(isset($ary[0][ $data['numofactors']])) {
$data['actor'][] = trim($ary[1][$data['numofactors']]);
$data['numofactors']++;
}
}
//Plot
if (preg_match('/.*?(.*?)<\/Content>/i',$resp['data'],$ary)){
$data['plot'] = trim($ary[1]);
}
else if (preg_match('/.*?(.*?)<\/Content>/i',$resp['data'],$ary)){
$data['plot'] = trim($ary[1]);
}
$data['plot'] = preg_replace(' ', "", $data['plot']);
$string = "<I>";
$data['plot'] = str_replace($string, "", $data['plot']);
$string = "</I>";
$data['plot'] = str_replace($string, "", $data['plot']);
//Language
preg_match_all('/.*?(.*?)<\/Name>.*?Original Language<\/Type>.*?/i',$resp['data'],$ary,PREG_PATTERN_ORDER);
$data['language'] = trim(join(', ',$ary[1]));
foreach($ary[1] as $item)
{
$item = trim(substr($item,0,15));
/*
* if language is not already in config list, then add it to languages in the options table
*/
$found = FALSE;
foreach ($config['languages'] as $val)
{
if (strtoupper($item) == trim(strtoupper($val)))
{
$found = TRUE;
break;
}
} // end for each
if (!$found)
{
$result = mysql_query ("SELECT addlanguages FROM $config[options]"); // retrieve all configuration options from the database
$row = mysql_fetch_array( $result);
$itemtypes = $row['addlanguages'];
if ($itemtypes) // if there are other genres in the table
$itemtypes .= "\r\n".$item;
else
$itemtypes .= $item;
$sql = "UPDATE $config[options] SET ";
$sql .= "addlanguages=".escit($itemtypes); // text area of media types
$result = mysql_query("$sql");
array_push($config['languages'], $item);
}
} // end for each
//Runtime
preg_match('/([0-9,]+)<\/RunningTime>/si',$resp['data'],$ary);
$data['runtime'] = trim($ary[1]);
//echo '';
//print_r($data);
//echo ' ';
return $data;
}
/**
* Search a Movie
*
* Searches for a given title on the IMDB and returns the found links in
* an array
*
*/
function imdbTitle($title) {
global $config;
global $CLIENTERROR;
global $titlefound;
$titlefound = 0;
$resp = httpClient($config['imdbServer'].'/find?q='.urlencode($title));
if(!$resp['success']) $CLIENTERROR .= $resp['error']."\n";
$single = array();
/*
echo 'respons is ['.$resp['data'].'] ';
print_r($resp);
echo 'url = ['.$resp['url'].']';
*/
if (preg_match('/'.preg_quote($config['imdbServer'],'/').'\/[Tt]itle\/tt([0-9]+)\//',$resp['url'],$single)){
$titlefound = 1;
$ary[0]['id'] =$single[1];
$ary[0]['title']=$title;
$data = parsedata($resp, $ary[0]['id']);
return $data;
}
// had to []* for " around the title
// #2 if (preg_match_all('/HREF="\/title\/tt([0-9]+)\/\?[a-zA-Z0-9=;_-]+">([]*.+?[]*)<\/A>/i',$resp['data'],$data,PREG_SET_ORDER)){
// #1 if (preg_match_all('/HREF="\/title\/tt([0-9]+)\/\?[a-zA-Z0-9=;_-]+">(.+?)<\/A>/i',$resp['data'],$data,PREG_SET_ORDER)){
// if (preg_match_all('/HREF="\/title\/tt([0-9]+)\/">([]*.+?[]*)<\/A>(.+?)<\/td>/i',$resp['data'],$data,PREG_SET_ORDER)){
if (preg_match_all('/link=\/title\/tt([0-9]+)\/\';">([^<][]*.+?[]*)<\/A>(.+?)<\/td>/i',$resp['data'],$data,PREG_SET_ORDER)){
$i = 0;
foreach ($data as $row) {
$i++;
$info['id'] =$row[1];
//echo 'info'.$info['id'];
$info['title']=$i.'. '.$row[2].$row[3];
$ary[]=$info;
}
return $ary;
}
else {
return false;
}
}
/******************************************************************************
* Fetches the data for a given IMDB-ID
*
*/
function imdbId($imdbID) {
//echo ' in imdbID = '.$imdbID;
global $config;
global $CLIENTERROR;
global $titlefound;
$data= array(); //result
$ary = array(); //temp
//fetch mainpage
$resp = httpClient($config['imdbServer'].'/title/tt'.$imdbID.'/'); # added trailing / to avoid redirect
//echo ' success = '. $resp['success'].'header = '.$resp['header'];
if(!$resp['success']){
$CLIENTERROR .= $resp['error']."\n";
return false;
}
//echo ' go parsedata';
$data = parsedata($resp, $imdbID);
return $data;
}
/************************************************************************************
* Split the response data into fields
**************************************************************************************/
function parsedata ($resp, $imdbID) {
global $config;
// echo ' in parsedata';
$data['id'] = $imdbID;
$resp['data'] = preg_replace("/\x0D/",'',$resp['data']);
$resp['data'] = preg_replace("/\x0A/",'',$resp['data']);
$resp['data'] = preg_replace("/\x09/",'',$resp['data']);
//Titles
preg_match('/(.*?) \([1-2][0-9][0-9][0-9].*?\).*?<\/TITLE>/i',$resp['data'],$ary);
/*
echo 'ary = ';
print_r($ary);
echo ' ';
*/
list($t,$s) = split(" - ",trim($ary[1]),2);
$data['title'] = trim($t);
$quote = '"';
$sub = substr($data['title'],0,5);
if ($sub == $quote)
$data['title'] = substr($data['title'],5);
$sub = substr($data['title'],count($data['title'])-6,5);
if ($sub == $quote)
$data['title'] = substr($data['title'],0,-5);
/*
* Check if the title starts with 'The' or 'A'
*/
$the = 'The ';
$sub = substr($data['title'],0,4);
if ($sub == $the)
$data['title'] = substr($data['title'],4).', The';
else {
$a = 'A ';
$sub = substr($data['title'],0,2);
if ($sub == $a)
$data['title'] = substr($data['title'],2).', A';
}
$data['subtitle'] = trim($s);
//Year
preg_match('/([1-2][0-9][0-9][0-9])<\/A>/i',$resp['data'],$ary);
$data['year'] = trim($ary[1]);
//if ($config['append'] == 1)
$data['title'] .= ' ('.$data['year'].')';
//Cover-URL
preg_match('/name="poster".*?(http:\/\/.+?\.(jpe?g|gif))/i',$resp['data'],$ary);
$data['coverurl'] = trim($ary[1]);
$Dat = GetFileName( $data['coverurl'], true); //get the file-name of the URL i.e. 13m.jpg
if (!is_dir($config['cover_image'])){ // check if directory to save the images exists
//echo ' Error: DVD cover image directory does not exist. Path defined in config.inc.php.';
//echo ' Defaulted directory is your root.';
$config['cover_image'] = "";
}
$FileName = $config['cover_image'].$data['id']."-".$Dat;
if (file_exists($FileName)){ // if we already have the cover
$data['coverurl'] = $FileName;
}
else if (DownloadFile($data['coverurl'], $FileName)){
if (file_exists($FileName)) // if we were able to download and save the cover, use it
$data['coverurl'] = $FileName;
else // otherwise, use the default no cover image
$data['coverurl'] = $config['cover_image'].'no_cover.jpg';
}
//MPAA Rating
preg_match('/MPAA<\/A>:.*?(Rated.*?)<\/div>/i',$resp['data'],$ary);
$data['mpaa'] = trim($ary[1]); //$data[mpaa] has the entire description
$data['mpaashort'] = explode(" ",$data['mpaa']); // $data[mpaashort][1] has the rating
// Certification if MPAA is blank
if (!$data['mpaa']){
$num = preg_match_all('//i',$resp['data'],$ary,PREG_PATTERN_ORDER);
$data['certification'] = trim($ary[1][$num-1]);
}
//Runtime
preg_match('/Runtime:?<\/h5>:?.*?([0-9,]+).*?<\/div>/si',$resp['data'],$ary);
$data['runtime'] = preg_replace('/,/', '', trim($ary[1]));
//Director
preg_match('/Directors:<\/h5>(.*?)<\/A>/i',$resp['data'],$ary);
if ($ary[1])
$data['director'] = trim($ary[1]);
else
{
preg_match('/Director:<\/h5>(.*?)<\/A>/i',$resp['data'],$ary);
if ($ary[1])
$data['director'] = trim($ary[1]);
}
//Rating
preg_match('/ | ... | Shrek (voice) |
preg_match_all('/(.*?)<\/A>.*?(\.\.\. )?<\/TD> | (.*?)<\/TD>/i',$resp['data'],$ary,PREG_PATTERN_ORDER);
$data['numofactors']=0;
while(isset($ary[2][ $data['numofactors']])) {
$character = $ary[4][ $data['numofactors']];
if (preg_match("/^(.*?)<\/a>/i", $character,$matches)) $character = $matches[1];
$data['actor'][] = trim(strip_tags($ary[2][ $data['numofactors']]));
$data['role'][] = trim(strip_tags($character));
$data['numofactors']++;
}
//fetch Plot
$save = $resp['data'];
$resp = httpClient($config['imdbServer'].'/title/tt'.$imdbID.'/plotsummary');
if(!$resp['success'])
{
preg_match('/ |