comparison php/read/getBookDetails.php @ 42:3f400072bf14

merge, involved ediff3 wrt getAmazonInfo.php
author Charlie Root
date Sat, 05 Jan 2019 06:21:12 -0500
parents c24ae74bf6d5
children dbc006408d2b
comparison
equal deleted inserted replaced
41:0578c6e438d3 42:3f400072bf14
67 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); 67 $res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
68 68
69 if ( mysqli_num_rows( $res ) > 0 ) 69 if ( mysqli_num_rows( $res ) > 0 )
70 { 70 {
71 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); 71 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
72 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
72 $timestamp=$rows['Timestamp']; 73 $timestamp=$rows['Timestamp'];
73 //echo $timestamp; 74 //echo $timestamp;
74 date_default_timezone_set('UTC'); 75 date_default_timezone_set('UTC');
75 $dbStamp = strtotime($timestamp); 76 $dbStamp = strtotime($timestamp);
76 //echo $dbStamp; 77 //echo $dbStamp;
95 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; 96 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
96 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; 97 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
97 $output .= "<ProductGroup>Book</ProductGroup>"; 98 $output .= "<ProductGroup>Book</ProductGroup>";
98 } 99 }
99 } 100 }
100 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop 101 else {
101 include "../../../private/db.php"; 102 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
103 }
102 104
103 if($proceed == true) 105 if($proceed == true)
104 { 106 {
105 include "aws_signed_request.php"; 107 include "aws_signed_request.php";
106 //book does not exist already so look up all the info from browse nodes.... 108 include_once "../../xml/doAmazonRequest.inc";
107 109 file_put_contents('/var/ywww/debug/phpDebug',
108 $Adefault=array( 110 "nbd proceeding: $isbn\n",
109 'language' =>'en', //what language to render the page in 111 FILE_APPEND);
110 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us 112
111 '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....
112 'operation' =>'ItemLookup', //what to do? //ItemSearch 114
113 'searchparameter' =>'ItemId', //what kind of search? 115 $Adefault=array(
114 'searchindex' => 'Books', 116 'language' =>'en', //what language to render the page in
115 'searchparameterdata'=>$isbn, //what to search for? 117 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
116 'search' =>$isbn, 118 'page' =>1, //first page to show (we are counting from 1 not 0)
117 //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
118 '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?
119 'show_url' =>false, //debug: show XML request url to be send to Amazon? 121 'searchindex' => 'Books',
120 'show_xml' =>false, //debug: show incoming XML code from Amazon? 122 'searchparameterdata'=>$isbn, //what to search for?
121 ); 123 'search' =>$isbn,
122 //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'
123 125 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending
124 $Aassociates_id=array( 126 'show_url' =>false, //debug: show XML request url to be send to Amazon?
125 'uk' => 'bookwhack-21', 127 'show_xml' =>false, //debug: show incoming XML code from Amazon?
126 'us' => 'your02b-20', 128 );
127 '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
128 'de' => 'book04c-21', 130
129 'fr' => 'book07f-21', 131 $Aassociates_id=array(
130 ); 132 'uk' => 'bookwhack-21',
131 133 'us' => 'your02b-20',
132 $Aserver=array( 134 'ca' => 'book009-20',
133 'ca' => array( 135 'de' => 'book04c-21',
134 'ext' => 'ca' , //Canadian normal server 136 'fr' => 'book07f-21',
135 'nor' => 'http://www.amazon.ca' , //Canadian normal server 137 );
136 'xml' => 'http://xml.amazon.com' , //Canadian xml server 138
137 ), 139 $Aserver=array(
138 'de' => array( 140 'ca' => array(
139 'ext' => 'de' , //German normal server 141 'ext' => 'ca' , //Canadian normal server
140 'nor' => 'http://www.amazon.de' , //German normal server 142 'nor' => 'http://www.amazon.ca' , //Canadian normal server
141 'xml' => 'http://xml-eu.amazon.com', //German xml server 143 'xml' => 'http://xml.amazon.com' , //Canadian xml server
142 ), 144 ),
143 'fr' => array( 145 'de' => array(
144 'ext' => 'fr' , //French normal server 146 'ext' => 'de' , //German normal server
145 'nor' => 'http://www.amazon.fr' , //French normal server 147 'nor' => 'http://www.amazon.de' , //German normal server
146 'xml' => 'http://xml-eu.amazon.com', //French xml server 148 'xml' => 'http://xml-eu.amazon.com', //German xml server
147 ), 149 ),
148 'jp' => array( 150 'fr' => array(
149 'ext' => 'jp' , //Japanese normal server, not co.jp! 151 'ext' => 'fr' , //French normal server
150 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server 152 'nor' => 'http://www.amazon.fr' , //French normal server
151 'xml' => 'http://xml.amazon.com' , //Japanese xml server 153 'xml' => 'http://xml-eu.amazon.com', //French xml server
152 ), 154 ),
153 'uk' => array( 155 'jp' => array(
154 'ext' => 'co.uk' , //UK normal server 156 'ext' => 'jp' , //Japanese normal server, not co.jp!
155 'nor' => 'http://www.amazon.co.uk' , //UK normal server 157 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server
156 'xml' => 'http://xml-eu.amazon.com', //UK xml server 158 'xml' => 'http://xml.amazon.com' , //Japanese xml server
157 ), 159 ),
158 'us' => array( 160 'uk' => array(
159 'ext' => 'com' , //USA normal server 161 'ext' => 'co.uk' , //UK normal server
160 'nor' => 'http://www.amazon.com' , //USA normal server 162 'nor' => 'http://www.amazon.co.uk' , //UK normal server
161 'xml' => 'http://xml.amazon.com' , //USA xml server 163 'xml' => 'http://xml-eu.amazon.com', //UK xml server
162 ), 164 ),
163 ); 165 'us' => array(
164 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; 166 'ext' => 'com' , //USA normal server
165 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; 167 'nor' => 'http://www.amazon.com' , //USA normal server
166 168 'xml' => 'http://xml.amazon.com' , //USA xml server
167 //for all parameters see if the user has overruled it or use the default 169 ),
168 foreach ($Adefault as $i=>$d) { 170 );
169 $$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,6,'d');
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 = "";
170 } 273 }
171 274
172 $parameters=array( 275 $g1 = "null";
173 'Operation' =>$operation , 276 $g2 = "null";
174 'Keywords' =>urlencode($search) , 277 $g3 = "null";
175 "$searchparameter"=>$searchparameterdata , 278 $loop = 1;
176 'ItemPage' =>$page , //which page? 279
177 'AssociateTag' =>$Aassociates_id[$locale], 280 foreach ($genArr as $key => $value) {
178 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes' 281 //echo "$key => $value";
179 ); 282 if ($key>2047) {
180 283 //HST added
181 if ($searchindex!='Books') { 284 break;
182 // HST did this
183 // Used to be in $parameters init above, but
184 // that caused an error:
185 // If idType equals ASIN, SearchIndex cannot be present
186 //'SearchIndex' =>$searchindex , //Books for example.
187 $parameters['SearchIndex']=$searchindex;
188 } 285 }
189 286 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
190 287 //echo $queryG;
191 $ext=$Aserver[$locale]['ext']; 288 $resG = mysqli_query($link, $queryG);
192 $file_data=$ext; 289
193 ksort($parameters); 290 switch ($loop) {
194 foreach ($parameters as $i=>$d) { 291 case 1:
195 $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++;
196 } 303 }
197 304 }
198 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); 305
199 //echo $url; 306 if($salesRank == "")
200 307 $salesRank = "null";
201 $crl = curl_init(); 308
202 $timeout = 5; 309 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
203 curl_setopt ($crl, CURLOPT_URL,$url); 310 include "../../../private/db.php";
204 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 311
205 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 312 $title = strtr($title, '"', "'");
206 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 313 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)";
207 $out = curl_exec($crl); 314 //echo $queryInsert;
208 curl_close($crl); 315
209 316 $res = mysqli_query($link, $queryInsert);
210 //echo $out; 317 if (!$res) {
211 318 $err=mysqli_error( $link );
212 if (preg_match("<Error>",$out)) { 319 mysqli_close($link);
213 $xml = new SimpleXMLElement($out); 320 file_put_contents('/var/ywww/debug/phpDebug',
214 $resName=$xml->getName(); 321 "anb failed: $err, $pubDate, $g2, $publisher, $title\n",
215 $code=$xml->Error->Code; 322 FILE_APPEND);
216 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: $resName, $code\n",FILE_APPEND); 323 exit($err);
217 if ($code=='RequestThrottled') { 324 }
218 usleep(200000); // Try to reduce throttling until we get a 325 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
219 // principled solution in place 326
220 } 327
221 else { 328 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
222 file_put_contents('/var/ywww/debug/phpDebug',"parms bd: ". 329 $output .= "<Title>" . htmlspecialchars($title) . "</Title>";
223 print_r($parameters,TRUE)."\n",FILE_APPEND); 330 $output .= "<Author>" . htmlspecialchars($author) . "</Author>";
224 if ($code=="") { 331 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>";
225 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$out\n",FILE_APPEND); 332 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
226 } 333 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
227 } 334 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
228 } 335 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
229 else { 336 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
230 $xml = new SimpleXMLElement($out); 337 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
231 //use this xml to pull out the necessary information and save it 338 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
232 set_error_handler(function () { 339 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";
233 global $out,$isbn; 340 $output .= "<ProductGroup>Book</ProductGroup>";
234 file_put_contents('/var/ywww/debug/phpDebug', 341 }
235 "Caught one bd: ".$isbn, 342 catch (Exception $e) {
236 FILE_APPEND); 343 file_put_contents('/var/ywww/debug/phpDebug',
237 file_put_contents('/var/ywww/debug/phpDebug', 344 "gBD: dAR failed:".$e->getMessage()."\n",
238 print_r($out, TRUE)."\n", 345 FILE_APPEND);
239 FILE_APPEND); 346 }
240 } ); 347 }
241
242 $title="";
243 $author="";
244 $binding="";
245 $dewey="";
246 $imageURL="";
247 $salesRank="";
248 $pubDate="";
249 $publisher="";
250
251 $title = $xml->Items->Item->ItemAttributes->Title;
252 $author = $xml->Items->Item->ItemAttributes->Author;
253 $binding = $xml->Items->Item->ItemAttributes->Binding;
254 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
255 if($dewey == "")
256 $dewey = "null";
257 $imageURL = $xml->Items->Item->MediumImage->URL;
258 $salesRank = $xml->Items->Item->SalesRank;
259 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
260 $publisher = $xml->Items->Item->ItemAttributes->Publisher;
261 restore_error_handler();
262 /*echo $title;
263 echo $author;
264 echo $binding;
265 echo $dewey;
266 echo $imageURL;
267 echo $salesRank;
268 echo $pubDate;
269 echo $publisher;*/
270
271 $genreID = "";
272 $genre = "";
273 $genArr = array();
274 $g1 = "null";
275 $g2 = "null";
276 $g3 = "null";
277 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){
278 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){
279 //sexy recursive function
280 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
281
282 if($genre != "")
283 $genArr[strval($genreID)] = strval($genre);
284 //$genArr[$i] = array(strval($genreID) => strval($genre));
285
286 //echo $genre;
287 //echo $genreID;
288
289 $genre = "";
290 $genreID = "";
291 }
292
293 $g1 = "null";
294 $g2 = "null";
295 $g3 = "null";
296 $loop = 1;
297
298 foreach ($genArr as $key => $value) {
299 //echo "$key => $value";
300 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
301 //echo $queryG;
302 $resG = mysqli_query($link, $queryG);
303
304 switch ($loop) {
305 case 1:
306 $g1 = $key;
307 break;
308 case 2:
309 $g2 = $key;
310 break;
311 case 3:
312 $g3 = $key;
313 break;
314 }
315
316 $loop++;
317 }
318 }
319
320 if($salesRank == "")
321 $salesRank = "null";
322
323 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
324 include "../../../private/db.php";
325
326 $title = strtr($title, '"', "'");
327 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)";
328 //echo $queryInsert;
329
330 $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link ));
331 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
332
333
334 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
335 $output .= "<Title>" . htmlspecialchars($title) . "</Title>";
336 $output .= "<Author>" . htmlspecialchars($author) . "</Author>";
337 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>";
338 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
339 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
340 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
341 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
342 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
343 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
344 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
345 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";
346 $output .= "<ProductGroup>Book</ProductGroup>";
347 }
348 }
349
350 $output .= "</BookDetails>"; 348 $output .= "</BookDetails>";
351 349
352 echo $output; 350 echo $output;
353 } 351 }
354 ?> 352 ?>