Mercurial > hg > ywww
comparison xml/getAmazonInfo.php @ 42:3f400072bf14
merge, involved ediff3 wrt getAmazonInfo.php
author | Charlie Root |
---|---|
date | Sat, 05 Jan 2019 06:21:12 -0500 |
parents | 0578c6e438d3 2c0c95bd97a6 |
children | dbc006408d2b |
comparison
equal
deleted
inserted
replaced
41:0578c6e438d3 | 42:3f400072bf14 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 $lastReqTime=0; | 3 include_once "doAmazonRequest.inc"; |
4 | |
4 function getAmazonDet($isbn,$go,$localeIn) | 5 function getAmazonDet($isbn,$go,$localeIn) |
5 { | 6 { |
6 global $output; | 7 $Adefault=array( |
7 $Adefault=array( | 8 'language' =>'en', //what language to render the page in |
8 'language' =>'en', //what language to render the page in | 9 'locale' =>$localeIn, //which server's products? available: ca,de,fr,jp,uk,us |
9 'locale' =>$localeIn, //which server's products? available: ca,de,fr,jp,uk,us | 10 //'mode' =>'books', //what product category? |
10 //'mode' =>'books', //what product category? | 11 'page' =>1, //first page to show (we are counting from 1 not 0) |
11 'page' =>1, //first page to show (we are counting from 1 not 0) | 12 //'search' =>'Machiavelli', //what to search for? |
12 //'search' =>'Machiavelli', //what to search for? | 13 'operation' =>'ItemLookup', //what to do? //ItemSearch |
13 'operation' =>'ItemLookup', //what to do? //ItemSearch | 14 'searchindex' =>'Books', //what product category for search? |
14 // 'searchindex' =>'Books', //what product category for search? | 15 'searchparameter' =>'ItemId', //what kind of search? |
15 'searchparameter' =>'ItemId', //what kind of search? | 16 'searchparameterdata'=>$isbn, //what to search for? |
16 'searchparameterdata'=>$isbn, //what to search for? | 17 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' |
17 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' | 18 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending |
18 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending | 19 'show_url' =>false, //debug: show XML request url to be send to Amazon? |
19 'show_url' =>false, //debug: show XML request url to be send to Amazon? | 20 'show_xml' =>false, //debug: show incoming XML code from Amazon? |
20 'show_xml' =>false, //debug: show incoming XML code from Amazon? | 21 ); |
21 ); | 22 //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 |
22 //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 | 23 //for all parameters see if the user has overruled it or use the default |
23 | 24 foreach ($Adefault as $i=>$d) { |
24 $Aassociates_id=array( | 25 $$i=isset($_GET[$i])?$_GET[$i]:$d; |
25 'uk' => 'bookwhack-21', | 26 } |
26 'us' => 'your02b-20', | 27 switch($locale){ |
27 'ca' => 'book009-20', | 28 case "us": |
28 'de' => 'book04c-21', | 29 $loc = 0; |
29 'fr' => 'book07f-21', | 30 break; |
30 ); | 31 case "uk": |
31 | 32 $loc = 1; |
32 $Aserver=array( | 33 break; |
33 'ca' => array( | 34 case "ca": |
34 'ext' => 'ca' , //Canadian normal server | 35 $loc = 2; |
35 'nor' => 'http://www.amazon.ca' , //Canadian normal server | 36 break; |
36 'xml' => 'http://xml.amazon.com' , //Canadian xml server | 37 case "de": |
37 ), | 38 $loc = 3; |
38 'de' => array( | 39 break; |
39 'ext' => 'de' , //German normal server | 40 case "fr": |
40 'nor' => 'http://www.amazon.de' , //German normal server | 41 $loc = 4; |
41 'xml' => 'http://xml-eu.amazon.com', //German xml server | 42 break; |
42 ), | 43 }; |
43 'fr' => array( | 44 |
44 'ext' => 'fr' , //French normal server | 45 try { |
45 'nor' => 'http://www.amazon.fr' , //French normal server | 46 return getAmazonDetRemote($go,$language,$locale,$loc, |
46 'xml' => 'http://xml-eu.amazon.com', //French xml server | 47 $page,$operation,$searchparameter, |
47 ), | 48 $searchparameterdata, |
48 'jp' => array( | 49 $show_array,$show_url,$show_xml); |
49 'ext' => 'jp' , //Japanese normal server, not co.jp! | 50 } |
50 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server | 51 catch (Exception $e) { |
51 'xml' => 'http://xml.amazon.com' , //Japanese xml server | 52 return getAmazonDetLocal($go,$language,$loc, |
52 ), | 53 $page,$operation,$searchparameter, |
53 'uk' => array( | 54 $searchparameterdata, |
54 'ext' => 'co.uk' , //UK normal server | 55 $show_array,$show_url,$show_xml); |
55 'nor' => 'http://www.amazon.co.uk' , //UK normal server | 56 } |
56 'xml' => 'http://xml-eu.amazon.com', //UK xml server | |
57 ), | |
58 'us' => array( | |
59 'ext' => 'com' , //USA normal server | |
60 'nor' => 'http://www.amazon.com' , //USA normal server | |
61 'xml' => 'http://xml.amazon.com' , //USA xml server | |
62 ), | |
63 ); | |
64 | |
65 //if(go != 1) | |
66 //include "aws_signed_request.php"; | |
67 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; | |
68 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; | |
69 //for all parameters see if the user has overruled it or use the default | |
70 foreach ($Adefault as $i=>$d) { | |
71 $$i=isset($_GET[$i])?$_GET[$i]:$d; | |
72 } | 57 } |
73 //this is the data that is used to form the request for AWS | 58 |
74 //this is the part that is search specific | 59 function getAmazonDetRemote($go,$language,$locale,$loc, |
60 $page,$operation,$searchparameter, | |
61 $searchparameterdata, | |
62 $show_array,$show_url,$show_xml) | |
63 { | |
64 global $output, $Aassociates_id, $Aserver; | |
65 | |
66 //if(go != 1) | |
67 //include "aws_signed_request.php"; | |
68 //this is the data that is used to form the request for AWS | |
69 //this is the part that is search specific | |
75 $parameters=array( | 70 $parameters=array( |
76 'Operation' =>$operation , | 71 'Operation' =>$operation , |
77 //'Keywords' =>urlencode($search) , | 72 //'Keywords' =>urlencode($search) , |
78 //'SearchIndex' =>$searchindex , //Books for example. | 73 "$searchparameter"=>$searchparameterdata , |
79 "$searchparameter"=>$searchparameterdata , | 74 'ItemPage' =>$page , //which page? |
80 'ItemPage' =>$page , //which page? | 75 'AssociateTag' =>$Aassociates_id[$locale], |
81 'AssociateTag' =>$Aassociates_id[$locale], | 76 'ResponseGroup' =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes' , //Small, Medium, Large or SellerListing,'BrowseNodes',// , |
82 'ResponseGroup' =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes' , //Small, Medium, Large or SellerListing,'BrowseNodes',// , | 77 'ReviewSort' =>'-HelpfulVotes' |
83 'ReviewSort' =>'-HelpfulVotes' | 78 ); |
84 ); | 79 |
85 | 80 if (isset($searchindex) && $searchindex!='Books') { |
86 $ext=$Aserver[$locale]['ext']; | 81 // HST did this |
87 $file_data=$ext; | 82 // Used to be in $parameters init above, but |
88 ksort($parameters); | 83 // that caused an error: |
89 foreach ($parameters as $i=>$d) { | 84 // If idType equals ASIN, SearchIndex cannot be present |
90 $file_data.='&'.$i.'='.$d; | 85 //'SearchIndex' =>$searchindex , //Books for example. |
91 } | 86 $parameters['SearchIndex']=$searchindex; |
92 | 87 } |
93 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | 88 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,3,'i'); // may throw exception |
94 //echo $url; | 89 set_error_handler(function () { |
95 $crl = curl_init(); | 90 global $output; |
96 $timeout = 5; | 91 file_put_contents('/var/ywww/debug/phpDebug', |
97 curl_setopt ($crl, CURLOPT_URL,$url); | 92 "Caught one?: ".$searchparameterdata, |
98 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | 93 FILE_APPEND); |
99 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | 94 file_put_contents('/var/ywww/debug/phpDebug', |
100 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | 95 print_r($output, TRUE)."\n", |
101 /* $timeDiff=((int)(microtime(true)*1000))-$lastReqTime; | 96 FILE_APPEND); |
102 if ($timeDiff<1000) { | 97 } ); |
103 usleep((1000-$timeDiff)*1000); // microseconds | 98 $review = $xml->Items->Item->CustomerReviews->IFrameURL; |
104 }*/ | 99 restore_error_handler(); |
105 $output = curl_exec($crl); | 100 //echo $review; |
106 //$lastReqTime=(int)(microtime(true)*1000); // milliseconds | 101 if ($review != "") |
107 curl_close($crl); | 102 { |
108 | 103 $review1 = ""; |
109 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ | 104 $review2 = ""; |
110 $review = ""; | 105 $review3 = ""; |
111 // HST added this | 106 $text = @file_get_contents($review . "&truncate=300"); |
112 if (preg_match("<Error>",$output)) { | 107 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text); |
113 $xml = new SimpleXMLElement($output); | 108 $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text); |
114 $resName=$xml->getName(); | 109 if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } else { $getBody = $removeTop2;} |
115 $code=$xml->Error->Code; | 110 $removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody); |
116 file_put_contents('/var/ywww/debug/phpDebug',"Losing (test): $resName, $code\n",FILE_APPEND); | 111 $removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv); |
117 if ($code=='RequestThrottled') { | 112 $setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs); |
118 usleep(200000); // Try to reduce throttling until we get a | 113 //replace <!-- BOUNDARY --> with BOTTOM-TOP |
119 // principled solution in place | 114 $remove1 = '~<table cellpadding="0"(.*?)%">~s'; |
115 $setBoundary = preg_replace($remove1,'', $setBoundary); | |
116 $remove2 = '~</td><td bg(.*?)</table>~s'; | |
117 $setBoundary = preg_replace($remove2,'', $setBoundary); | |
118 $remove3 = '~<a name=(.*?)</a>~s'; | |
119 $setBoundary = preg_replace($remove3,'', $setBoundary); | |
120 $setBoundary2 = str_replace('<br />','', $setBoundary); | |
121 //remove all extra crap; | |
122 $setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2); | |
123 //replace </td> with BOTTOM | |
124 | |
125 if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews)) | |
126 { | |
127 $reviewContents = $reviews[1]; | |
128 //print_r($reviewContents); | |
129 $review1 = trim($reviewContents[0]); | |
130 $review1 = str_replace("\n", "", $review1); | |
131 $review1 = str_replace("\r", "", $review1); | |
132 if (isset($reviewContents[1])) { | |
133 $review2 = trim($reviewContents[1]); | |
134 $review2 = str_replace("\n", "", $review2); | |
135 $review2 = str_replace("\r", "", $review2); | |
120 } | 136 } |
121 else { | 137 else { |
122 file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n". | 138 $review2 = ""; |
123 print_r($parameters,TRUE)."\n",FILE_APPEND); | 139 } |
124 if ($code=="") { | 140 if (isset($reviewContents[2])) { |
125 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND); | 141 $review3 = trim($reviewContents[2]); |
126 } | 142 $review3 = str_replace("\n", "", $review3); |
127 } | 143 $review3 = str_replace("\r", "", $review3); |
128 } | 144 } |
129 else { | 145 else { |
130 $xml = new SimpleXMLElement($output); | 146 $review3 = ""; |
131 set_error_handler(function () { | 147 } |
132 global $output; | 148 } |
133 file_put_contents('/var/ywww/debug/phpDebug', | 149 else |
134 "Caught one?: ".$searchparameterdata, | 150 { |
135 FILE_APPEND); | |
136 file_put_contents('/var/ywww/debug/phpDebug', | |
137 print_r($output, TRUE)."\n", | |
138 FILE_APPEND); | |
139 } ); | |
140 $review = $xml->Items->Item->CustomerReviews->IFrameURL; | |
141 // The above is failing repeatedly -- | |
142 //PHP Notice: Trying to get property of non-object in | |
143 // /var/ywww/xml/getAmazonInfo.php on line [109] | |
144 // See the dumped structure at the end of this file for the | |
145 // cause | |
146 restore_error_handler(); | |
147 //echo $review; | |
148 $review1 = ""; | 151 $review1 = ""; |
149 $review2 = ""; | 152 $review2 = ""; |
150 $review3 = ""; | 153 $review3 = ""; |
151 } | 154 //echo "EPIC FAIL"; |
152 if ($review != "") | 155 } |
153 { | 156 |
154 $text = @file_get_contents($review . "&truncate=300"); | 157 unset($xml->Items->Item->CustomerReviews); |
155 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text); | 158 $xdoc = new DomDocument; |
156 $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text); | 159 $xdoc->loadXML($xml->asXML()); |
157 if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } | 160 |
158 $removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody); | 161 $cReviews = $xdoc ->createElement('CustomerReviews'); |
159 $removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv); | 162 $cReviewHolder = $xdoc ->createElement('Review'); |
160 $setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs); | 163 $cReview = $xdoc ->createElement('Content'); |
161 //replace <!-- BOUNDARY --> with BOTTOM-TOP | 164 $cReviewHolder2 = $xdoc ->createElement('Review'); |
162 $remove1 = '~<table cellpadding="0"(.*?)%">~s'; | 165 $cReview2 = $xdoc ->createElement('Content'); |
163 $setBoundary = preg_replace($remove1,'', $setBoundary); | 166 $cReviewHolder3 = $xdoc ->createElement('Review'); |
164 $remove2 = '~</td><td bg(.*?)</table>~s'; | 167 $cReview3 = $xdoc ->createElement('Content'); |
165 $setBoundary = preg_replace($remove2,'', $setBoundary); | 168 |
166 $remove3 = '~<a name=(.*?)</a>~s'; | 169 $txtNode = $xdoc ->createTextNode ($review1); |
167 $setBoundary = preg_replace($remove3,'', $setBoundary); | 170 $cReview -> appendChild($txtNode); |
168 $setBoundary2 = str_replace('<br />','', $setBoundary); | 171 |
169 //remove all extra crap; | 172 $txtNode2 = $xdoc ->createTextNode ($review2); |
170 $setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2); | 173 $cReview2 -> appendChild($txtNode2); |
171 //replace </td> with BOTTOM | 174 |
172 | 175 $txtNode3 = $xdoc ->createTextNode ($review3); |
173 if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews)) | 176 $cReview3 -> appendChild($txtNode3); |
174 { | 177 |
175 $reviewContents = $reviews[1]; | 178 $cReviewHolder -> appendChild($cReview); |
176 //print_r($reviewContents); | 179 $cReviewHolder2 -> appendChild($cReview2); |
177 $review1 = trim($reviewContents[0]); | 180 $cReviewHolder3 -> appendChild($cReview3); |
178 $review1 = str_replace("\n", "", $review1); | 181 |
179 $review1 = str_replace("\r", "", $review1); | 182 $cReviews -> appendChild($cReviewHolder); |
180 if (isset($reviewContents[1])) { | 183 $cReviews -> appendChild($cReviewHolder2); |
181 $review2 = trim($reviewContents[1]); | 184 $cReviews -> appendChild($cReviewHolder3); |
182 $review2 = str_replace("\n", "", $review2); | 185 |
183 $review2 = str_replace("\r", "", $review2); | 186 $xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews); |
184 } | 187 $newXML = simplexml_import_dom($xdoc); |
185 else { | 188 $output = $newXML->asXml(); |
186 $review2 = ""; | 189 } |
187 } | |
188 if (isset($reviewContents[2])) { | |
189 $review3 = trim($reviewContents[2]); | |
190 $review3 = str_replace("\n", "", $review3); | |
191 $review3 = str_replace("\r", "", $review3); | |
192 } | |
193 else { | |
194 $review3 = ""; | |
195 } | |
196 } | |
197 else | |
198 { | |
199 $review1 = ""; | |
200 $review2 = ""; | |
201 $review3 = ""; | |
202 //echo "EPIC FAIL"; | |
203 } | |
204 | |
205 unset($xml->Items->Item->CustomerReviews); | |
206 $xdoc = new DomDocument; | |
207 $xdoc->loadXML($xml->asXML()); | |
208 | |
209 $cReviews = $xdoc ->createElement('CustomerReviews'); | |
210 $cReviewHolder = $xdoc ->createElement('Review'); | |
211 $cReview = $xdoc ->createElement('Content'); | |
212 $cReviewHolder2 = $xdoc ->createElement('Review'); | |
213 $cReview2 = $xdoc ->createElement('Content'); | |
214 $cReviewHolder3 = $xdoc ->createElement('Review'); | |
215 $cReview3 = $xdoc ->createElement('Content'); | |
216 | |
217 $txtNode = $xdoc ->createTextNode ($review1); | |
218 $cReview -> appendChild($txtNode); | |
219 | |
220 $txtNode2 = $xdoc ->createTextNode ($review2); | |
221 $cReview2 -> appendChild($txtNode2); | |
222 | |
223 $txtNode3 = $xdoc ->createTextNode ($review3); | |
224 $cReview3 -> appendChild($txtNode3); | |
225 | |
226 $cReviewHolder -> appendChild($cReview); | |
227 $cReviewHolder2 -> appendChild($cReview2); | |
228 $cReviewHolder3 -> appendChild($cReview3); | |
229 | |
230 $cReviews -> appendChild($cReviewHolder); | |
231 $cReviews -> appendChild($cReviewHolder2); | |
232 $cReviews -> appendChild($cReviewHolder3); | |
233 | |
234 $xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews); | |
235 $newXML = simplexml_import_dom($xdoc); | |
236 $output = $newXML->asXml(); | |
237 } | |
238 | 190 |
239 switch($locale){ | 191 if($go == 1) { |
240 case "us": | 192 $item = $xml->Items->Item[0]; |
241 $loc = 0; | 193 if ($item && $item->ItemAttributes && $item->ItemAttributes->Title) { |
242 break; | 194 $title = $item->ItemAttributes->Title; } |
243 case "uk": | 195 else { $title = "[no title]"; }; |
244 $loc = 1; | 196 file_put_contents('/var/ywww/debug/phpDebug',"win: $title\n", |
245 break; | 197 FILE_APPEND); |
246 case "ca": | 198 return $output; |
247 $loc = 2; | 199 } |
248 break; | 200 else { |
249 case "de": | 201 if ($xml->Items->Item) { |
250 $loc = 3; | 202 $title = $xml->Items->Item[0]->ItemAttributes->Title; |
251 break; | 203 $author = $xml->Items->Item[0]->ItemAttributes->Author; |
252 case "fr": | 204 $binding = $xml->Items->Item[0]->ItemAttributes->Binding; |
253 $loc = 4; | 205 $dewey = $xml->Items->Item[0]->ItemAttributes->DeweyDecimalNumber; |
254 break; | 206 if($dewey == "") |
255 }; | 207 $dewey = "null"; |
208 $imageURL = $xml->Items->Item[0]->MediumImage->URL; | |
209 $salesRank = $xml->Items->Item[0]->SalesRank; | |
210 $pubDate = $xml->Items->Item[0]->ItemAttributes->PublicationDate; | |
211 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";} | |
212 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";} | |
213 if (strlen($pubDate)==0) { | |
214 $pubDate="null"; | |
215 } | |
216 else { | |
217 $pubDate="\"$pubDate\""; | |
218 } | |
219 $publisher = $xml->Items->Item[0]->ItemAttributes->Publisher; | |
220 } | |
221 else { | |
222 $title = $salesRank = ""; | |
223 $dewey = "null"; | |
224 } | |
225 | |
226 $genreID = ""; | |
227 $genre = ""; | |
228 $genArr = array(); | |
229 | |
230 if ($xml->Items->Item[0] && $xml->Items->Item[0]->BrowseNodes) { | |
231 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
232 //sexy recursive function | |
233 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
234 | |
235 if($genre != "") | |
236 $genArr[strval($genreID)] = strval($genre); | |
237 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
238 | |
239 //echo $genre; | |
240 //echo $genreID; | |
241 | |
242 $genre = ""; | |
243 $genreID = ""; | |
244 } | |
245 } | |
246 | |
247 $g1 = "null"; | |
248 $g2 = "null"; | |
249 $g3 = "null"; | |
250 $loop = 1; | |
251 | |
252 foreach ($genArr as $key => $value) { | |
253 //echo "$key => $value"; | |
254 if ($key>2047) { | |
255 //HST added | |
256 break; | |
257 } | |
258 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table | |
259 //echo $queryG; | |
260 include "../../private/db.php"; | |
261 $resG = mysqli_query($link, $queryG); | |
262 mysqli_close($link); | |
263 | |
264 switch ($loop) { | |
265 case 1: | |
266 $g1 = $key; | |
267 break; | |
268 case 2: | |
269 $g2 = $key; | |
270 break; | |
271 case 3: | |
272 $g3 = $key; | |
273 break; | |
274 } | |
275 | |
276 $loop++; | |
277 } | |
278 | |
279 if($salesRank == "") | |
280 $salesRank = "null"; | |
281 | |
282 if($title != "") | |
283 { | |
284 include "../../private/db.php"; | |
285 if ($publisher->count()==0) { | |
286 $publisher="null"; | |
287 } | |
288 else { | |
289 $publisher=mysqli_real_escape_string($link,$publisher); | |
290 if (strlen($publisher)>30) { | |
291 $publisher=rtrim(substr($publisher,0,30),"\\"); | |
292 } | |
293 $publisher="\"".$publisher."\""; | |
294 } | |
295 if ($author->count()==0) { | |
296 $author="unknown"; | |
297 } | |
298 else { | |
299 $author=mysqli_real_escape_string($link,$author); | |
300 if (strlen($author)>30) { | |
301 $author=rtrim(substr($author,0,30),"\\"); | |
302 } | |
303 } | |
304 $author="\"".$author."\""; | |
305 $title=mysqli_real_escape_string($link,$title); | |
306 if (strlen($title)>100) { | |
307 $title=rtrim(substr($title,0,100),"\\"); | |
308 } | |
309 $title="\"".$title."\""; | |
310 $review1 = mysqli_real_escape_string($link,$review1); | |
311 if (strlen($review1)>500) { $review1=rtrim(substr($review1,0,500),"\\");} | |
312 $review2 = mysqli_real_escape_string($link,$review2); | |
313 if (strlen($review2)>500) { $review2=rtrim(substr($review2,0,500),"\\");} | |
314 $review3 = mysqli_real_escape_string($link,$review3); | |
315 if (strlen($review3)>500) { $review3=rtrim(substr($review3,0,500),"\\");} | |
316 | |
317 $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",$title, $author,\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,$publisher,$g1,$g2,$g3,$loc)"; | |
318 //echo $queryInsert; | |
319 | |
320 $res = mysqli_query($link, $queryInsert); | |
321 if (!$res) { | |
322 $err=mysqli_error( $link ); | |
323 mysqli_close($link); | |
324 file_put_contents('/var/ywww/debug/phpDebug', | |
325 "anb failed: $queryInsert\n$err\n", | |
326 FILE_APPEND); | |
327 exit($err); | |
328 } | |
329 $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")"; | |
330 if($review1 != "") { | |
331 $res = mysqli_query($link, $queryInsertReviews); | |
332 if (!$res) { | |
333 $err=mysqli_error( $link ); | |
334 mysqli_close($link); | |
335 file_put_contents('/var/ywww/debug/phpDebug', | |
336 "anr failed: $queryInsertReviews\n", | |
337 FILE_APPEND); | |
338 exit($err); | |
339 } | |
340 } | |
341 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
342 } | |
343 | |
344 echo $output; | |
345 } | |
346 } | |
347 | |
348 function getAmazonDetLocal($go,$language,$loc, | |
349 $page,$operation,$searchparameter, | |
350 $searchparameterdata, | |
351 $show_array,$show_url,$show_xml) | |
352 { | |
353 global $output; | |
354 //look up info from db | |
355 include "../../private/db.php"; | |
356 $query = "CALL b_getBookInfo('$searchparameterdata', $loc)"; | |
357 //echo $query; | |
358 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
256 | 359 |
257 $errorCode = $xml->Error->Code; | 360 $output = ""; |
258 //echo $errorCode; | 361 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
259 | 362 $output .= "<Details>"; |
260 if($errorCode != "AccountLimitExceeded") | 363 if ( mysqli_num_rows( $res ) > 0 ) |
261 { | 364 { |
262 if($go == 1) { | 365 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); |
263 file_put_contents('/var/ywww/debug/phpDebug',"win: ". | 366 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
264 $xml->Items->Item->ItemAttributes->Title."\n", | 367 $output .= "<ASIN>" . $searchparameterdata . "</ASIN>"; |
265 FILE_APPEND); | 368 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; |
266 return $output; | 369 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; |
267 } | 370 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; |
268 else | 371 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; |
269 { | 372 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; |
270 $title = $xml->Items->Item->ItemAttributes->Title; | 373 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; |
271 $author = $xml->Items->Item->ItemAttributes->Author; | 374 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; |
272 $binding = $xml->Items->Item->ItemAttributes->Binding; | 375 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; |
273 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber; | 376 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; |
274 if($dewey == "") | 377 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; |
275 $dewey = "null"; | 378 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; |
276 $imageURL = $xml->Items->Item->MediumImage->URL; | 379 $output .= "<ProductGroup>Book</ProductGroup>"; |
277 $salesRank = $xml->Items->Item->SalesRank; | 380 $output .= "<Error>AccountLimitExceeded</Error>"; |
278 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; | 381 |
279 $publisher = $xml->Items->Item->ItemAttributes->Publisher; | 382 } |
280 | 383 else { |
281 $genreID = ""; | 384 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
282 $genre = ""; | 385 } |
283 $genArr = array(); | 386 $output .= "</Details>"; |
284 | 387 echo $output; |
285 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
286 //sexy recursive function | |
287 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
288 | |
289 if($genre != "") | |
290 $genArr[strval($genreID)] = strval($genre); | |
291 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
292 | |
293 //echo $genre; | |
294 //echo $genreID; | |
295 | |
296 $genre = ""; | |
297 $genreID = ""; | |
298 } | |
299 | |
300 $g1 = "null"; | |
301 $g2 = "null"; | |
302 $g3 = "null"; | |
303 $loop = 1; | |
304 | |
305 foreach ($genArr as $key => $value) { | |
306 //echo "$key => $value"; | |
307 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table | |
308 //echo $queryG; | |
309 include "../../private/db.php"; | |
310 $resG = mysqli_query($link, $queryG); | |
311 mysqli_close($link); | |
312 | |
313 switch ($loop) { | |
314 case 1: | |
315 $g1 = $key; | |
316 break; | |
317 case 2: | |
318 $g2 = $key; | |
319 break; | |
320 case 3: | |
321 $g3 = $key; | |
322 break; | |
323 } | |
324 | |
325 $loop++; | |
326 } | |
327 | |
328 if($salesRank == "") | |
329 $salesRank = "null"; | |
330 | |
331 $title = strtr($title, '"', "'"); | |
332 include "../../private/db.php"; | |
333 $review1 = mysqli_real_escape_string($link,$review1); | |
334 $review2 = mysqli_real_escape_string($link,$review2); | |
335 $review3 = mysqli_real_escape_string($link,$review3); | |
336 | |
337 if($title != "") | |
338 { | |
339 $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)"; | |
340 //echo $queryInsert; | |
341 $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")"; | |
342 | |
343 $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link )); | |
344 if($review1 != "" && $review2 != "" && $review3 != "") | |
345 $resG = mysqli_query($link, $queryInsertReviews) or exit( mysqli_error( $link )); | |
346 | |
347 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
348 } | |
349 | |
350 echo $output; | |
351 } | |
352 } | |
353 else | |
354 { | |
355 //look up info from db | |
356 include "../../private/db.php"; | |
357 $query = "CALL b_getBookInfo('$searchparameterdata', $loc)"; | |
358 //echo $query; | |
359 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
360 | |
361 $output = ""; | |
362 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
363 $output .= "<Details>"; | |
364 if ( mysqli_num_rows( $res ) > 0 ) | |
365 { | |
366 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); | |
367 | |
368 $output .= "<ASIN>" . $searchparameterdata . "</ASIN>"; | |
369 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
370 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
371 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
372 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
373 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
374 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
375 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
376 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
377 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
378 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
379 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
380 $output .= "<ProductGroup>Book</ProductGroup>"; | |
381 $output .= "<Error>AccountLimitExceeded</Error>"; | |
382 | |
383 } | |
384 $output .= "</Details>"; | |
385 echo $output; | |
386 } | |
387 } | 388 } |
388 | 389 |
389 function findGenre($browseNode, &$ID, &$gen) | 390 function findGenre($browseNode, &$ID, &$gen) |
390 { | 391 { |
391 if($browseNode->Name == "Subjects") | 392 if($browseNode->Name == "Subjects") |
406 } | 407 } |
407 } | 408 } |
408 | 409 |
409 if(!isset($ret)) | 410 if(!isset($ret)) |
410 { | 411 { |
411 include "aws_signed_request.php"; | 412 include "aws_signed_request.php"; |
412 getAmazonDet('default',0,'us'); //will get overwritten | 413 getAmazonDet('default',0,'us'); //will get overwritten from URI params |
414 // by the foreach ($Adefault as ... loop above | |
413 } | 415 } |
414 /*Caught oneSimpleXMLElement Object | 416 /*Caught oneSimpleXMLElement Object |
415 ( | 417 ( |
416 [OperationRequest] => SimpleXMLElement Object | 418 [OperationRequest] => SimpleXMLElement Object |
417 ( | 419 ( |
576 | 578 |
577 ) | 579 ) |
578 | 580 |
579 ) | 581 ) |
580 */ | 582 */ |
583 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ | |
581 ?> | 584 ?> |