/** * need to work out page_no, items_per_page, offset, etc * * This method will set the $this->_is_next_page and $this->is_previous_page * flags. */ function _queryListing(&$HTTP_VARS) { // reset errors $this->_errors = NULL; $at_least_one_search_field_populated = FALSE; $input_field_values_r = array(); $this->_search_query = NULL; // need to get a list of all input field values to pass into the querySite call. $results = fetch_site_plugin_input_field_rs($this->_type); if ($results) { while ($input_field_r = db_fetch_assoc($results)) { if (isset($HTTP_VARS[$input_field_r['field']]) && strlen($HTTP_VARS[$input_field_r['field']]) > 0) { $field_value = trim($HTTP_VARS[$input_field_r['field']]); $at_least_one_search_field_populated = TRUE; if ($input_field_r['field_type'] == 'scan-isbn' || $input_field_r['field_type'] == 'scan-upc') { // Determine type of scanner. if (strrpos($HTTP_VARS[$input_field_r['field']], '.')) { // cuecat if ($input_field_r['field_type'] == 'scan-isbn') { $scanCode = get_cuecat_isbn_code($field_value); } else { $scanCode = get_cuecat_upc_code($field_value); } } else { //non-cuecat or modified cuecat if ($input_field_r['field_type'] == 'scan-isbn') { $scanCode = get_isbn_code($field_value); } else { $scanCode = get_upc_code($field_value); } } if ($scanCode !== FALSE) { $field_value = $scanCode; } } $input_field_values_r[$input_field_r['field']] = $field_value; $this->_search_query[] = array('field' => $input_field_r['field'], 'value' => $field_value, 'prompt' => $input_field_r['prompt'], 'field_type' => $input_field_r['field_type']); } } //while db_free_result($results); } // only continue if at least one input field was populated with a value. if ($at_least_one_search_field_populated) { unset($this->_item_list_rs); // initialise if not set. if (!is_numeric($HTTP_VARS['page_no'])) { $this->_page_no = 1; } else { $this->_page_no = $HTTP_VARS['page_no']; } if ($this->_page_no > 1) { $this->_is_previous_page = TRUE; } else { $this->_is_previous_page = FALSE; } // default, must be overriden by the individual plugins. $this->_is_next_page = FALSE; $offset = 0; if ($this->_items_per_page > 0 && $this->_page_no > 1) { $offset = $this->_items_per_page * ($HTTP_VARS['page_no'] - 1) + 1; } $this->_total_count = 0; // if its not a legal type, don't pass it on if (!is_exists_item_type($HTTP_VARS['s_item_type'])) { unset($HTTP_VARS['s_item_type']); } $this->_http_vars = $HTTP_VARS; // do not want to pass page_no through $this->_http_vars['page_no'] = NULL; // now at this point we need to grab the $items_per_page and // page_no values. if ($this->queryListing($this->_page_no, $this->_items_per_page, $offset, $HTTP_VARS['s_item_type'], $input_field_values_r)) { // no need for this anymore $this->_http_vars = NULL; // if a single item returned, we will populate the itemData at this point too if ($this->getRowCount() == 1 && $this->isPreviousPage() === FALSE) { $rowData = $this->getRowData(0); $this->_item_data_r = NULL; // call the queryItem function directly here. $return_val = $this->_queryItem($rowData['attributes'], $HTTP_VARS['s_item_type']); if ($return_val) { return TRUE; } else { return FALSE; } } else { // in some cases, the plugin will not be able to provide this // information, but where it can, we can derive the is_next_page flag, // other plugins, would have to call setNextPage() function instead. if ($this->_items_per_page > 0 && $this->_total_count > $offset + $this->_items_per_page) { $this->_is_next_page = TRUE; } // expect the call to querySite to set the is_next_page flag return TRUE; } } else { // no need for this anymore $this->_http_vars = NULL; return FALSE; } } else { // if($at_least_one_search_field_populated) // nothing found. return TRUE; } }
function queryItem($search_attributes_r, $s_item_type) { // assumes we have an exact match here $pageBuffer = $this->fetchURI("http://" . $this->url . "/gp/product/" . $search_attributes_r[$this->asinId]); // no sense going any further here. if (strlen($pageBuffer) == 0) { return FALSE; } $pageBuffer = preg_replace('/[\\r\\n]+/', ' ', $pageBuffer); $pageBuffer = preg_replace('/>[\\s]*</', '><', $pageBuffer); //<span id="btAsinTitle">Prometheus (Blu-ray/ DVD + Digital Copy) (2012)</span> //<span id="btAsinTitle" style="">Homeland: The Dark Elf Trilogy, Part 1 (Forgotten Realms: The Legend of Drizzt, Book I) (Bk. 1) <span style="text-transform:capitalize; font-size: 16px;">[Mass Market Paperback]</... //<h1 class="a-size-large a-spacing-none" id="title"> Illustration School: Let's Draw Happy People <span class="a-size-medium a-color-secondary a-text-normal">Hardcover</span></h1> //<span id="productTitle" class="a-size-large">Men in Black 3 [Blu-ray]</span> if (preg_match("/<span id=\"btAsinTitle\"[^>]*>([^<]+)<\\/?span/s", $pageBuffer, $regs) || preg_match("/<span id=\"productTitle\"[^>]*?>([^<]+)<\\/span/s", $pageBuffer, $regs) || preg_match("/<h[^>]*?id=\"title\"[^>]*>([^<]+)</", $pageBuffer, $regs) || preg_match("/<b class=\"sans\">([^<]+)<\\/b>/s", $pageBuffer, $regs) || preg_match("/<b class=\"sans\">([^<]+)<!--/s", $pageBuffer, $regs)) { $title = trim($regs[1]); // If extra year appended, remove it and just get the title. if (preg_match("/(.*)\\([0-9]+\\)\$/", $title, $regs2)) { $title = $regs2[1]; } $title = trim(str_replace("\"", "", $title)); if (($idx = strpos($title, '(Blu-ray')) !== FALSE) { $title = substr($title, 0, $idx); } else { if (($idx = strpos($title, '[Blu-ray')) !== FALSE) { $title = substr($title, 0, $idx); } } $this->addItemAttribute('title', $title); //Amazon.com: DVD: First Blood (Special Edition) (1982) // Need to escape any (, ), [, ], :, ., if (preg_match("/" . preg_quote($this->getItemAttribute('title'), "/") . " \\(([0-9]*)\\)/s", $pageBuffer, $regs)) { $this->addItemAttribute('year', $regs[1]); } } // a hack! $upcId = get_upc_code($search_attributes_r['search.title']); if ($upcId && $upcId != $this->getItemAttribute('title')) { $this->addItemAttribute('upc_id', $upcId); } // ** Front Cover Image ** if (preg_match("!<img id=\"main-image\" src=\"([^\"]+)\"!s", $pageBuffer, $regs)) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $regs[1]); $this->addItemAttribute('imageurl', $image); } else { if (preg_match("!registerImage\\(\"original_image[^\"]*\", \"([^\"]+)\"!", $pageBuffer, $regs)) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $regs[1]); $this->addItemAttribute('imageurl', $image); } else { if (preg_match("!<img id=\"landingImage\".*?src=\"([^\"]+)\"!s", $pageBuffer, $regs)) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $regs[1]); $this->addItemAttribute('imageurl', $image); } else { if (preg_match("!<img [^>]*?id=\"imgBlkFront\" [^>]*?src=\"([^\"]+)\"!s", $pageBuffer, $regs) || preg_match("!<img [^>]*?src=\"([^\"]+)\" [^>]*?id=\"imgBlkFront\"!s", $pageBuffer, $regs)) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $regs[1]); $this->addItemAttribute('imageurl', $image); } } } } // ** Back Cover Image ** if (preg_match("!<img [^>]*?id=\"imgBlkBack\" [^>]*?src=\"([^\"]+)\"!", $pageBuffer, $regs) || preg_match("!<img [^>]*?src=\"([^\"]+)\" [^>]*?id=\"imgBlkBack\"!", $pageBuffer, $regs)) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $regs[1]); $this->addItemAttribute('imageurlb', $image); } if (preg_match_all("!registerImage\\(\"cust_image[^\"]*\", \"([^\"]+)\"!", $pageBuffer, $regs)) { while (list(, $image) = each($regs[1])) { // remove image extras _xxx_. $image = preg_replace('!(\\/[^.]+\\.)_[^.]+_\\.!', "\$1", $image); $this->addItemAttribute('cust_imageurl', $image); } } //http://www.amazon.com/gp/product/product-description/0007136587/ref=dp_proddesc_0/002-1041562-0884857?ie=UTF8&n=283155&s=books if (preg_match("!<a href=\"http://" . $this->url . "/gp/product/product-description/" . $search_attributes_r[$this->asinId] . "/[^>]*>See all Editorial Reviews</a>!", $pageBuffer, $regs) || preg_match("!<a href=\"http://" . $this->url . "/gp/product/product-description/" . $search_attributes_r[$this->asinId] . "/[^>]*>See all Reviews</a>!", $pageBuffer, $regs)) { $reviewPage = $this->fetchURI("http://" . $this->url . "/gp/product/product-description/" . $search_attributes_r[$this->asinId] . "/reviews/"); if (strlen($reviewPage) > 0) { $reviews = parse_amazon_reviews($reviewPage); if (is_not_empty_array($reviews)) { $this->addItemAttribute('blurb', $reviews); } } } else { $reviews = parse_amazon_reviews($pageBuffer); if (is_not_empty_array($reviews)) { $this->addItemAttribute('blurb', $reviews); } } if (preg_match("/<span class=listprice>\\\$([^<]*)<\\/span>/i", $pageBuffer, $regs)) { $this->addItemAttribute('listprice', $regs[1]); } else { if (preg_match("/<td class=\"listprice\">\\\$([^<]*)<\\/td>/i", $pageBuffer, $regs)) { $this->addItemAttribute('listprice', $regs[1]); } else { if (preg_match("!>List Price:</[^\\\$]+\\\$([0-9\\.]+)!m", $pageBuffer, $regs)) { $this->addItemAttribute('listprice', $regs[1]); } } } // amazon price value: <b class="priceLarge">$7.99</b> if (preg_match("!<b class=\"(priceLarge|price)\">\\\$([^<]*)</b>!i", $pageBuffer, $regs)) { $this->addItemAttribute('listprice', $regs[2]); $this->addItemAttribute('price', $regs[2]); } //http://g-ec2.images-amazon.com/images/G/01/x-locale/common/customer-reviews/stars-4-0._V47081936_.gif if (preg_match("!<li><b>Average Customer Review:</b>[\\s]*<img src=\".*?/stars-([^\\.]+).!i", $pageBuffer, $regs)) { $amazonreview = str_replace('-', '.', $regs[1]); $this->addItemAttribute('amznrating', $amazonreview); $this->addItemAttribute('amazon_review', $amazonreview); } if (($startIndex = strpos($pageBuffer, "<h2>Look for Similar Items by Subject</h2>")) !== FALSE && ($endIndex = strpos($pageBuffer, "matching ALL checked", $startIndex)) !== FALSE) { $subjectform = substr($pageBuffer, $startIndex, $endIndex - $startIndex); if (preg_match_all("!<input type=\"checkbox\" name=\"field\\+keywords\" value=\"([^\"]+)\"!", $subjectform, $matches)) { $this->addItemAttribute('genre', $matches[1]); } } // Get the mapped AMAZON index type $index_type = ifempty($this->getConfigValue('item_type_to_index_map', $s_item_type), strtolower($s_item_type)); switch ($index_type) { case 'dvd': case 'vhs': $this->parse_amazon_video_data($search_attributes_r, $s_item_type, $pageBuffer); break; case 'videogames': $this->parse_amazon_game_data($search_attributes_r, $pageBuffer); break; case 'books': $this->parse_amazon_books_data($search_attributes_r, $pageBuffer); break; case 'music': $this->parse_amazon_music_data($search_attributes_r, $pageBuffer); break; default: //Not much here, but what else can we do? break; } return TRUE; }