comparison php/read/getBookDetails.php @ 36:e570b199108b

use doAmazonRequest
author Charlie Root
date Fri, 04 Jan 2019 12:59:40 -0500
parents 5cae8d572998
children c24ae74bf6d5
comparison
equal deleted inserted replaced
35:86f79bc1d142 36:e570b199108b
99 } 99 }
100 } 100 }
101 else { 101 else {
102 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop 102 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
103 } 103 }
104 include "../../../private/db.php";
105 104
106 if($proceed == true) 105 if($proceed == true)
107 { 106 {
108 include "aws_signed_request.php"; 107 include "aws_signed_request.php";
109 //book does not exist already so look up all the info from browse nodes.... 108 include_once "../../xml/doAmazonRequest.inc";
110 109 file_put_contents('/var/ywww/debug/phpDebug',
111 $Adefault=array( 110 "nbd proceeding: $isbn\n",
112 'language' =>'en', //what language to render the page in 111 FILE_APPEND);
113 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us 112
114 'page' =>1, //first page to show (we are counting from 1 not 0) 113 //book does not exist already so look up all the info from browse nodes....
115 'operation' =>'ItemLookup', //what to do? //ItemSearch 114
116 'searchparameter' =>'ItemId', //what kind of search? 115 $Adefault=array(
117 'searchindex' => 'Books', 116 'language' =>'en', //what language to render the page in
118 'searchparameterdata'=>$isbn, //what to search for? 117 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
119 'search' =>$isbn, 118 'page' =>1, //first page to show (we are counting from 1 not 0)
120 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' 119 'operation' =>'ItemLookup', //what to do? //ItemSearch
121 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending 120 'searchparameter' =>'ItemId', //what kind of search?
122 'show_url' =>false, //debug: show XML request url to be send to Amazon? 121 'searchindex' => 'Books',
123 'show_xml' =>false, //debug: show incoming XML code from Amazon? 122 'searchparameterdata'=>$isbn, //what to search for?
124 ); 123 'search' =>$isbn,
125 //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 124 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
126 125 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending
127 $Aassociates_id=array( 126 'show_url' =>false, //debug: show XML request url to be send to Amazon?
128 'uk' => 'bookwhack-21', 127 'show_xml' =>false, //debug: show incoming XML code from Amazon?
129 'us' => 'your02b-20', 128 );
130 'ca' => 'book009-20', 129 //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
131 'de' => 'book04c-21', 130
132 'fr' => 'book07f-21', 131 $Aassociates_id=array(
133 ); 132 'uk' => 'bookwhack-21',
134 133 'us' => 'your02b-20',
135 $Aserver=array( 134 'ca' => 'book009-20',
136 'ca' => array( 135 'de' => 'book04c-21',
137 'ext' => 'ca' , //Canadian normal server 136 'fr' => 'book07f-21',
138 'nor' => 'http://www.amazon.ca' , //Canadian normal server 137 );
139 'xml' => 'http://xml.amazon.com' , //Canadian xml server 138
140 ), 139 $Aserver=array(
141 'de' => array( 140 'ca' => array(
142 'ext' => 'de' , //German normal server 141 'ext' => 'ca' , //Canadian normal server
143 'nor' => 'http://www.amazon.de' , //German normal server 142 'nor' => 'http://www.amazon.ca' , //Canadian normal server
144 'xml' => 'http://xml-eu.amazon.com', //German xml server 143 'xml' => 'http://xml.amazon.com' , //Canadian xml server
145 ), 144 ),
146 'fr' => array( 145 'de' => array(
147 'ext' => 'fr' , //French normal server 146 'ext' => 'de' , //German normal server
148 'nor' => 'http://www.amazon.fr' , //French normal server 147 'nor' => 'http://www.amazon.de' , //German normal server
149 'xml' => 'http://xml-eu.amazon.com', //French xml server 148 'xml' => 'http://xml-eu.amazon.com', //German xml server
150 ), 149 ),
151 'jp' => array( 150 'fr' => array(
152 'ext' => 'jp' , //Japanese normal server, not co.jp! 151 'ext' => 'fr' , //French normal server
153 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server 152 'nor' => 'http://www.amazon.fr' , //French normal server
154 'xml' => 'http://xml.amazon.com' , //Japanese xml server 153 'xml' => 'http://xml-eu.amazon.com', //French xml server
155 ), 154 ),
156 'uk' => array( 155 'jp' => array(
157 'ext' => 'co.uk' , //UK normal server 156 'ext' => 'jp' , //Japanese normal server, not co.jp!
158 'nor' => 'http://www.amazon.co.uk' , //UK normal server 157 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server
159 'xml' => 'http://xml-eu.amazon.com', //UK xml server 158 'xml' => 'http://xml.amazon.com' , //Japanese xml server
160 ), 159 ),
161 'us' => array( 160 'uk' => array(
162 'ext' => 'com' , //USA normal server 161 'ext' => 'co.uk' , //UK normal server
163 'nor' => 'http://www.amazon.com' , //USA normal server 162 'nor' => 'http://www.amazon.co.uk' , //UK normal server
164 'xml' => 'http://xml.amazon.com' , //USA xml server 163 'xml' => 'http://xml-eu.amazon.com', //UK xml server
165 ), 164 ),
166 ); 165 'us' => array(
167 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; 166 'ext' => 'com' , //USA normal server
168 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; 167 'nor' => 'http://www.amazon.com' , //USA normal server
169 168 'xml' => 'http://xml.amazon.com' , //USA xml server
170 //for all parameters see if the user has overruled it or use the default 169 ),
171 foreach ($Adefault as $i=>$d) { 170 );
172 $$i=isset($_GET[$i])?$_GET[$i]:$d; 171
172 //for all parameters see if the user has overruled it or use the default
173 foreach ($Adefault as $i=>$d) {
174 $$i=isset($_GET[$i])?$_GET[$i]:$d;
175 }
176
177 $parameters=array(
178 'Operation' =>$operation ,
179 'Keywords' =>urlencode($search) ,
180 "$searchparameter"=>$searchparameterdata ,
181 'ItemPage' =>$page , //which page?
182 'AssociateTag' =>$Aassociates_id[$locale],
183 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes'
184 );
185
186 if ($searchindex!='Books') {
187 $parameters['SearchIndex']=$searchindex;
188 }
189
190 try {
191 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,3);
192 //use this xml to pull out the necessary information and save it
193 set_error_handler(function () {
194 global $out,$isbn;
195 file_put_contents('/var/ywww/debug/phpDebug',
196 "Caught one bd: ".$isbn,
197 FILE_APPEND);
198 file_put_contents('/var/ywww/debug/phpDebug',
199 print_r($out, TRUE)."\n",
200 FILE_APPEND);
201 } );
202 include "../../../private/db.php";
203
204 $title="";
205 $author="";
206 $binding="";
207 $dewey="";
208 $imageURL="";
209 $salesRank="";
210 $pubDate="";
211 $publisher="";
212
213 $title = $xml->Items->Item->ItemAttributes->Title;
214 $author = $xml->Items->Item->ItemAttributes->Author;
215 $binding = $xml->Items->Item->ItemAttributes->Binding;
216 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
217 if($dewey == "")
218 $dewey = "null";
219 $imageURL = $xml->Items->Item->MediumImage->URL;
220 $salesRank = $xml->Items->Item->SalesRank;
221 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
222 $publisher = $xml->Items->Item->ItemAttributes->Publisher;
223 restore_error_handler();
224 if ($publisher and strlen($publisher)>30) {
225 $publisher=substr($publisher,0,30);
226 }
227 if ($author and strlen($author)>30) {
228 $author=substr($author,0,30);
229 }
230 if ($title and strlen($title)>100) {
231 $title=substr($title,0,100);
232 }
233 $publisher=mysqli_real_escape_string($link,$publisher);
234 $author=mysqli_real_escape_string($link,$author);
235 $title=mysqli_real_escape_string($link,$title);
236 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
237 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
238 if (strlen($pubDate)==0) {
239 $pubDate="null";
240 }
241 else {
242 $pubDate="\"$pubDate\"";
243 }
244 /*echo $title;
245 echo $author;
246 echo $binding;
247 echo $dewey;
248 echo $imageURL;
249 echo $salesRank;
250 echo $pubDate;
251 echo $publisher;*/
252
253 $genreID = "";
254 $genre = "";
255 $genArr = array();
256 $g1 = "null";
257 $g2 = "null";
258 $g3 = "null";
259 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){
260 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){
261 //sexy recursive function
262 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
263
264 if($genre != "")
265 $genArr[strval($genreID)] = strval($genre);
266 //$genArr[$i] = array(strval($genreID) => strval($genre));
267
268 //echo $genre;
269 //echo $genreID;
270
271 $genre = "";
272 $genreID = "";
173 } 273 }
174 274
175 $parameters=array( 275 $g1 = "null";
176 'Operation' =>$operation , 276 $g2 = "null";
177 'Keywords' =>urlencode($search) , 277 $g3 = "null";
178 "$searchparameter"=>$searchparameterdata , 278 $loop = 1;
179 'ItemPage' =>$page , //which page? 279
180 'AssociateTag' =>$Aassociates_id[$locale], 280 foreach ($genArr as $key => $value) {
181 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes' 281 //echo "$key => $value";
182 ); 282 if ($key>2047) {
183 283 //HST added
184 if ($searchindex!='Books') { 284 break;
185 // HST did this
186 // Used to be in $parameters init above, but
187 // that caused an error:
188 // If idType equals ASIN, SearchIndex cannot be present
189 //'SearchIndex' =>$searchindex , //Books for example.
190 $parameters['SearchIndex']=$searchindex;
191 } 285 }
192 286 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
193 287 //echo $queryG;
194 $ext=$Aserver[$locale]['ext']; 288 $resG = mysqli_query($link, $queryG);
195 $file_data=$ext; 289
196 ksort($parameters); 290 switch ($loop) {
197 foreach ($parameters as $i=>$d) { 291 case 1:
198 $file_data.='&'.$i.'='.$d; 292 $g1 = $key;
293 break;
294 case 2:
295 $g2 = $key;
296 break;
297 case 3:
298 $g3 = $key;
299 break;
300 }
301
302 $loop++;
199 } 303 }
200 304 }
201 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);
202 //echo $url;
203
204 $crl = curl_init();
205 $timeout = 5;
206 curl_setopt ($crl, CURLOPT_URL,$url);
207 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
208 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
209 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
210 $gotit=0;
211 $requestIP = $_SERVER['REMOTE_ADDR'];
212 $semaphore = new SyncSemaphore("Amazon");
213 $gotit = $semaphore->lock(1000);
214 if (!$gotit) {
215 file_put_contents('/var/ywww/debug/phpDebug',
216 "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND);
217 $gotit=$semaphore->lock(1000);
218 file_put_contents('/var/ywww/debug/phpDebug',
219 "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND);
220 }
221 $out = curl_exec($crl);
222 curl_close($crl);
223 usleep(500000);
224 if ($gotit) {
225 $semaphore->unlock();
226 }
227 else {
228 file_put_contents('/var/ywww/debug/phpDebug',
229 "W/o lock for $requestIP 2b\n",FILE_APPEND);
230 }
231 //echo $out;
232 $mm=array();
233 if (preg_match("/<Error>/",$out,$mm)) {
234 $xml = new SimpleXMLElement($out);
235 $resName=$xml->getName();
236 $code=$xml->Error->Code;
237 if ($code) {
238 $message=$xml->Error->Message;
239 }
240 else {
241 $code=$xml->Items->Request->Errors->Error->Code;
242 $message=$xml->Items->Request->Errors->Error->Message;
243 }
244 $requestIP = $_SERVER['REMOTE_ADDR'];
245 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: ".$mm[0].", $resName, $code, $requestIP\n",FILE_APPEND);
246 if ($code=='RequestThrottled') {
247 usleep(200000); // Try to reduce throttling until we get a
248 // principled solution in place
249 }
250 else {
251 file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND);
252 if ($code=="") {
253 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$out\n",FILE_APPEND);
254 }
255 }
256 }
257 else {
258 $xml = new SimpleXMLElement($out);
259 //use this xml to pull out the necessary information and save it
260 set_error_handler(function () {
261 global $out,$isbn;
262 file_put_contents('/var/ywww/debug/phpDebug',
263 "Caught one bd: ".$isbn,
264 FILE_APPEND);
265 file_put_contents('/var/ywww/debug/phpDebug',
266 print_r($out, TRUE)."\n",
267 FILE_APPEND);
268 } );
269
270 $title="";
271 $author="";
272 $binding="";
273 $dewey="";
274 $imageURL="";
275 $salesRank="";
276 $pubDate="";
277 $publisher="";
278
279 $title = $xml->Items->Item->ItemAttributes->Title;
280 $author = $xml->Items->Item->ItemAttributes->Author;
281 $binding = $xml->Items->Item->ItemAttributes->Binding;
282 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
283 if($dewey == "")
284 $dewey = "null";
285 $imageURL = $xml->Items->Item->MediumImage->URL;
286 $salesRank = $xml->Items->Item->SalesRank;
287 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
288 $publisher = $xml->Items->Item->ItemAttributes->Publisher;
289 restore_error_handler();
290 if ($publisher and strlen($publisher)>30) {
291 $publisher=substr($publisher,0,30);
292 }
293 if ($author and strlen($author)>30) {
294 $author=substr($author,0,30);
295 }
296 if ($title and strlen($title)>100) {
297 $title=substr($title,0,100);
298 }
299 $publisher=mysqli_real_escape_string($link,$publisher);
300 $author=mysqli_real_escape_string($link,$author);
301 $title=mysqli_real_escape_string($link,$title);
302 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
303 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
304 if (strlen($pubDate)==0) {
305 $pubDate="null";
306 }
307 else {
308 $pubDate="\"$pubDate\"";
309 }
310 /*echo $title;
311 echo $author;
312 echo $binding;
313 echo $dewey;
314 echo $imageURL;
315 echo $salesRank;
316 echo $pubDate;
317 echo $publisher;*/
318
319 $genreID = "";
320 $genre = "";
321 $genArr = array();
322 $g1 = "null";
323 $g2 = "null";
324 $g3 = "null";
325 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){
326 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){
327 //sexy recursive function
328 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
329
330 if($genre != "")
331 $genArr[strval($genreID)] = strval($genre);
332 //$genArr[$i] = array(strval($genreID) => strval($genre));
333
334 //echo $genre;
335 //echo $genreID;
336
337 $genre = "";
338 $genreID = "";
339 }
340
341 $g1 = "null";
342 $g2 = "null";
343 $g3 = "null";
344 $loop = 1;
345
346 foreach ($genArr as $key => $value) {
347 //echo "$key => $value";
348 if ($key>2047) {
349 //HST added
350 break;
351 }
352 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
353 //echo $queryG;
354 $resG = mysqli_query($link, $queryG);
355
356 switch ($loop) {
357 case 1:
358 $g1 = $key;
359 break;
360 case 2:
361 $g2 = $key;
362 break;
363 case 3:
364 $g3 = $key;
365 break;
366 }
367
368 $loop++;
369 }
370 }
371 305
372 if($salesRank == "") 306 if($salesRank == "")
373 $salesRank = "null"; 307 $salesRank = "null";
374 308
375 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop 309 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
376 include "../../../private/db.php"; 310 include "../../../private/db.php";
377 311
378 $title = strtr($title, '"', "'"); 312 $title = strtr($title, '"', "'");
379 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)"; 313 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)";
380 //echo $queryInsert; 314 //echo $queryInsert;
381 315
382 $res = mysqli_query($link, $queryInsert); 316 $res = mysqli_query($link, $queryInsert);
383 if (!$res) { 317 if (!$res) {
384 $err=mysqli_error( $link ); 318 $err=mysqli_error( $link );
385 mysqli_close($link); 319 mysqli_close($link);
386 file_put_contents('/var/ywww/debug/phpDebug', 320 file_put_contents('/var/ywww/debug/phpDebug',
387 "anb failed: $err, $pubDate, $g2, $publisher, $title\n", 321 "anb failed: $err, $pubDate, $g2, $publisher, $title\n",
388 FILE_APPEND); 322 FILE_APPEND);
389 exit($err); 323 exit($err);
390 } 324 }
391 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop 325 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
392 326
393 327
394 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; 328 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
395 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; 329 $output .= "<Title>" . htmlspecialchars($title) . "</Title>";
396 $output .= "<Author>" . htmlspecialchars($author) . "</Author>"; 330 $output .= "<Author>" . htmlspecialchars($author) . "</Author>";
397 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>"; 331 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>";
398 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>"; 332 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
399 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>"; 333 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
400 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>"; 334 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
401 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>"; 335 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
402 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>"; 336 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
403 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>"; 337 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
404 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>"; 338 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
405 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>"; 339 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";
406 $output .= "<ProductGroup>Book</ProductGroup>"; 340 $output .= "<ProductGroup>Book</ProductGroup>";
407 } 341 }
408 } 342 catch (Exception $e) {
409 343 file_put_contents('/var/ywww/debug/phpDebug',
344 "gBD: dAR failed:".$e->getMessage()."\n",
345 FILE_APPEND);
346 }
347 }
410 $output .= "</BookDetails>"; 348 $output .= "</BookDetails>";
411 349
412 echo $output; 350 echo $output;
413 } 351 }
414 ?> 352 ?>