changeset 7:1dfe64e365a0

a bit more debugging/bomb-proofing
author Robert Boland <robert@markup.co.uk>
date Fri, 17 Feb 2017 13:40:20 +0000
parents 077b0a0a3e6d
children 226f00d9f6f5 970a4834afff
files us/index.php xml/getAmazonInfo.php
diffstat 2 files changed, 54 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/us/index.php	Thu Feb 16 22:29:02 2017 +0000
+++ b/us/index.php	Fri Feb 17 13:40:20 2017 +0000
@@ -150,32 +150,56 @@
 				$recommended = "";
 				$linkedRecommended = "";
 				for($i=0;$i<sizeof($xml->isbn);$i++){
-					$linkedISBN = $xml->isbn[$i];
-					include "../../private/db.php"; 
-					$query = "CALL b_getBookInfo('$linkedISBN', $countryCode)";
-					//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);		
-						$titleL = $rows["Title"];
-						$authorL = $rows["Author"];
-						mysqli_close($link);
-					}		
-					else
-					{					
-						$bookLDetails = getAmazonDet($linkedISBN,1,'us'); 
-						$bookLXML = new SimpleXMLElement($bookLDetails);
-						$titleL = $bookLXML->Items->Item[0]->ItemAttributes->Title;
-						$authorL = $bookLXML->Items->Item[0]->ItemAttributes->Author;
+				  $linkedISBN = $xml->isbn[$i];
+				  include "../../private/db.php"; 
+				  $query = "CALL b_getBookInfo('$linkedISBN', $countryCode)";
+				  //echo $query;
+				  $res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
+				  set_error_handler(function () {
+				      global $bookLDetails, $linkedISBN;
+				      file_put_contents('/var/ywww/debug/phpDebug',
+							"Caught other one?: ".$linkedISBN,
+							FILE_APPEND);
+				      //file_put_contents('/var/ywww/debug/phpDebug',
+				      //		print_r($bookLDetails, TRUE)."\n",
+				      //		FILE_APPEND);
+				    } );
+				  if ( mysqli_num_rows( $res ) > 0 )
+				    {
+				      $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);		
+				      $titleL = $rows["Title"];
+				      $authorL = $rows["Author"];
+				      mysqli_close($link);
+				    }		
+				  else
+				    {					
+				      $bookLDetails = getAmazonDet($linkedISBN,1,'us'); 
+				      $bookLXML = new SimpleXMLElement($bookLDetails);
+				      // HST added this
+				      if (preg_match("<Error>",$bookLDetails)) {
+					$resName=$bookLXML->getName();
+					$code=$bookLXML->Error->Code;
+					file_put_contents('/var/ywww/debug/phpDebug',"Losing 2: $resName, ".
+							  $code."\n",FILE_APPEND);
+					if ($code=='RequestThrottled') {
+					  usleep(200000); // Try to reduce throttling until we get a 
+					  // principled solution in place
 					}
-					if($titleL != "")
-					{
-						$recommended .= " $titleL by $authorL,";
-						$underAuthor = str_replace(" ","_",$authorL);
-						$underTitle = str_replace(" ","_",$titleL);
-						$linkedRecommended .= "<a href=\"http://www.YourNextRead.com/us/#!isbn=$linkedISBN/$underTitle" . "_by_$authorL\">$titleL by $authorL</a>, ";		
-					}					
+					$titleL = "";
+				      }
+				      else {
+					$titleL = $bookLXML->Items->Item[0]->ItemAttributes->Title;
+					$authorL = $bookLXML->Items->Item[0]->ItemAttributes->Author;
+				      }
+				    }
+				  restore_error_handler();
+				  if($titleL != "")
+				    {
+				      $recommended .= " $titleL by $authorL,";
+				      $underAuthor = str_replace(" ","_",$authorL);
+				      $underTitle = str_replace(" ","_",$titleL);
+				      $linkedRecommended .= "<a href=\"http://www.YourNextRead.com/us/#!isbn=$linkedISBN/$underTitle" . "_by_$authorL\">$titleL by $authorL</a>, ";		
+				    }					
 				}
 				
 				$vote = "Vote on these recommendations!";
--- a/xml/getAmazonInfo.php	Thu Feb 16 22:29:02 2017 +0000
+++ b/xml/getAmazonInfo.php	Fri Feb 17 13:40:20 2017 +0000
@@ -118,7 +118,11 @@
 	  $resName=$xml->getName();
 	  $code=$xml->Error->Code;
 	  file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, ".
-			    $xml->Error->Code."\n",FILE_APPEND);
+			    $code."\n",FILE_APPEND);
+	  if ($code=='RequestThrottled') {
+	    usleep(200000); // Try to reduce throttling until we get a 
+	    // principled solution in place
+	  }
 	}
 	else {
 	  $xml = new SimpleXMLElement($output);