Mercurial > hg > ywww
annotate php/read/getBookDetails.php @ 39:2c0c95bd97a6
pass char for log lines in to doAmazonRequest,
move static arrays to aws_s_r
| author | Charlie Root |
|---|---|
| date | Fri, 04 Jan 2019 18:03:24 -0500 |
| parents | c24ae74bf6d5 |
| children | dbc006408d2b |
| rev | line source |
|---|---|
| 2 | 1 <?php |
| 2 | |
| 3 function findGenre($browseNode, &$ID, &$gen) | |
| 4 { | |
| 5 if($browseNode->Name == "Subjects") | |
| 6 { | |
| 7 return true; | |
| 8 } | |
| 9 else | |
| 10 { | |
| 11 if($browseNode->Ancestors->BrowseNode) | |
| 12 { | |
| 13 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) | |
| 14 { | |
| 15 $gen = $browseNode->Name; | |
| 16 $ID = $browseNode->BrowseNodeId; | |
| 17 | |
| 18 //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well | |
| 19 // return true; | |
| 20 } | |
| 21 } | |
| 22 return false; | |
| 23 } | |
| 24 } | |
| 25 | |
| 3 | 26 global $out,$isbn; |
| 2 | 27 if(isset($_GET['isbn'])){ |
| 28 $isbn = $_GET['isbn']; | |
| 29 if(isset($_GET['locale'])){ | |
| 30 $locale=$_GET['locale']; | |
| 31 | |
| 32 switch ($locale) { | |
| 33 case "uk": | |
| 34 $loc = 1; | |
| 35 break; | |
| 36 case "us": | |
| 37 $loc = 0; | |
| 38 break; | |
| 39 case "ca": | |
| 40 $loc = 2; | |
| 41 break; | |
| 42 case "de": | |
| 43 $loc = 3; | |
| 44 break; | |
| 45 case "fr": | |
| 46 $loc = 4; | |
| 47 break; | |
| 48 case "": | |
| 49 $loc = 0; | |
| 50 break; | |
| 51 } | |
| 52 } | |
| 53 else{ | |
| 54 $loc=0; | |
| 55 } | |
| 56 $output = ""; | |
| 57 | |
| 58 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
| 59 $output .= "<BookDetails>"; | |
| 60 | |
| 61 $proceed = true; | |
| 62 include "../../../private/db.php"; | |
| 63 | |
| 64 $query = "CALL b_getBookInfo('$isbn', $loc)"; | |
| 65 //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'"; | |
| 66 //echo $query; | |
| 67 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
| 68 | |
| 69 if ( mysqli_num_rows( $res ) > 0 ) | |
| 70 { | |
| 71 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); | |
|
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
72 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
| 2 | 73 $timestamp=$rows['Timestamp']; |
| 74 //echo $timestamp; | |
| 75 date_default_timezone_set('UTC'); | |
| 76 $dbStamp = strtotime($timestamp); | |
| 77 //echo $dbStamp; | |
| 78 $unixTime = time(); | |
| 79 //echo $unixTime; | |
| 80 $timeDiff = $unixTime - $dbStamp; | |
| 81 //echo $timeDiff; | |
| 82 | |
| 83 if($timeDiff < 2592000) //if it has been updated in the last 30 days | |
| 84 { | |
| 85 $proceed = false; | |
| 86 $output .= "<ISBN>" . $isbn . "</ISBN>"; | |
| 87 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
| 88 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
| 89 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
| 90 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
| 91 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
| 92 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
| 93 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
| 94 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
| 95 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
| 96 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
| 97 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
| 98 $output .= "<ProductGroup>Book</ProductGroup>"; | |
| 99 } | |
| 100 } | |
|
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
101 else { |
|
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
102 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
|
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
103 } |
| 2 | 104 |
| 105 if($proceed == true) | |
| 36 | 106 { |
| 107 include "aws_signed_request.php"; | |
| 108 include_once "../../xml/doAmazonRequest.inc"; | |
| 109 file_put_contents('/var/ywww/debug/phpDebug', | |
| 110 "nbd proceeding: $isbn\n", | |
| 111 FILE_APPEND); | |
| 112 | |
| 113 //book does not exist already so look up all the info from browse nodes.... | |
| 2 | 114 |
| 36 | 115 $Adefault=array( |
| 116 'language' =>'en', //what language to render the page in | |
| 117 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us | |
| 118 'page' =>1, //first page to show (we are counting from 1 not 0) | |
| 119 'operation' =>'ItemLookup', //what to do? //ItemSearch | |
| 120 'searchparameter' =>'ItemId', //what kind of search? | |
| 121 'searchindex' => 'Books', | |
| 122 'searchparameterdata'=>$isbn, //what to search for? | |
| 123 'search' =>$isbn, | |
| 124 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' | |
| 125 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending | |
| 126 'show_url' =>false, //debug: show XML request url to be send to Amazon? | |
| 127 'show_xml' =>false, //debug: show incoming XML code from Amazon? | |
| 128 ); | |
| 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 | |
| 2 | 130 |
| 36 | 131 $Aassociates_id=array( |
| 132 'uk' => 'bookwhack-21', | |
| 133 'us' => 'your02b-20', | |
| 134 'ca' => 'book009-20', | |
| 135 'de' => 'book04c-21', | |
| 136 'fr' => 'book07f-21', | |
| 137 ); | |
| 2 | 138 |
| 36 | 139 $Aserver=array( |
| 140 'ca' => array( | |
| 141 'ext' => 'ca' , //Canadian normal server | |
| 142 'nor' => 'http://www.amazon.ca' , //Canadian normal server | |
| 143 'xml' => 'http://xml.amazon.com' , //Canadian xml server | |
| 144 ), | |
| 145 'de' => array( | |
| 146 'ext' => 'de' , //German normal server | |
| 147 'nor' => 'http://www.amazon.de' , //German normal server | |
| 148 'xml' => 'http://xml-eu.amazon.com', //German xml server | |
| 149 ), | |
| 150 'fr' => array( | |
| 151 'ext' => 'fr' , //French normal server | |
| 152 'nor' => 'http://www.amazon.fr' , //French normal server | |
| 153 'xml' => 'http://xml-eu.amazon.com', //French xml server | |
| 154 ), | |
| 155 'jp' => array( | |
| 156 'ext' => 'jp' , //Japanese normal server, not co.jp! | |
| 157 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server | |
| 158 'xml' => 'http://xml.amazon.com' , //Japanese xml server | |
| 159 ), | |
| 160 'uk' => array( | |
| 161 'ext' => 'co.uk' , //UK normal server | |
| 162 'nor' => 'http://www.amazon.co.uk' , //UK normal server | |
| 163 'xml' => 'http://xml-eu.amazon.com', //UK xml server | |
| 164 ), | |
| 165 'us' => array( | |
| 166 'ext' => 'com' , //USA normal server | |
| 167 'nor' => 'http://www.amazon.com' , //USA normal server | |
| 168 'xml' => 'http://xml.amazon.com' , //USA xml server | |
| 169 ), | |
| 170 ); | |
| 2 | 171 |
| 36 | 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 } | |
| 2 | 189 |
| 36 | 190 try { |
| 38 | 191 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,6,'d'); |
| 36 | 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)); | |
| 2 | 267 |
| 36 | 268 //echo $genre; |
| 269 //echo $genreID; | |
| 270 | |
| 271 $genre = ""; | |
| 272 $genreID = ""; | |
|
21
46382face560
use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents:
18
diff
changeset
|
273 } |
| 36 | 274 |
| 275 $g1 = "null"; | |
| 276 $g2 = "null"; | |
| 277 $g3 = "null"; | |
| 278 $loop = 1; | |
| 279 | |
| 280 foreach ($genArr as $key => $value) { | |
| 281 //echo "$key => $value"; | |
| 282 if ($key>2047) { | |
| 283 //HST added | |
| 284 break; | |
|
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
285 } |
| 36 | 286 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table |
| 287 //echo $queryG; | |
| 288 $resG = mysqli_query($link, $queryG); | |
| 289 | |
| 290 switch ($loop) { | |
| 291 case 1: | |
| 292 $g1 = $key; | |
| 293 break; | |
| 294 case 2: | |
| 295 $g2 = $key; | |
| 296 break; | |
| 297 case 3: | |
| 298 $g3 = $key; | |
| 299 break; | |
|
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
300 } |
| 36 | 301 |
| 302 $loop++; | |
|
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
303 } |
| 36 | 304 } |
| 305 | |
| 306 if($salesRank == "") | |
| 307 $salesRank = "null"; | |
| 2 | 308 |
| 36 | 309 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
| 310 include "../../../private/db.php"; | |
| 2 | 311 |
| 36 | 312 $title = strtr($title, '"', "'"); |
| 313 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)"; | |
| 314 //echo $queryInsert; | |
| 2 | 315 |
| 36 | 316 $res = mysqli_query($link, $queryInsert); |
| 317 if (!$res) { | |
| 318 $err=mysqli_error( $link ); | |
| 319 mysqli_close($link); | |
| 320 file_put_contents('/var/ywww/debug/phpDebug', | |
| 321 "anb failed: $err, $pubDate, $g2, $publisher, $title\n", | |
| 322 FILE_APPEND); | |
| 323 exit($err); | |
| 324 } | |
| 325 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
| 2 | 326 |
| 327 | |
| 36 | 328 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; |
| 329 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; | |
| 330 $output .= "<Author>" . htmlspecialchars($author) . "</Author>"; | |
| 331 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>"; | |
| 332 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>"; | |
| 333 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>"; | |
| 334 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>"; | |
| 335 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>"; | |
| 336 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>"; | |
| 337 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>"; | |
| 338 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>"; | |
| 339 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>"; | |
| 340 $output .= "<ProductGroup>Book</ProductGroup>"; | |
| 341 } | |
| 342 catch (Exception $e) { | |
| 343 file_put_contents('/var/ywww/debug/phpDebug', | |
| 344 "gBD: dAR failed:".$e->getMessage()."\n", | |
| 345 FILE_APPEND); | |
| 346 } | |
| 347 } | |
| 2 | 348 $output .= "</BookDetails>"; |
| 349 | |
| 350 echo $output; | |
| 351 } | |
| 352 ?> |
