Mercurial > hg > ywww
annotate php/read/getBookDetails.php @ 43:dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
use dl(...) for debug logging, defined in dlog.php
use doAmazonRequest in amazonBookSearch
use ../private/web.php (q.v., not in mercurial) for Aserver and Aassociates
Started by updating from 40:c24ae74bf6d5, i.e. just before the bug on the main line
author | Charlie Root |
---|---|
date | Sat, 05 Jan 2019 18:00:10 -0500 |
parents | c24ae74bf6d5 |
children | 38d209611508 |
rev | line source |
---|---|
2 | 1 <?php |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
2 include_once "dlog.php"; |
2 | 3 |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
4 include_once "web.php"; |
2 | 5 function findGenre($browseNode, &$ID, &$gen) |
6 { | |
7 if($browseNode->Name == "Subjects") | |
8 { | |
9 return true; | |
10 } | |
11 else | |
12 { | |
13 if($browseNode->Ancestors->BrowseNode) | |
14 { | |
15 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) | |
16 { | |
17 $gen = $browseNode->Name; | |
18 $ID = $browseNode->BrowseNodeId; | |
19 | |
20 //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well | |
21 // return true; | |
22 } | |
23 } | |
24 return false; | |
25 } | |
26 } | |
27 | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
28 global $out,$isbn,$Aserver,$Aassociates_id; |
2 | 29 if(isset($_GET['isbn'])){ |
30 $isbn = $_GET['isbn']; | |
31 if(isset($_GET['locale'])){ | |
32 $locale=$_GET['locale']; | |
33 | |
34 switch ($locale) { | |
35 case "uk": | |
36 $loc = 1; | |
37 break; | |
38 case "us": | |
39 $loc = 0; | |
40 break; | |
41 case "ca": | |
42 $loc = 2; | |
43 break; | |
44 case "de": | |
45 $loc = 3; | |
46 break; | |
47 case "fr": | |
48 $loc = 4; | |
49 break; | |
50 case "": | |
51 $loc = 0; | |
52 break; | |
53 } | |
54 } | |
55 else{ | |
56 $loc=0; | |
57 } | |
58 $output = ""; | |
59 | |
60 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
61 $output .= "<BookDetails>"; | |
62 | |
63 $proceed = true; | |
64 include "../../../private/db.php"; | |
65 | |
66 $query = "CALL b_getBookInfo('$isbn', $loc)"; | |
67 //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'"; | |
68 //echo $query; | |
69 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
70 | |
71 if ( mysqli_num_rows( $res ) > 0 ) | |
72 { | |
73 $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
|
74 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
2 | 75 $timestamp=$rows['Timestamp']; |
76 //echo $timestamp; | |
77 date_default_timezone_set('UTC'); | |
78 $dbStamp = strtotime($timestamp); | |
79 //echo $dbStamp; | |
80 $unixTime = time(); | |
81 //echo $unixTime; | |
82 $timeDiff = $unixTime - $dbStamp; | |
83 //echo $timeDiff; | |
84 | |
85 if($timeDiff < 2592000) //if it has been updated in the last 30 days | |
86 { | |
87 $proceed = false; | |
88 $output .= "<ISBN>" . $isbn . "</ISBN>"; | |
89 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
90 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
91 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
92 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
93 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
94 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
95 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
96 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
97 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
98 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
99 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
100 $output .= "<ProductGroup>Book</ProductGroup>"; | |
101 } | |
102 } | |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
103 else { |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
104 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
|
105 } |
2 | 106 |
107 if($proceed == true) | |
36 | 108 { |
109 include "aws_signed_request.php"; | |
110 include_once "../../xml/doAmazonRequest.inc"; | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
111 dl("nbd proceeding: $isbn\n"); |
36 | 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 |
131 | |
36 | 132 //for all parameters see if the user has overruled it or use the default |
133 foreach ($Adefault as $i=>$d) { | |
134 $$i=isset($_GET[$i])?$_GET[$i]:$d; | |
135 } | |
136 | |
137 $parameters=array( | |
138 'Operation' =>$operation , | |
139 'Keywords' =>urlencode($search) , | |
140 "$searchparameter"=>$searchparameterdata , | |
141 'ItemPage' =>$page , //which page? | |
142 'AssociateTag' =>$Aassociates_id[$locale], | |
143 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes' | |
144 ); | |
145 | |
146 if ($searchindex!='Books') { | |
147 $parameters['SearchIndex']=$searchindex; | |
148 } | |
2 | 149 |
36 | 150 try { |
38 | 151 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,6,'d'); |
36 | 152 //use this xml to pull out the necessary information and save it |
153 set_error_handler(function () { | |
154 global $out,$isbn; | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
155 dl("Caught one bd: $isbn\n"); |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
156 dl(print_r($out, TRUE)."\n"); |
36 | 157 } ); |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
158 include "db.php"; |
36 | 159 |
160 $title=""; | |
161 $author=""; | |
162 $binding=""; | |
163 $dewey=""; | |
164 $imageURL=""; | |
165 $salesRank=""; | |
166 $pubDate=""; | |
167 $publisher=""; | |
168 | |
169 $title = $xml->Items->Item->ItemAttributes->Title; | |
170 $author = $xml->Items->Item->ItemAttributes->Author; | |
171 $binding = $xml->Items->Item->ItemAttributes->Binding; | |
172 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber; | |
173 if($dewey == "") | |
174 $dewey = "null"; | |
175 $imageURL = $xml->Items->Item->MediumImage->URL; | |
176 $salesRank = $xml->Items->Item->SalesRank; | |
177 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; | |
178 $publisher = $xml->Items->Item->ItemAttributes->Publisher; | |
179 restore_error_handler(); | |
180 if ($publisher and strlen($publisher)>30) { | |
181 $publisher=substr($publisher,0,30); | |
182 } | |
183 if ($author and strlen($author)>30) { | |
184 $author=substr($author,0,30); | |
185 } | |
186 if ($title and strlen($title)>100) { | |
187 $title=substr($title,0,100); | |
188 } | |
189 $publisher=mysqli_real_escape_string($link,$publisher); | |
190 $author=mysqli_real_escape_string($link,$author); | |
191 $title=mysqli_real_escape_string($link,$title); | |
192 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";} | |
193 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";} | |
194 if (strlen($pubDate)==0) { | |
195 $pubDate="null"; | |
196 } | |
197 else { | |
198 $pubDate="\"$pubDate\""; | |
199 } | |
200 /*echo $title; | |
201 echo $author; | |
202 echo $binding; | |
203 echo $dewey; | |
204 echo $imageURL; | |
205 echo $salesRank; | |
206 echo $pubDate; | |
207 echo $publisher;*/ | |
208 | |
209 $genreID = ""; | |
210 $genre = ""; | |
211 $genArr = array(); | |
212 $g1 = "null"; | |
213 $g2 = "null"; | |
214 $g3 = "null"; | |
215 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){ | |
216 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
217 //sexy recursive function | |
218 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
219 | |
220 if($genre != "") | |
221 $genArr[strval($genreID)] = strval($genre); | |
222 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
2 | 223 |
36 | 224 //echo $genre; |
225 //echo $genreID; | |
226 | |
227 $genre = ""; | |
228 $genreID = ""; | |
21
46382face560
use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents:
18
diff
changeset
|
229 } |
36 | 230 |
231 $g1 = "null"; | |
232 $g2 = "null"; | |
233 $g3 = "null"; | |
234 $loop = 1; | |
235 | |
236 foreach ($genArr as $key => $value) { | |
237 //echo "$key => $value"; | |
238 if ($key>2047) { | |
239 //HST added | |
240 break; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
241 } |
36 | 242 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table |
243 //echo $queryG; | |
244 $resG = mysqli_query($link, $queryG); | |
245 | |
246 switch ($loop) { | |
247 case 1: | |
248 $g1 = $key; | |
249 break; | |
250 case 2: | |
251 $g2 = $key; | |
252 break; | |
253 case 3: | |
254 $g3 = $key; | |
255 break; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
256 } |
36 | 257 |
258 $loop++; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
259 } |
36 | 260 } |
261 | |
262 if($salesRank == "") | |
263 $salesRank = "null"; | |
2 | 264 |
36 | 265 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
266 include "../../../private/db.php"; | |
2 | 267 |
36 | 268 $title = strtr($title, '"', "'"); |
269 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)"; | |
270 //echo $queryInsert; | |
2 | 271 |
36 | 272 $res = mysqli_query($link, $queryInsert); |
273 if (!$res) { | |
274 $err=mysqli_error( $link ); | |
275 mysqli_close($link); | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
276 dl("anb failed: $err, $pubDate, $g2, $publisher, $title\n"); |
36 | 277 exit($err); |
278 } | |
279 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
2 | 280 |
281 | |
36 | 282 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; |
283 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; | |
284 $output .= "<Author>" . htmlspecialchars($author) . "</Author>"; | |
285 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>"; | |
286 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>"; | |
287 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>"; | |
288 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>"; | |
289 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>"; | |
290 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>"; | |
291 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>"; | |
292 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>"; | |
293 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>"; | |
294 $output .= "<ProductGroup>Book</ProductGroup>"; | |
295 } | |
296 catch (Exception $e) { | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
297 dl("gBD: dAR failed:".$e->getMessage()."\n"); |
36 | 298 } |
299 } | |
2 | 300 $output .= "</BookDetails>"; |
301 | |
302 echo $output; | |
303 } | |
304 ?> |