Mercurial > hg > ywww
view php/read/similar.php @ 21:46382face560
use a semaphore to try to cut down on RequestThrottled fails
author | Charlie Root |
---|---|
date | Thu, 25 Oct 2018 09:42:12 -0400 |
parents | ae1459564f66 |
children | d606320ec331 |
line wrap: on
line source
<?php function dbConnect($go) { if($go == 1) { return "../../private/db.php"; } else { return "../../../private/db.php"; } } function addAmazonSimilarBooks($isbn1, $loc, $go) { include dbConnect($go); $queryLoc = "CALL getLocation('$loc')"; //"select locname from location where LocID = $loc"; $res = mysqli_query($link, $queryLoc) or exit( mysqli_error( $link )); mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop include dbConnect($go); $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); $locale=$rows['locname']; $Adefault=array( 'language' =>'en', //what language to render the page in 'locale' =>$locale, //which server's products? available: ca,de,fr,jp,uk,us 'page' =>1, 'operation' =>'SimilarityLookup', 'searchparameter' =>'ItemId', //what kind of search? 'searchparameterdata'=>$isbn1, //what to search for? ); $Aassociates_id=array( 'uk' => 'bookwhack-21', 'us' => 'your02b-20', 'ca' => 'book009-20', 'de' => 'book04c-21', 'fr' => 'book07f-21', ); $Aserver=array( 'ca' => array( 'ext' => 'ca' , //Canadian normal server 'nor' => 'http://www.amazon.ca' , //Canadian normal server 'xml' => 'http://xml.amazon.com' , //Canadian xml server ), 'de' => array( 'ext' => 'de' , //German normal server 'nor' => 'http://www.amazon.de' , //German normal server 'xml' => 'http://xml-eu.amazon.com', //German xml server ), 'fr' => array( 'ext' => 'fr' , //French normal server 'nor' => 'http://www.amazon.fr' , //French normal server 'xml' => 'http://xml-eu.amazon.com', //French xml server ), 'jp' => array( 'ext' => 'jp' , //Japanese normal server, not co.jp! 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server 'xml' => 'http://xml.amazon.com' , //Japanese xml server ), 'uk' => array( 'ext' => 'co.uk' , //UK normal server 'nor' => 'http://www.amazon.co.uk' , //UK normal server 'xml' => 'http://xml-eu.amazon.com', //UK xml server ), 'us' => array( 'ext' => 'com' , //USA normal server 'nor' => 'http://www.amazon.com' , //USA normal server 'xml' => 'http://xml.amazon.com' , //USA xml server ), ); include "aws_signed_request.php"; $public_key ="AKIAIHTNWC7L6LOUY4LQ"; $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; //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) , //'SearchIndex' =>$searchindex , //Books for example. "$searchparameter"=>$searchparameterdata , 'ItemPage' =>$page , //which page? 'AssociateTag' =>$Aassociates_id[$locale], 'ResponseGroup' =>'Small' , //Small, Medium, Large or SellerListing, Similarities 'Availability'=>'Available', ); $ext=$Aserver[$locale]['ext']; $url=aws_signed_request($ext,$parameters,$public_key,$private_key); //echo $url; $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $gotit=0; $semaphore = new SyncSemaphore("Amazon"); $gotit = $semaphore->lock(4000); if (!$gotit) { file_put_contents('/var/ywww/debug/phpDebug', "Got it: ".(int)$gotit." 4\n",FILE_APPEND); $gotit = $semaphore->lock(5000); file_put_contents('/var/ywww/debug/phpDebug', "Got it: ".(int)$gotit." $requestIP 4a\n",FILE_APPEND); } $ret = curl_exec($crl); curl_close($crl); usleep(1000000); if ($gotit) { $semaphore->unlock(); } else { file_put_contents('/var/ywww/debug/phpDebug', "Unlocking 4\n",FILE_APPEND); } //echo $ret; $xml = new SimpleXMLElement($ret); //$isbn1 = $xml->Items->Request->SimilarityLookupRequest->ItemId; //echo $isbn1; $result = ""; for($i=0;$i<sizeof($xml->Items->Item);$i++){ foreach($xml->Items->Item[$i]->ASIN as $isbn2){ // echo $isbn2; $group = $xml->Items->Item[$i]->ItemAttributes->ProductGroup; //foreach one of these, create a new request to check whether its a book. //if it is then add it to the return (or add it to the database) if($group == 'Book') { $query1 = "CALL b_getLinkID('$isbn1', '$isbn2', $loc)"; // "select ScoreID from scoretable where BookID1 = '$isbn1' and BookID2 = '$isbn2' and LocID = $loc"; //echo $query1; $data1 = mysqli_query($link, $query1) or exit( mysqli_error( $link )); mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop include dbConnect($go); if ( mysqli_num_rows( $data1 ) < 1 ) { $queryL = "CALL b_addNewLink('$isbn1','$isbn2',0,$loc)"; //echo $queryL; $results1 = mysqli_query($link, $queryL); $rows=mysqli_fetch_array($results1, MYSQLI_ASSOC); $linkid=$rows['LinkID']; //reset connection mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop include dbConnect($go); //$scoreQuery = "CALL b_insertTempScore('$isbn1','$isbn2',$loc,$linkid,0)"; //echo $scoreQuery; //$scoreFinal = mysqli_query($link, $scoreQuery); $result .= "<type>0</type>"; $result .= "<isbn>" . $isbn2 . "</isbn>"; $result .= "<linkID>" . trim($linkid) . "</linkID>"; } } } } return $result; } ?>