changeset 36:e570b199108b

use doAmazonRequest
author Charlie Root
date Fri, 04 Jan 2019 12:59:40 -0500
parents 86f79bc1d142
children 633402a4995d
files php/read/getBookDetails.php
diffstat 1 files changed, 227 insertions(+), 289 deletions(-) [+]
line wrap: on
line diff
--- a/php/read/getBookDetails.php	Fri Jan 04 12:54:12 2019 -0500
+++ b/php/read/getBookDetails.php	Fri Jan 04 12:59:40 2019 -0500
@@ -101,312 +101,250 @@
 	else {
 	  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
 	}
-	include "../../../private/db.php";
 	
 	if($proceed == true)
-	{
-		include "aws_signed_request.php"; 
-		//book does not exist already so look up all the info from browse nodes....
+	  {
+	    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
+	    $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',
-		);
+	    $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
-		  ),
-		);
-		$public_key ="AKIAIHTNWC7L6LOUY4LQ";
-		$private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";			
+	    $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;
-		}
+	    //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;
+	    }
 
-		  $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') {
-		    // HST did this
-		    // Used to be in $parameters init above, but
-		    // that caused an error:
-		    // If idType equals ASIN, SearchIndex cannot be present
-		    //'SearchIndex'     =>$searchindex      ,  //Books for example.
-		      $parameters['SearchIndex']=$searchindex;
-		  }
-
-
-		$ext=$Aserver[$locale]['ext'];  
-		$file_data=$ext;
-		ksort($parameters);
-		foreach ($parameters as $i=>$d) {
-		  $file_data.='&'.$i.'='.$d;
-		}
-
-		$url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
-		//echo $url;
+	    try {
+	      $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,3);
+	      //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));
 
-		$crl = curl_init();
-		$timeout = 5;
-		curl_setopt ($crl, CURLOPT_URL,$url);
-		curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 
-		curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
-		curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
-  	        $gotit=0;
-		$requestIP = $_SERVER['REMOTE_ADDR'];
-		$semaphore = new SyncSemaphore("Amazon");
-		$gotit = $semaphore->lock(1000);
-		if (!$gotit) {
-		  file_put_contents('/var/ywww/debug/phpDebug',
-           	  "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND);
-		  $gotit=$semaphore->lock(1000);
-		  file_put_contents('/var/ywww/debug/phpDebug',
-		    "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND);
-		}
-		$out = curl_exec($crl);
-		curl_close($crl);
-		usleep(500000);
-		if ($gotit) {
-		  $semaphore->unlock();
+		  //echo $genre;
+		  //echo $genreID;
+			
+		  $genre = "";
+		  $genreID = "";
 		}
-		else {
-		  file_put_contents('/var/ywww/debug/phpDebug',
-				    "W/o lock for $requestIP 2b\n",FILE_APPEND);
-		}
-		//echo $out;
-		$mm=array();
-		if (preg_match("/<Error>/",$out,$mm)) {
-		  $xml = new SimpleXMLElement($out);	
-		  $resName=$xml->getName();
-		  $code=$xml->Error->Code;
-		  if ($code) {
-		    $message=$xml->Error->Message;
-		  }
-		  else {
-		    $code=$xml->Items->Request->Errors->Error->Code;
-		    $message=$xml->Items->Request->Errors->Error->Message;
-		  }
-		  $requestIP = $_SERVER['REMOTE_ADDR'];
-		  file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: ".$mm[0].", $resName, $code, $requestIP\n",FILE_APPEND);
-		  if ($code=='RequestThrottled') {
-		    usleep(200000); // Try to reduce throttling until we get a 
-		    // principled solution in place
+		
+		$g1 = "null";
+		$g2 = "null";
+		$g3 = "null";
+		$loop = 1;
+		
+		foreach ($genArr as $key => $value) {
+		  //echo "$key => $value";
+		  if ($key>2047) {
+		    //HST added
+		    break;
 		  }
-		  else {
-		    file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND);
-		    if ($code=="") {
-		      file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$out\n",FILE_APPEND);
-		    }
+		  $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++;
 		}
-		else {
-		  $xml = new SimpleXMLElement($out);
-		  //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);
-		    } );
-	
-		  $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;*/
+	      }
+		  
+	      if($salesRank == "")
+		$salesRank = "null";
 		
-		  $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 = "";
-		    }
+	      mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
+	      include "../../../private/db.php";
 		
-		    $g1 = "null";
-		    $g2 = "null";
-		    $g3 = "null";
-		    $loop = 1;
+	      $title = strtr($title, '"', "'");	
+	      $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)";	
+	      //echo $queryInsert;
 		
-		    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		
+	      $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>";
-		}
-	}
-	
+	      $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;