view php/read/getBookDetails.php @ 38:c24ae74bf6d5

pass char for log lines in to doAmazonRequest
author Charlie Root
date Fri, 04 Jan 2019 18:02:20 -0500
parents e570b199108b
children dbc006408d2b
line wrap: on
line source

<?php

function findGenre($browseNode, &$ID, &$gen)
{
	if($browseNode->Name == "Subjects")
	{
		 return true;		 
	}
	else
	{
		if($browseNode->Ancestors->BrowseNode)
		{
			if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true)
			{
				$gen = $browseNode->Name;
				$ID = $browseNode->BrowseNodeId;
				
				//if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well
				//	return true;
			}		
		}
		return false;		
	}
}

global $out,$isbn;
if(isset($_GET['isbn'])){
	$isbn = $_GET['isbn'];
	if(isset($_GET['locale'])){
	$locale=$_GET['locale'];
	
	switch ($locale) {
    case "uk":
        $loc = 1;
        break;
    case "us":
        $loc = 0;
        break;
    case "ca":
        $loc = 2;
        break;
	case "de":
        $loc = 3;
        break;
	case "fr":
        $loc = 4;
        break;
	case "":
        $loc = 0;
        break;
	}
	}
	else{
	$loc=0;
	}
	$output = "";

	$output .=  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	$output .=  "<BookDetails>";
	
	$proceed = true;
	include "../../../private/db.php";

	$query = "CALL b_getBookInfo('$isbn', $loc)";
	//select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'";
	//echo $query;
	$res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
	
	if ( mysqli_num_rows( $res ) > 0 )
	{
		$rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
		mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
		$timestamp=$rows['Timestamp'];
		//echo $timestamp;
		date_default_timezone_set('UTC');
		$dbStamp = strtotime($timestamp);
		//echo $dbStamp;
		$unixTime = time();
		//echo $unixTime;			
		$timeDiff = $unixTime - $dbStamp;
		//echo $timeDiff;
		
		if($timeDiff < 2592000) //if it has been updated in the last 30 days 
		{	
			$proceed = false;
			$output .=  "<ISBN>" . $isbn . "</ISBN>";
			$output .=  "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";
			$output .=  "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
			$output .=  "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
			$output .=  "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
			$output .=  "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
			$output .=  "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
			$output .=  "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
			$output .=  "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
			$output .=  "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
			$output .=  "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
			$output .=  "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
			$output .=  "<ProductGroup>Book</ProductGroup>";
		}
	}
	else {
	  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
	}
	
	if($proceed == true)
	  {
	    include "aws_signed_request.php";
	    include_once "../../xml/doAmazonRequest.inc";
	    file_put_contents('/var/ywww/debug/phpDebug',
			      "nbd proceeding: $isbn\n",
			      FILE_APPEND);

	    //book does not exist already so look up all the info from browse nodes....

	    $Adefault=array(
			    'language'           =>'en',           //what language to render the page in
			    'locale'             =>'us',           //which server's products? available: ca,de,fr,jp,uk,us
			    'page'               =>1,              //first page to show (we are counting from 1 not 0)
			    'operation'          =>'ItemLookup',   //what to do?	//ItemSearch
			    'searchparameter'    =>'ItemId',       //what kind of search?
			    'searchindex'     => 'Books',
			    'searchparameterdata'=>$isbn,  //what to search for?
			    'search' =>$isbn,
			    //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
			    'show_array'         =>false,          //debug: show complete incoming array? You can use this to see what other information Amazon is sending
			    'show_url'           =>false,          //debug: show XML request url to be send to Amazon?
			    'show_xml'           =>false,          //debug: show incoming XML code from Amazon?
			    );
	    //change the debug options to true if you want to activate them or call the script with '?show_array=true' to see what actual information you're getting from Amazon and how little my standard script is actually showing of it

	    $Aassociates_id=array(
				  'uk' => 'bookwhack-21',
				  'us' => 'your02b-20',
				  'ca' => 'book009-20',
				  'de' => 'book04c-21',
				  'fr' => 'book07f-21',
				  );

	    $Aserver=array(
			   'ca' => array(
					 'ext' => 'ca'                      ,  //Canadian normal server
					 'nor' => 'http://www.amazon.ca'    ,  //Canadian normal server
					 'xml' => 'http://xml.amazon.com'   ,  //Canadian xml server
					 ),
			   'de' => array(
					 'ext' => 'de'                      ,  //German normal server
					 'nor' => 'http://www.amazon.de'    ,  //German normal server
					 'xml' => 'http://xml-eu.amazon.com',  //German xml server
					 ),
			   'fr' => array(
					 'ext' => 'fr'                      ,  //French normal server
					 'nor' => 'http://www.amazon.fr'    ,  //French normal server
					 'xml' => 'http://xml-eu.amazon.com',  //French xml server
					 ),
			   'jp' => array(
					 'ext' => 'jp'                      ,  //Japanese normal server, not co.jp!
					 'nor' => 'http://www.amazon.co.jp' ,  //Japanese normal server
					 'xml' => 'http://xml.amazon.com'   ,  //Japanese xml server
					 ),
			   'uk' => array(
					 'ext' => 'co.uk'                   ,  //UK normal server
					 'nor' => 'http://www.amazon.co.uk' ,  //UK normal server
					 'xml' => 'http://xml-eu.amazon.com',  //UK xml server
					 ),
			   'us' => array(
					 'ext' => 'com'                     ,  //USA normal server
					 'nor' => 'http://www.amazon.com'   ,  //USA normal server
					 'xml' => 'http://xml.amazon.com'   ,  //USA xml server
					 ),
			   );

	    //for all parameters see if the user has overruled it or use the default
	    foreach ($Adefault as $i=>$d) {
	      $$i=isset($_GET[$i])?$_GET[$i]:$d;
	    }

	    $parameters=array(
			      'Operation'       =>$operation              ,
			      'Keywords'        =>urlencode($search)      ,
			      "$searchparameter"=>$searchparameterdata    ,
			      'ItemPage'        =>$page                   ,  //which page?
			      'AssociateTag'    =>$Aassociates_id[$locale],
			      'ResponseGroup'   =>'ItemAttributes,Images,SalesRank,BrowseNodes'
			      );

	    if ($searchindex!='Books') {
	      $parameters['SearchIndex']=$searchindex;
	    }

	    try {
	      $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,6,'d');
	      //use this xml to pull out the necessary information and save it
	      set_error_handler(function () {
		  global $out,$isbn;
		  file_put_contents('/var/ywww/debug/phpDebug',
				    "Caught one bd: ".$isbn,
				    FILE_APPEND);
		  file_put_contents('/var/ywww/debug/phpDebug',
				    print_r($out, TRUE)."\n",
				    FILE_APPEND);
		} );
	      include "../../../private/db.php";
	
	      $title="";
	      $author="";
	      $binding="";
	      $dewey="";
	      $imageURL="";
	      $salesRank="";
	      $pubDate="";
	      $publisher="";
	
	      $title = $xml->Items->Item->ItemAttributes->Title;
	      $author = $xml->Items->Item->ItemAttributes->Author;
	      $binding = $xml->Items->Item->ItemAttributes->Binding;
	      $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
	      if($dewey == "")
		$dewey = "null";
	      $imageURL = $xml->Items->Item->MediumImage->URL;
	      $salesRank = $xml->Items->Item->SalesRank;
	      $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
	      $publisher = $xml->Items->Item->ItemAttributes->Publisher;
	      restore_error_handler();
	      if ($publisher and strlen($publisher)>30) {
		$publisher=substr($publisher,0,30);
	      }
	      if ($author and strlen($author)>30) {
		$author=substr($author,0,30);
	      }
	      if ($title and strlen($title)>100) {
		$title=substr($title,0,100);
	      }
	      $publisher=mysqli_real_escape_string($link,$publisher);
	      $author=mysqli_real_escape_string($link,$author);
	      $title=mysqli_real_escape_string($link,$title);
	      if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
	      if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
	      if (strlen($pubDate)==0) {
		$pubDate="null";
	      }
	      else {
		$pubDate="\"$pubDate\"";
	      }
	      /*echo $title;
	       echo $author;
	       echo $binding;
	       echo $dewey;
	       echo $imageURL;
	       echo $salesRank;
	       echo $pubDate;
	       echo $publisher;*/
		
	      $genreID = "";
	      $genre = "";		
	      $genArr = array();
	      $g1 = "null";
	      $g2 = "null";
	      $g3 = "null";
	      if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){		
		for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){					
		  //sexy recursive function
		  findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
			
		  if($genre != "")
		    $genArr[strval($genreID)] = strval($genre);
		  //$genArr[$i] = array(strval($genreID) => strval($genre));

		  //echo $genre;
		  //echo $genreID;
			
		  $genre = "";
		  $genreID = "";
		}
		
		$g1 = "null";
		$g2 = "null";
		$g3 = "null";
		$loop = 1;
		
		foreach ($genArr as $key => $value) {
		  //echo "$key => $value";
		  if ($key>2047) {
		    //HST added
		    break;
		  }
		  $queryG = "CALL b_addBrowseNode($key,\"$value\")";	//add the name value pair for genre to new table	
		  //echo $queryG;
		  $resG = mysqli_query($link, $queryG);
			
		  switch ($loop) {
		  case 1:
		    $g1 = $key;
		    break;
		  case 2:
		    $g2 = $key;
		    break;
		  case 3:
		    $g3 = $key;
		    break;
		  }
			
		  $loop++;
		}
	      }
		  
	      if($salesRank == "")
		$salesRank = "null";
		
	      mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
	      include "../../../private/db.php";
		
	      $title = strtr($title, '"', "'");	
	      $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)";	
	      //echo $queryInsert;
		
	      $res = mysqli_query($link, $queryInsert);
	      if (!$res) {
		$err=mysqli_error( $link );
		mysqli_close($link);
		file_put_contents('/var/ywww/debug/phpDebug',
				  "anb failed: $err, $pubDate, $g2, $publisher, $title\n",
				  FILE_APPEND);
		exit($err);
	      }
	      mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop		

		
	      $output .=  "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
	      $output .=  "<Title>" . htmlspecialchars($title) . "</Title>";
	      $output .=  "<Author>" . htmlspecialchars($author) . "</Author>";
	      $output .=  "<Binding>" . htmlspecialchars($binding) . "</Binding>";
	      $output .=  "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
	      $output .=  "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
	      $output .=  "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
	      $output .=  "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
	      $output .=  "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
	      $output .=  "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
	      $output .=  "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
	      $output .=  "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";	
	      $output .=  "<ProductGroup>Book</ProductGroup>";
	    }
	    catch (Exception $e) {
	      file_put_contents('/var/ywww/debug/phpDebug',
				"gBD: dAR failed:".$e->getMessage()."\n",
				FILE_APPEND);
	    }
	  }
	$output .=  "</BookDetails>";
	
	echo $output;
}
?>