summaryrefslogtreecommitdiffstats
path: root/modules/amazon/AmazonSearchEngine.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/amazon/AmazonSearchEngine.class.php')
-rw-r--r--modules/amazon/AmazonSearchEngine.class.php189
1 files changed, 189 insertions, 0 deletions
diff --git a/modules/amazon/AmazonSearchEngine.class.php b/modules/amazon/AmazonSearchEngine.class.php
new file mode 100644
index 00000000..6a6f7b73
--- /dev/null
+++ b/modules/amazon/AmazonSearchEngine.class.php
@@ -0,0 +1,189 @@
+<?php
+/*
+
+ Copyright (c) 2001 - 2005 Ampache.org
+ All rights reserved.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+*/
+
+/*!
+ @header AmazonSearch Class
+ @discussion This class takes a token (amazon ID)
+ and then allows you to do a search using the REST
+ method. Currently it is semi-hardcoded to do music
+ searches and only return information abou the album
+ art
+*/
+class AmazonSearch {
+
+ var $base_url = "http://webservices.amazon.com/onca/xml?";
+ var $search;
+ var $token;
+ var $results=array(); // Array of results
+ var $_parser; // The XML parser
+ var $_grabtags; // Tags to grab the contents of
+ var $_sourceTag; // source tag don't ask
+ var $_subTag; // Stupid hack to make things come our right
+ var $_currentTag; // Stupid hack to make things come out right
+ var $_currentTagContents;
+
+ function AmazonSearch($token, $associates_id = 'none') {
+
+ $this->token = $token;
+ $this->associates_id = $associates_id;
+
+ $this->_grabtags = array(
+ 'ASIN', 'ProductName', 'Catalog', 'ErrorMsg',
+ 'Description', 'ReleaseDate', 'Manufacturer', 'ImageUrlSmall',
+ 'ImageUrlMedium', 'ImageUrlLarge', 'Author', 'Artist','Title','URL',
+ 'SmallImage','MediumImage','LargeImage');
+
+ } // AmazonSearch
+
+ /*!
+ @create_parser
+ @discussion this sets up an XML Parser
+ */
+ function create_parser() {
+ $this->_parser = xml_parser_create();
+
+ xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
+
+ xml_set_object($this->_parser, $this);
+
+ xml_set_element_handler($this->_parser, 'start_element', 'end_element');
+
+ xml_set_character_data_handler($this->_parser, 'cdata');
+
+ } // create_parser
+
+ /*!
+ @function search
+ @discussion do a full search on the url they pass
+ */
+ function run_search($url) {
+
+ /* Create the Parser */
+ $this->create_parser();
+
+ $snoopy = new Snoopy;
+ $snoopy->fetch($url);
+ $contents = $snoopy->results;
+
+
+ if (!xml_parse($this->_parser, $contents)) {
+ die(sprintf('XML error: %s at line %d',xml_error_string(xml_get_error_code($this->_parser)),xml_get_current_line_number($this->_parser)));
+ }
+
+ xml_parser_free($this->_parser);
+
+ } // search
+
+ /*!
+ @function search
+ @discussion takes terms and a type
+ */
+ function search($terms, $type='Music') {
+
+ $url = $this->base_url . "Service=AWSECommerceService&SubscriptionId=" . $this->token .
+ "&Operation=ItemSearch&Artist=" . urlencode($terms['artist']) . "&Title=" . urlencode($terms['album']) .
+ "&Keywords=" . urlencode($terms['keywords']) . "&SearchIndex=" . $type;
+
+ $this->run_search($url);
+
+ unset($this->results['ASIN']);
+
+ } // search
+
+ /*!
+ @function lookup
+ @discussion this takes a ASIN and looks up the
+ item in question, possible to pass array
+ of asin's
+ */
+ function lookup($asin, $type='Music') {
+
+ if (is_array($asin)) {
+ foreach ($asin as $key=>$value) {
+ $url = $this->base_url . "Service=AWSECommerceService&SubscriptionId=" . $this->token .
+ "&Operation=ItemLookup&ItemId=" . $key . "&ResponseGroup=Images";
+ $this->run_search($url);
+ }
+ } // if array of asin's
+ else {
+ $url = $this->base_url . "Service=AWSECommerceService&SubscriptionId=" . $this->token .
+ "&Operation=ItemLookup&ItemId=" . $asin . "&ResponseGroup=Images";
+ $this->run_search($url);
+ } // else
+
+ unset($this->results['ASIN']);
+
+ } // lookup
+
+ function start_element($parser, $tag, $attributes) {
+
+ if ($tag == "ASIN") {
+ $this->_sourceTag = $tag;
+ }
+ if ($tag == "SmallImage" || $tag == "MediumImage" || $tag == "LargeImage") {
+ $this->_subTag = $tag;
+ }
+
+ /* If it's in the tag list, don't grab our search results though */
+ if (strlen($this->_sourceTag)) {
+ $this->_currentTag = $tag;
+ }
+ else {
+ $this->_currentTag = '';
+ }
+
+
+ } // start_element
+
+ function cdata($parser, $cdata) {
+
+ $tag = $this->_currentTag;
+ $subtag = $this->_subTag;
+ $source = $this->_sourceTag;
+
+ switch ($tag) {
+ case 'URL':
+ $this->results[$source][$subtag] = trim($cdata);
+ break;
+ case 'ASIN':
+ $this->_sourceTag = trim($cdata);
+ break;
+ default:
+ if (strlen($tag)) {
+ $this->results[$source][$tag] = trim($cdata);
+ }
+ break;
+ } // end switch
+
+
+ } // cdata
+
+ function end_element($parser, $tag) {
+
+ /* Zero the tag */
+ $this->_currentTag = '';
+
+ } // end_element
+
+} // end AmazonSearch
+
+?>