Mercurial > hg > ywww
view php/read/getBookDetails.php @ 58:39600878ff93
prefer empty result to no result at all
author | Charlie Root |
---|---|
date | Sun, 09 Jun 2019 06:48:35 -0400 |
parents | dd93cb4b77ad |
children |
line wrap: on
line source
<?php include_once "dlog.php"; include_once "web.php"; function findGenre($browseNode, &$ID, &$gen) { if($browseNode->Name == "Subjects") { return true; } else { if($browseNode->Ancestors->BrowseNode) { if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) { $gen = $browseNode->Name; $ID = $browseNode->BrowseNodeId; //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well // return true; } } return false; } } global $out,$isbn,$Aserver,$Aassociates_id; if(isset($_GET['isbn'])){ $isbn = $_GET['isbn']; if(isset($_GET['locale'])){ $locale=$_GET['locale']; switch ($locale) { case "uk": $loc = 1; break; case "us": $loc = 0; break; case "ca": $loc = 2; break; case "de": $loc = 3; break; case "fr": $loc = 4; break; case "": $loc = 0; break; } } else{ $loc=0; } $output = ""; $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; $output .= "<BookDetails>"; $proceed = true; include "db.php"; $query = "CALL b_getBookInfo('$isbn', $loc)"; //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'"; //echo $query; $res = my_query($query,'gbd1'); if ( mysqli_num_rows( $res ) > 0 ) { $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop $timestamp=$rows['Timestamp']; //echo $timestamp; date_default_timezone_set('UTC'); $dbStamp = strtotime($timestamp); //echo $dbStamp; $unixTime = time(); //echo $unixTime; $timeDiff = $unixTime - $dbStamp; //echo $timeDiff; if($timeDiff < 2592000) //if it has been updated in the last 30 days { $proceed = false; $output .= "<ISBN>" . $isbn . "</ISBN>"; $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; $output .= "<ProductGroup>Book</ProductGroup>"; } } else { mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop } dl("gbd: ".strlen($output)." $proceed\n$output\n"); if($proceed == true) { include "aws_signed_request.php"; include_once "xml/doAmazonRequest.php"; //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 //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; } try { $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,2,'d'); //use this xml to pull out the necessary information and save it set_error_handler(function () { global $out,$isbn; dl("Caught one bd: $isbn\n"); dl(print_r($out, TRUE)."\n"); } ); include "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)); //echo $genre; //echo $genreID; $genre = ""; $genreID = ""; } $g1 = "null"; $g2 = "null"; $g3 = "null"; $loop = 1; 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 = my_query($queryG,'gbd2'); 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 "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 = my_query($queryInsert,'gbd3'); 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>"; } catch (Exception $e) { dl("gBD: dAR failed:".$e->getMessage()."\n"); if (isset($rows)) { dl("gBD: returning stale info from DB: $timeDiff\n"); /* HST hack, copied from above */ $output .= "<ISBN>" . $isbn . "</ISBN>"; $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; $output .= "<ProductGroup>Book</ProductGroup>"; } else { dl("gBD: no DB info either, bailing\n"); # try to avoid looping behaviour from client :-( } } } $output .= "</BookDetails>"; echo $output; } ?>