/[drupal]/contributions/modules/amazon_items/AmazonSearchEngine.class.inc
ViewVC logotype

Contents of /contributions/modules/amazon_items/AmazonSearchEngine.class.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Wed Aug 3 04:11:52 2005 UTC (4 years, 3 months ago) by nsk
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-4-6
Changes since 1.2: +4 -1 lines
File MIME type: text/x-php
Thanks Matthew
1 <?php
2 // $Id: AmazonSearchEngine.class.inc,v 1.2 2005/08/02 20:10:35 nsk Exp $
3
4 /*
5 This file is free for anyone to use or modify as they wish.
6 Author: Simon Willison, July 2002
7 See http://scripts.incutio.com/
8 */
9
10 class AmazonSearchEngine {
11 var $server = 'xml.amazon.com';
12 var $server_xml_url;
13 var $search;
14 var $dev_token;
15 var $associates_id;
16 var $type = 'lite';
17 var $version;
18 var $results; // Array of results
19 var $xml_format;
20 var $xml_content;
21 var $totalResults = 0;
22 var $_parser; // The XML parser
23 var $_currentResult;
24 var $_grabtags; // Tags to grab the contents of
25 var $_currentTagContents;
26
27 function AmazonSearchEngine($dev_token, $associates_id = 'none', $version = '1.0', $atype = 'lite', $f = 'xml') {
28 $this->dev_token = $dev_token;
29 $this->associates_id = $associates_id;
30
31 $this->version = $version;
32 if ($this->version == '1.0') {
33 $this->server_xml_url = 'onca/xml';
34 } else if ($this->version == '2.0') {
35 $this->server_xml_url = 'onca/xml2';
36 }
37 else if ($this->version == "3.0") {
38 $this->server_xml_url = "onca/xml3";
39 }
40
41 $this->type = $atype;
42 $this->xml_format = $f;
43
44 $this->results = array();
45 $this->_grabtags = array(
46 'Asin', 'ProductName', 'Catalog', 'ErrorMsg',
47 'Description', 'ReleaseDate', 'Manufacturer', 'ImageUrlSmall',
48 'ImageUrlMedium', 'ImageUrlLarge', 'ListPrice', 'OurPrice',
49 'UsedPrice', 'Author', 'Artist', 'Rating', 'Summary', 'Comment',
50 'Product', 'Feature', 'TotalResults', 'Track', 'ByArtist'
51 );
52 }
53
54 function doFetch($url) {
55 $fp = fsockopen($this->server, 80);
56 fputs($fp, "GET " . $url . " HTTP/1.0\n\n");
57 $body = false;
58 $xml = '';
59 while(($line = fgets($fp, 1024))) {
60 $body = $body || trim($line) == "";
61 if($body) {
62 $xml .= $line;
63 }
64 }
65 fclose($fp);
66 return trim($xml);
67 }
68
69 function doSearch($url) {
70 if(file_exists('includes/Snoopy.class.inc')) {
71 $s = new Snoopy;
72 $s->fetch($url);
73 $contents = $s->results;
74 } else {
75 $contents = $this->doFetch($url);
76 }
77 $this->xml_content = $contents;
78 if ($this->xml_format == 'xml') {
79 $this->_parser = xml_parser_create('UTF-8');
80 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
81 xml_set_object($this->_parser, $this);
82 xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
83 xml_set_character_data_handler($this->_parser, 'cdata');
84 if (!xml_parse($this->_parser, $contents)) {
85 die(sprintf('XML error: %s at line %d',
86 xml_error_string(xml_get_error_code($this->_parser)),
87 xml_get_current_line_number($this->_parser)));
88 }
89 xml_parser_free($this->_parser);
90 }
91 }
92
93 function getVersionParam() {
94 $version_text = ($this->version == '1.0') ? 'v=1.0&' : '';
95 return $version_text;
96 }
97
98 function getSortParam($rsort = '') {
99 $sort_text = ($rsort == '') ? '' : (($this->version == '2.0') ? "&sort=$rsort" : '');
100 return $sort_text;
101 }
102
103 function getBaseAmazonURL() {
104 $version_text = $this->getVersionParam();
105 $url = "http://{$this->server}/{$this->server_xml_url}?{$version_text}t={$this->associates_id}&dev-t={$this->dev_token}";
106 return $url;
107 }
108
109 function getFinalAmazonURL($url) {
110 return "{$url}&f={$this->xml_format}";
111 }
112
113 function searchTerm($term, $mode = 'books', $page = 1, $rsort = '') {
114 $sort_text = $this->getSortParam($rsort);
115 $url = $this->getBaseAmazonURL();
116 $url .= "&KeywordSearch=".urlencode($term)."&mode=$mode&type={$this->type}&page=$page{$sort_text}";
117 $this->doSearch($this->getFinalAmazonURL($url));
118 }
119
120 function searchISBN($isbn) {
121 $isbns = is_array($isbn) ? strtoupper(implode(",", $isbn)) : strtoupper($isbn);
122 $sort_text = $this->getSortParam($rsort);
123 $url = $this->getBaseAmazonURL();
124 $url .= "&AsinSearch=$isbns";
125 $url .= "&type={$this->type}";
126 $this->doSearch($this->getFinalAmazonURL($url));
127 }
128
129 function searchAuthor($author, $page = 1, $rsort = '') {
130 $sort_text = $this->getSortParam($rsort);
131 $url = $this->getBaseAmazonURL();
132 $url .= "&AuthorSearch=".urlencode($author)."&mode=books";
133 $url .= "&type={$this->type}&page=$page{$sort_text}";
134 $this->doSearch($this->getFinalAmazonURL($url));
135 }
136
137 function searchRelated($asin, $page = 1) {
138 $sort_text = $this->getSortParam($rsort);
139 $url = $this->getBaseAmazonURL();
140 $url .= "&SimilaritySearch=$asin";
141 $url .= "&type={$this->type}&page=$page";
142 $this->doSearch($this->getFinalAmazonURL($url));
143 }
144
145 /* extra functions */
146
147 function browseNode($browsenode, $mode = 'books', $page = 1, $rsort = '') {
148 $sort_text = $this->getSortParam($rsort);
149 $url = $this->getBaseAmazonURL();
150 $url .= "&BrowseNodeSearch=$browsenode&mode=$mode";
151 $url .= "&type={$this->type}&page=$page{$sort_text}";
152 $this->doSearch($this->getFinalAmazonURL($url));
153 }
154
155 function listMania($listid, $page = 1, $rsort = '') {
156 $sort_text = $this->getSortParam($rsort);
157 $url = $this->getBaseAmazonURL();
158 $url .= "&ListManiaSearch=$listid";
159 $url .= "&type={$this->type}";
160 $this->doSearch($this->getFinalAmazonURL($url));
161 }
162
163 function searchAsin($asin, $page = 1) {
164 $asins = is_array($asin)?strtoupper(implode(",", $asin)):strtoupper($asin);
165 $sort_text = $this->getSortParam($rsort);
166 $url = $this->getBaseAmazonURL();
167 $url .= "&AsinSearch=$asins";
168 $url .= "&type={$this->type}";
169 $this->doSearch($this->getFinalAmazonURL($url));
170 }
171
172 function searchUPC($upc, $mode = 'music', $page = 1, $rsort = '') {
173 $upcs = is_array($upc)?implode(",", $upc):$upc;
174 $sort_text = $this->getSortParam($rsort);
175 $url = $this->getBaseAmazonURL();
176 $url .= "&UpcSearch=$upcs&mode=$mode";
177 $url .= "&type={$this->type}";
178 $this->doSearch($this->getFinalAmazonURL($url));
179 }
180
181 function searchWishList($wl, $page = 1, $rsort = '') {
182 if ($this->version == '2.0') {
183 $sort_text = $this->getSortParam($rsort);
184 $url = $this->getBaseAmazonURL();
185 $url .= "WishlistSearch=$wl&type={$this->type}";
186 $this->doSearch($this->getFinalAmazonURL($url));
187 }
188 }
189
190 /* end special functions */
191
192 function tag_open($parser, $tag, $attributes)
193 {
194 $this->_currentTagContents = '';
195 if ($tag == 'Details') {
196 // We've hit a new result
197 $this->_currentResult = new AmazonProduct;
198 $this->_currentResult->url = $attributes['url'];
199 }
200 if (in_array($tag, $this->_grabtags)) {
201 $this->_currentTag = $tag;
202 } else {
203 $this->_currentTag = '';
204 }
205 }
206
207 function cdata($parser, $cdata)
208 {
209 if ($this->_currentTag) {
210 if (!empty($this->_currentTagContents)) {
211 $this->_currentTagContents .= $cdata;
212 } else {
213 $this->_currentTagContents = $cdata;
214 }
215 }
216 }
217
218 function tag_close($parser, $tag)
219 {
220 switch ($tag) {
221 case 'Details':
222 // We've hit the end of the result
223 $this->results[] = $this->_currentResult;
224 break;
225 case 'Author':
226 $this->_currentResult->Authors[] = $this->_currentTagContents;
227 break;
228 case 'Artist':
229 $this->_currentResult->Artists[] = $this->_currentTagContents;
230 break;
231 case 'Rating':
232 $this->_currentResult->ReviewRating[] = $this->_currentTagContents;
233 break;
234 case 'Summary':
235 $this->_currentResult->ReviewSummary[] = $this->_currentTagContents;
236 break;
237 case 'Comment':
238 $this->_currentResult->ReviewComment[] = $this->_currentTagContents;
239 break;
240 case 'Product':
241 $this->_currentResult->SimilarProducts[] = $this->_currentTagContents;
242 break;
243 case 'Feature':
244 $this->_currentResult->Features[] = $this->_currentTagContents;
245 break;
246 case 'TotalResults':
247 $this->totalResults = $this->_currentTagContents;
248 break;
249 case 'Track':
250 $this->_currentResult->Tracks[] = $this->_currentTagContents;
251 break;
252 case 'ByArtist':
253 $this->_currentResult->ByArtist[] = $this->_currentTagContents;
254 break;
255 default:
256 if (in_array($tag, $this->_grabtags)) {
257 $this->_currentResult->$tag = $this->_currentTagContents;
258 }
259 }
260 }
261 }
262 ?>

  ViewVC Help
Powered by ViewVC 1.1.2