Changeset 1274849
- Timestamp:
- 10/28/2015 08:26:57 PM (10 years ago)
- Location:
- check-amazon-link/trunk
- Files:
-
- 2 edited
-
AzWorker.php (modified) (5 diffs)
-
amazon-link-checker.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
check-amazon-link/trunk/AzWorker.php
r1257824 r1274849 194 194 } 195 195 196 197 /** 198 * This function accepts an array of up to 10 asins 199 * the region must be the same for all of them! 200 * 201 * @param $asins string|array 202 * @param $region string 203 * 204 * @return null|array if successful, returns the response array from the get request 205 */ 206 function getProductDetailsFromAmazon($asins, $region) { 207 208 if(is_array($asins)) { 209 // make it a comma separated list (String) 210 $asins = implode(",", $asins); 211 } 212 213 $params = array( 214 "Service" => "AWSECommerceService", 215 "Operation" => "ItemLookup", 216 "ResponseGroup" => "ItemAttributes, OfferSummary, VariationSummary", 217 "ItemId" => $asins, 218 "IdType" => "ASIN", 219 ); 220 $url = azlc_aws_signed_request( $region, $params, $this->azlkch_options['AWSAccessKeyId'], $this->azlkch_options['AWSSecretKey'], 221 $this->azlkch_options['associate_tag'], '2011-08-01' ); 222 223 global $azlc_logger; 224 $azlc_logger->write( $url ); 225 $response = wp_remote_get( $url ); 226 227 if ( is_wp_error( $response ) ) { 228 $error_string = $response->get_error_message(); 229 230 $azlc_logger->write( 231 '<div id="message" class="error"><p>' . $error_string . '</p> 232 <P>' . $url . '</P> 233 <P>The Region: ' . esc_html( $region ) . '</P>' 234 . '<pre>' . print_r( $this->azlkch_options ) . '</pre> 235 </div>' ); 236 return null; 237 } else { 238 $azlc_logger->write( "did not get any erro" ); 239 return $response; 240 } 241 } 242 243 function saveProductDetails($response_array, $region) { 244 /* @var $wpdb WPDB */ 245 global $wpdb; 246 global $azlc_database; 247 if($response_array) { 248 $body = wp_remote_retrieve_body( $response_array ); 249 $data = $this->parseAmazonXMLDataforProductDetails($body); 250 251 //for each item details 252 //update product_table if title is different 253 //insert into prices table 254 if(!empty($data['items'])) { 255 foreach($data['items'] as $item) { 256 257 //check if title is different 258 $title = $wpdb->get_var("SELECT title FROM " . $azlc_database->product_table . " 259 WHERE asin = '" . $item['asin'] . "'"); 260 if($title) { 261 if(strcmp($title, $item['Title'])!==0) { 262 //they are different, we need to update database 263 //$wpdb->update( $table, $data, $where, $format = null, $where_format = null ) 264 $wpdb->update($azlc_database->product_table, array('title'=>$item['Title']), array 265 ('asin' => $item['asin'])); 266 } 267 } else { 268 $abstract = $item['variation'] > 0 ? 1 : 0; 269 $item['abstract'] = $abstract; 270 271 272 $wpdb->insert( 273 $azlc_database->product_table, 274 array( 275 'asin' => $item['asin'], 276 'title' => $item['Title'], 277 'product_group' => $item['ProductGroup'], 278 'region' => $region, 279 'abstract' => $abstract 280 ) ); 281 } 282 283 //insert into prices database 284 285 $item['stock_status'] = AZLC_Utility::fuzzy_stock_status( $item ); 286 287 288 $wpdb->insert( 289 $azlc_database->product_data_table, 290 array( 291 'asin' => $item['asin'], 292 'TotalNew' => $item['TotalNew'], 293 'TotalUsed' => $item['TotalUsed'], 294 'TotalCollectible' => $item['TotalCollectible'], 295 'TotalRefurbished' => $item['TotalRefurbished'], 296 'LowestUsedPrice' => $item['LowestUsedPrice'], 297 'LowestCollectiblePrice' => $item['LowestCollectiblePrice'], 298 'LowestNewPrice' => $item['LowestNewPrice'], 299 'LowestRefurbishedPrice' => $item['LowestRefurbishedPrice'], 300 //'error_code' => $item['error_code'], 301 //'error_message' => $item['error_message'], 302 'time_of_retrieval' => current_time( 'mysql' ), 303 'stock_status' => $item['stock_status'] 304 ) ); 305 306 307 } 308 309 } 310 } 311 312 } 313 314 315 316 317 /** 318 * Parses Amazon XML response into a multidimensional array 319 * This function will work with single or multiple ASIN lookups 320 * 321 * Returns a multidimensional array 322 * The inner Items array contains the price data 323 * The inner Errors array contains an array of error messages 324 * @param $xml 325 * 326 * @return array array("items"=>, "errors"=>) 327 * @throws Exception 328 */ 329 function parseAmazonXMLDataforProductDetails($xml) { 330 $pxml = simplexml_load_string($xml); 331 $data = array("items"=>array(), "errors" =>array()); 332 333 if ($pxml === FALSE) { 334 $data["errors"][] = "Response could not be parsed."; 335 return $data; 336 } 337 338 339 $isValid = (string) $pxml->Items->Request->IsValid; 340 if(strcmp('True', $isValid)!==0) { 341 $error_string = "Invalid Request"; 342 if(isset($pxml->ItemLookupErrorResponse->Error->Code)) { 343 $error_string .= ", Error Code: " . (string) $pxml->ItemLookupErrorResponse->Error->Code; 344 } 345 if(isset($pxml->ItemLookupErrorResponse->Error->Message)) { 346 $error_string .= ", Error Message: " . (string) $pxml->ItemLookupErrorResponse->Error->Message; 347 } 348 $data["errors"][] = $error_string; 349 return $data; 350 } 351 352 353 $i = 0; 354 if(isset($pxml->Items->Request->Errors->Error)) { 355 foreach ( $pxml->Items->Request->Errors->Error as $error ) { 356 $error_string = 'Amazon Response Contains Error: '; 357 if ( isset( $error->Code ) ) { 358 $error_string .= "Error Code: " . (string) $error->Code; 359 } 360 if ( isset( $error->Message ) ) { 361 $error_string .= ", Error Message: " . (string) $error->Message; 362 } 363 $data["errors"][ $i ] = $error_string; 364 $i ++; 365 } 366 } 367 368 $i = 0; 369 foreach ($pxml->Items->Item as $item){ 370 if(!isset($item->ASIN)) { 371 continue; 372 } 373 374 $data['items'][$i]['asin'] = (string) $item->ASIN; 375 376 if (isset($item->ItemAttributes->Title)) { 377 $data['items'][$i]['Title'] = (string) $item->ItemAttributes->Title; 378 } else { 379 $data['items'][$i]['Title'] = Null; 380 } 381 382 if (isset($item->ItemAttributes->ProductGroup)) { 383 $data['items'][$i]['ProductGroup'] = (string)$item->ItemAttributes->ProductGroup; 384 } else { 385 $data['items'][$i]['ProductGroup'] = Null; 386 } 387 388 389 if (isset($item->OfferSummary->LowestUsedPrice->Amount)) { 390 $data['items'][$i]['LowestUsedPrice'] = (string)$item->OfferSummary->LowestUsedPrice->Amount; 391 } else { 392 $data['items'][$i]['LowestUsedPrice'] = Null; 393 } 394 395 396 if (isset($item->OfferSummary->LowestCollectiblePrice->Amount)) { 397 $data['items'][$i]['LowestCollectiblePrice'] = (string)$item->OfferSummary->LowestCollectiblePrice->Amount; 398 } else { 399 $data['items'][$i]['LowestCollectiblePrice'] = Null; 400 } 401 402 403 404 if (isset($item->OfferSummary->LowestRefurbishedPrice->Amount)) { 405 $data['items'][$i]['LowestRefurbishedPrice'] = (string)$item->OfferSummary->LowestRefurbishedPrice->Amount; 406 } else { 407 $data['items'][$i]['LowestRefurbishedPrice'] = Null; 408 } 409 410 411 if (isset($item->OfferSummary->LowestNewPrice->Amount)) { 412 $data['items'][$i]['LowestNewPrice'] = (string)$item->OfferSummary->LowestNewPrice->Amount; 413 } else { 414 $data['items'][$i]['LowestNewPrice'] = Null; 415 } 416 417 418 if (isset($item->Offers->TotalOffers)) { 419 $data['items'][$i]['TotalOffers'] = (string)$item->Offers->TotalOffers; 420 } else { 421 $data['items'][$i]['TotalOffers'] = Null; 422 } 423 424 if (isset($item->OfferSummary->TotalNew)) { 425 $data['items'][$i]['TotalNew'] = (string)$item->OfferSummary->TotalNew; 426 } else { 427 $data['items'][$i]['TotalNew'] = Null; 428 } 429 430 431 if (isset($item->OfferSummary->TotalUsed)) { 432 $data['items'][$i]['TotalUsed'] = (string)$item->OfferSummary->TotalUsed; 433 } else { 434 $data['items'][$i]['TotalUsed'] = Null; 435 } 436 437 if (isset($item->OfferSummary->TotalCollectible)) { 438 $data['items'][$i]['TotalCollectible'] = (string)$item->OfferSummary->TotalCollectible; 439 } else { 440 $data['items'][$i]['TotalCollectible'] = Null; 441 } 442 443 if (isset($item->OfferSummary->TotalRefurbished)) { 444 $data['items'][$i]['TotalRefurbished'] = (string) $item->OfferSummary->TotalCollectible; 445 } else { 446 $data['items'][$i]['TotalRefurbished'] = Null; 447 } 448 449 //VariationSummary->LowestPrice->Amount 450 451 if (isset($item->VariationSummary->LowestPrice->Amount)) { 452 $data['items'][$i]['variation'] = (string) $item->VariationSummary->LowestPrice->Amount; 453 } else { 454 $data['items'][$i]['variation'] = 0; 455 } 456 $i++; 457 } 458 459 return $data; 460 } 461 462 463 464 465 196 466 /** 197 467 * @param $asin … … 492 762 global $wpdb, $azlc_database; 493 763 $wpdb->query( "TRUNCATE TABLE " . $azlc_database->post_status_table ); 764 $wpdb->query( "TRUNCATE TABLE " . $azlc_database->link_instances_table ); 765 $wpdb->query( "TRUNCATE TABLE " . $azlc_database->product_data_table ); 766 $wpdb->query( "TRUNCATE TABLE " . $azlc_database->product_table ); 494 767 $internal_opts['pages_parsed'] = 0; 495 768 $options['reset'] = 0; … … 558 831 } 559 832 560 561 // 1) it gets a product from the link instance table562 // 2) if the product is not in the product details page563 // 3) then look up the product details and keep the row the SAME564 // 4) else, look up the price, and increment the row number565 833 public function lookup_next_asin() { 834 //goal: look up as many as 10 asins at a time 566 835 /* @var $wpdb WPDB */ 567 836 global $azlc_database, $wpdb, $azlc_logger; … … 570 839 $this->update_priority_queue(); 571 840 572 //now, check priority queue... if not empty, look up an asin from the priority queue573 841 $priority_queue = get_option( 'azlc_prq', array() ); 574 842 if ( ! empty( $priority_queue ) ) { 575 $asin = array_pop( $priority_queue ); 843 $asins = array(); 844 $asins[] = array_pop( $priority_queue ); 845 $region = $this->get_region( $asins[0] ); 846 $i=0; 847 if(is_null($region)) { 848 $region = $this->getDefaultRegion(); 849 } 850 while($i<10 && !empty($priority_queue)) { 851 $next_asin = array_pop($priority_queue); 852 $region2 = $this->get_region($next_asin); 853 if(strcmp($region, $region2)!==0) { 854 //if the regions are not the same, we can't look them up in one request 855 //put it back on the priority queue for next time 856 $priority_queue[] = $next_asin; 857 break; 858 } 859 $asins[] = $next_asin; 860 $i++; 861 //todo: consider adding more asins if this is less than 10! 862 } 576 863 update_option( 'azlc_prq', $priority_queue ); 577 $region = $this->get_region( $asin ); 578 $azlc_logger->write( "Got this asin from the priority queue: " . $asin ); 579 } else { // priority queue is empty so we will just look up the next asin in the links table 580 581 $query = "SELECT DISTINCT asin, region FROM " . $azlc_database->link_instances_table . " ORDER BY post_id"; 864 // now we should have an array of up to 10 asins with all the same region 865 } else { 582 866 $internal_options_array = get_option( 'azlc_plugin_options_internal' ); 583 $results = $wpdb->get_row( $query, ARRAY_A, $internal_options_array['continual_row'] ); 584 585 if ( is_null( $results ) ) { 867 $begin_row = $internal_options_array['continual_row']; 868 $time = current_time('mysql'); 869 $query = "SELECT DISTINCT asin, region FROM " . $azlc_database->link_instances_table . " 870 WHERE asin NOT IN (SELECT asin FROM " . $azlc_database->product_data_table . 871 " WHERE time_of_retrieval > 872 ( '" . $time . "' 873 - INTERVAL + 1 DAY)) ORDER BY post_id, region LIMIT " . $begin_row . " , 10"; 874 875 $items = $wpdb->get_results( $query, ARRAY_A ); 876 877 878 879 if ( is_null( $items ) || empty($items) ) { 586 880 587 881 //check if table is empty, if empty, exit (prevents inifinite loop) … … 601 895 } 602 896 603 $asin = $results['asin']; 604 $region = $results['region']; 605 606 $azlc_logger->write( "lookup_next_asin last query: " . $wpdb->last_query ); 607 $azlc_logger->write( "and the last error was: " . $wpdb->last_error ); 608 609 // increment row number for next time 610 $internal_options_array['continual_row'] ++; 897 898 $region = $items[0]['region']; 899 if(is_null($region)) { 900 $region = $this->getDefaultRegion(); 901 } 902 $asins = array(); 903 904 905 foreach ( $items as $item ) { 906 if ( strcmp( $item['region'], $region ) === 0 ) { 907 $asins[] = $item['asin']; 908 } else { 909 break; 910 } 911 } 912 913 $internal_options_array['continual_row'] = $internal_options_array['continual_row'] + count($asins); 611 914 update_option( 'azlc_plugin_options_internal', $internal_options_array ); 612 915 } 613 916 614 // check if region is unknown 615 if ( empty( $region ) ) { 616 // get default region 617 $azlc_logger->write( "Region is empty, getting default" ); 618 $options = get_option( 'azlc_plugin_options', array( 'region' => 'com' ) ); 619 $region = $options['region']; 620 } 621 622 // check to see if the product is in the product details table 623 $query = $wpdb->prepare( "SELECT asin FROM $azlc_database->product_table WHERE asin = %s", $asin ); 624 $wpdb->get_results( $query ); 625 626 if ( $wpdb->num_rows == 0 ) { 627 // we don't have any data about that product, lets fix that 628 $this->getProductDetailsAndStore( $asin, $region ); 629 // this needs to be on the priority queue still because the prices haven't yet been looked up 630 $this->add_to_priority_queue( $asin ); 631 } else { 632 // we already know what the product is, get prices! 633 $this->getCurrentPricesAndStore( $asin, $region ); 634 } 635 636 return $asin; 637 } 917 if(!empty($asins)) { 918 $res = $this->getProductDetailsFromAmazon($asins, $region); 919 $this->saveProductDetails($res, $region); 920 } 921 922 } 923 924 925 public function getDefaultRegion() { 926 $options = get_option( 'azlc_plugin_options', array( 'region' => 'com' ) ); 927 return $options['region']; 928 } 929 930 638 931 639 932 public function add_to_priority_queue( $asin ) { -
check-amazon-link/trunk/amazon-link-checker.php
r1272405 r1274849 226 226 $options = AmazonLinkCheckerCore::load_options(); 227 227 $internal_opts = get_option( 'azlc_plugin_options_internal' ); 228 228 229 $azworker = new AzWorker( $options ); 230 231 if(isset($options['reset'])) { 232 if($options['reset']==1) { 233 $azworker->reset(); 234 } 235 } 236 237 229 238 $post_ids = get_posts_to_parse( $azworker ); 230 239 … … 291 300 // this indicates that all posts are parsed, 292 301 // so, we should look up a product 293 $asin_looked_up = $azworker->lookup_next_asin(); 294 $x['asin'] = $asin_looked_up; 302 $azworker->lookup_next_asin(); 303 //todo: remove this 304 $x['asin'] = 'multiple'; 295 305 $x['interval'] = $options['min_sleep_time']; 296 306 } else { … … 298 308 $azworker->parse_single_post( $post_ids[0] ); 299 309 $x['interval'] = $options['ajax_sleep_time_parsing']; 310 311 //also, begin to look up the products 312 if(count($post_ids) % 3 === 0 ) { 313 //do this approx. every 3rd time 314 $azworker->lookup_next_asin(); 315 } 316 300 317 } 301 318
Note: See TracChangeset
for help on using the changeset viewer.