| 91 |
- |
1 |
<?php
|
|
|
2 |
error_reporting(E_ALL);
|
|
|
3 |
|
|
|
4 |
// Get itunes listings
|
|
|
5 |
function get_amazon_scrape($query, $searchCondition) {
|
| 95 |
- |
6 |
$vendors = Vendors::getInstance();
|
|
|
7 |
$config = $vendors->getVendor(Vendors::AMAZON);
|
|
|
8 |
$numListings = $config['numListings'];
|
|
|
9 |
|
|
|
10 |
$needMatches = empty($_SESSION["discogs"]);
|
|
|
11 |
if ($needMatches) {
|
|
|
12 |
$_SESSION["discogs"] = startMatches();
|
|
|
13 |
}
|
|
|
14 |
|
| 91 |
- |
15 |
$arr = [];
|
|
|
16 |
$products = [];
|
| 95 |
- |
17 |
$cnt = 0;
|
| 91 |
- |
18 |
|
|
|
19 |
libxml_use_internal_errors(true);
|
|
|
20 |
$html = getUrl("https://www.amazon.com/s?k=" . rawurlencode($query) . "&sf=qz&unfiltered=1&ref=nb_sb_noss");
|
|
|
21 |
$dom = new DOMDocument;
|
|
|
22 |
$dom->loadHTML($html);
|
|
|
23 |
$xpath = new DOMXPath($dom);
|
|
|
24 |
$nodes = $xpath->query('//a/@href');
|
|
|
25 |
foreach($nodes as $href) {
|
|
|
26 |
if (strpos($href->nodeValue, "/gp/offer-listing/B") === 0) {
|
|
|
27 |
$products[] = $href->nodeValue;
|
|
|
28 |
}
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
foreach($products as $product) {
|
|
|
32 |
$url = "https://www.amazon.com" . $product . "&tag=uj024-20&language=en_US";
|
|
|
33 |
$asin = explode('/', $product)[3];
|
|
|
34 |
|
|
|
35 |
$html = getUrl("https://www.amazon.com/dp/" . $asin, "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/70.0");
|
|
|
36 |
|
|
|
37 |
$dom = new DOMDocument;
|
|
|
38 |
$dom->loadHTML($html);
|
| 95 |
- |
39 |
$xpathPrd = new DOMXPath($dom);
|
| 91 |
- |
40 |
|
| 95 |
- |
41 |
$nodes = $xpathPrd->query('//table[@id="productDetailsTable"]//ul/li');
|
|
|
42 |
if ($nodes->length < 1) {
|
| 91 |
- |
43 |
continue;
|
|
|
44 |
}
|
| 95 |
- |
45 |
|
|
|
46 |
$format = "";
|
| 91 |
- |
47 |
foreach($nodes as $node) {
|
|
|
48 |
$str = trim($node->nodeValue);
|
| 95 |
- |
49 |
|
|
|
50 |
if (strpos($str, "Audio CD") === 0 ||
|
|
|
51 |
strpos($str, "Vinyl") === 0 ||
|
|
|
52 |
strpos($str, "Sheet") === 0 ||
|
|
|
53 |
strpos($str, "Hardcover") === 0 ||
|
|
|
54 |
strpos($str, "Paperback") === 0) {
|
|
|
55 |
$p = strpos($str, " (");
|
|
|
56 |
$format = ($p > 0 ? substr($str, 0, $p) : $str);
|
|
|
57 |
$releaseDate = ($p > 0 ? substr($str, $p+2, strlen($str) - $p - 3) : "");
|
|
|
58 |
}
|
|
|
59 |
|
| 91 |
- |
60 |
if (strpos($str, "Amazon Best Sellers Rank:") === 0) {
|
|
|
61 |
$p = strpos($str, " (");
|
|
|
62 |
$rank = substr($str, 11, $p-11);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (strpos($format, "Audio CD") === 0 ||
|
|
|
67 |
strpos($format, "Vinyl") === 0 ||
|
| 95 |
- |
68 |
strpos($str, "Sheet") === 0 ||
|
| 91 |
- |
69 |
strpos($format, "Hardcover") === 0 ||
|
|
|
70 |
strpos($format, "Paperback") === 0) {
|
|
|
71 |
if (strpos($format, "Audio CD") !== false) {
|
|
|
72 |
$mediaType = "CD";
|
|
|
73 |
} else if (strpos($format, "Vinyl") !== false) {
|
|
|
74 |
$mediaType = "Record";
|
|
|
75 |
} else if (strpos($format, "Paperback") !== false ||
|
|
|
76 |
strpos($format, "Sheet") !== false ||
|
|
|
77 |
strpos($format, "Hardcover") !== false) {
|
|
|
78 |
$mediaType = "Book";
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$str = "https://www.amazon.com" . $product;
|
|
|
82 |
$html = getUrl($str, "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/70.0");
|
|
|
83 |
|
|
|
84 |
$dom = new DOMDocument;
|
|
|
85 |
$dom->loadHTML($html);
|
|
|
86 |
$xpath = new DOMXPath($dom);
|
|
|
87 |
|
|
|
88 |
$nodes = $xpath->query('//div[@id="olpProductImage"]//img');
|
| 97 |
- |
89 |
$pic = "";
|
|
|
90 |
if ($nodes->length > 0) {
|
|
|
91 |
$pic = $nodes->item(0)->getAttribute("src");
|
|
|
92 |
}
|
|
|
93 |
|
| 91 |
- |
94 |
$nodes = $xpath->query('//div[@id="olpProductDetails"]/h1');
|
|
|
95 |
$title = trim($nodes->item(0)->nodeValue);
|
| 95 |
- |
96 |
$fullTitle = $title;
|
| 91 |
- |
97 |
|
|
|
98 |
$nodes = $xpath->query('//div[@id="olpProductByline"]');
|
|
|
99 |
if ($nodes->length > 0) {
|
|
|
100 |
$artists = trim($nodes->item(0)->nodeValue);
|
| 95 |
- |
101 |
$artists = str_replace(" (Artist)", "", $artists);
|
|
|
102 |
if (strpos($artists, "~ ") === 0) {
|
|
|
103 |
$artists = substr($artists, 2);
|
|
|
104 |
}
|
|
|
105 |
$fullTitle = $title . " by " . $artists;
|
| 91 |
- |
106 |
}
|
|
|
107 |
|
| 95 |
- |
108 |
if (strpos($format, "Audio CD") === 0 ||
|
|
|
109 |
strpos($format, "Vinyl") === 0) {
|
|
|
110 |
if ($needMatches) {
|
|
|
111 |
addMatch_scrape($xpathPrd, ++$cnt, $title, $artists, $format, $releaseDate, $asin, $url, $pic);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
| 91 |
- |
115 |
$listings = $xpath->query('//div[contains(concat(" ", normalize-space(@class), " "), " olpOffer ")]');
|
|
|
116 |
|
| 95 |
- |
117 |
$listingCnt = 0;
|
| 91 |
- |
118 |
foreach($listings as $listing) {
|
|
|
119 |
$nodes = $xpath->query('.//h3[contains(concat(" ", normalize-space(@class), " "), " olpSellerName ")]', $listing);
|
|
|
120 |
$str = trim($nodes->item(0)->nodeValue);
|
|
|
121 |
$sellerName = (empty($str) ? "Amazon" : $str);
|
|
|
122 |
$merchantName = "Amazon";
|
|
|
123 |
$feedbackPercent = -1;
|
|
|
124 |
$feedbackScore = -1;
|
|
|
125 |
|
|
|
126 |
if ($sellerName != "Amazon") {
|
|
|
127 |
$merchantName .= " Marketplace";
|
|
|
128 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpSellerColumn ")]//p', $listing);
|
|
|
129 |
if ($nodes->length > 0) {
|
|
|
130 |
$str = trim($nodes->item(0)->nodeValue);
|
|
|
131 |
$sellerrating = substr($str, 17);
|
|
|
132 |
$num = preg_match_all('/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/', $sellerrating, $matches);
|
|
|
133 |
if ($num == 3) {
|
|
|
134 |
$feedbackPercent = (int)$matches[0][0];
|
|
|
135 |
$feedbackScore = (int)str_replace( ',', '', $matches[0][2]);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
$nodes = $xpath->query('.//span[contains(concat(" ", normalize-space(@class), " "), " olpCondition ")]', $listing);
|
|
|
141 |
$str = trim($nodes->item(0)->nodeValue);
|
|
|
142 |
$pos = strpos($str, " - ");
|
|
|
143 |
if ($pos !== false) {
|
|
|
144 |
$condition = trim(substr($str, 0, $pos));
|
|
|
145 |
$detailCondition = trim(substr($str, $pos+3));
|
|
|
146 |
} else {
|
|
|
147 |
$condition = $str;
|
|
|
148 |
$detailCondition = $str;
|
|
|
149 |
}
|
|
|
150 |
if ($condition == "Collectible" || $condition == "Refurbished") {
|
|
|
151 |
$condition = 'Used';
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpConditionColumn ")]//div[contains(concat(" ", normalize-space(@class), " "), " comments ")]', $listing);
|
|
|
155 |
if ($nodes->length > 0) {
|
|
|
156 |
$conditionComment = trim($nodes->item(0)->nodeValue);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//span[contains(concat(" ", normalize-space(@class), " "), " olpOfferPrice ")]', $listing);
|
|
|
160 |
$price = substr(trim($nodes->item(0)->nodeValue), 1);
|
|
|
161 |
$currency = 'USD';
|
|
|
162 |
|
|
|
163 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//span[contains(concat(" ", normalize-space(@class), " "), " olpShippingPrice ")]', $listing);
|
|
|
164 |
if ($nodes->length > 0) {
|
|
|
165 |
$shippingCost = substr(trim($nodes->item(0)->nodeValue), 1);
|
|
|
166 |
$shippingCurrency = 'USD';
|
|
|
167 |
$freeShippingCap = 0;
|
|
|
168 |
} else {
|
|
|
169 |
$shippingCost = 0.00;
|
|
|
170 |
$shippingCurrency = 'USD';
|
|
|
171 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//p[contains(concat(" ", normalize-space(@class), " "), " olpShippingInfo ")]', $listing);
|
|
|
172 |
$str= trim($nodes->item(0)->nodeValue);
|
|
|
173 |
if (strpos($str, "FREE Shipping") !== false) {
|
|
|
174 |
$freeShippingCap = 0.00;
|
|
|
175 |
}
|
|
|
176 |
if (strpos($str, "on orders over") !== false) {
|
|
|
177 |
$freeShippingCap = 25.00;
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$country = 'US';
|
|
|
182 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpDeliveryColumn ")]//ul/li', $listing);
|
|
|
183 |
foreach($nodes as $node) {
|
|
|
184 |
$str = trim($node->nodeValue);
|
|
|
185 |
if (strpos($str, "Ships from") === 0) {
|
|
|
186 |
$p = strpos($str, ".");
|
|
|
187 |
$country = getCountryCode(substr($str, 11, $p-11));
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
$nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//i[contains(concat(" ", normalize-space(@class), " "), " a-icon-prime ")]', $listing);
|
|
|
192 |
if ($nodes->length > 0) {
|
|
|
193 |
$sellerName .= " Prime";
|
|
|
194 |
}
|
|
|
195 |
|
| 95 |
- |
196 |
if (++$listingCnt > $numListings) {
|
|
|
197 |
continue;
|
|
|
198 |
}
|
|
|
199 |
|
| 91 |
- |
200 |
$arr[] = array(
|
|
|
201 |
"Merchant" => $merchantName,
|
|
|
202 |
"Condition" => $condition,
|
| 95 |
- |
203 |
"Title" => $fullTitle,
|
| 91 |
- |
204 |
"Barcode" => "",
|
|
|
205 |
"BarcodeType" => "",
|
|
|
206 |
"Image" => $pic,
|
|
|
207 |
"URL" => $url,
|
|
|
208 |
"MediaType" => $mediaType,
|
|
|
209 |
"DetailCondition" => $detailCondition,
|
|
|
210 |
"Country" => $country,
|
|
|
211 |
"BestOffer" => false,
|
|
|
212 |
"TimeLeft" => 0,
|
|
|
213 |
"Price" => $price,
|
|
|
214 |
"Currency" => $currency,
|
|
|
215 |
"ListingType" => "Fixed",
|
|
|
216 |
"Location" => "US",
|
|
|
217 |
"Zip" => "",
|
|
|
218 |
"FeedbackScore" => $feedbackScore,
|
|
|
219 |
"FeedbackPercent" => $feedbackPercent,
|
|
|
220 |
"SellerName" => $sellerName,
|
|
|
221 |
"HandlingTime" => 1,
|
|
|
222 |
"ShippingCost" => $shippingCost,
|
|
|
223 |
"ShippingEstimated" => false,
|
|
|
224 |
"ShippingCurrency" => $shippingCurrency,
|
|
|
225 |
"FreeShippingCap" => $freeShippingCap,
|
|
|
226 |
"Show" => true
|
|
|
227 |
);
|
|
|
228 |
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
}
|
|
|
233 |
|
| 95 |
- |
234 |
if ($needMatches) {
|
|
|
235 |
if ($cnt = 0) {
|
|
|
236 |
$_SESSION["discogs"] = "";
|
|
|
237 |
} else {
|
|
|
238 |
$_SESSION["discogs"] .= endMatches();
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
|
| 91 |
- |
242 |
return ($arr);
|
| 93 |
- |
243 |
}
|
|
|
244 |
|
| 95 |
- |
245 |
function addMatch_scrape($xpath, $cnt, $title, $artists, $mediaType, $releaseDate, $asin, $url, $pic) {
|
|
|
246 |
$nodes = $xpath->query('//table[@id="productDetailsTable"]//ul/li');
|
|
|
247 |
if ($nodes->length < 1) {
|
|
|
248 |
return;
|
|
|
249 |
}
|
| 93 |
- |
250 |
|
| 95 |
- |
251 |
$runTime = "";
|
|
|
252 |
$noDiscs = "";
|
|
|
253 |
$label = "";
|
|
|
254 |
$edition = "";
|
|
|
255 |
$genre = "";
|
|
|
256 |
|
|
|
257 |
foreach($nodes as $node) {
|
|
|
258 |
$str = trim($node->nodeValue);
|
|
|
259 |
|
|
|
260 |
$p = strpos($str, "Run Time:");
|
|
|
261 |
if ($p === 0) {
|
|
|
262 |
$runTime = substr($str, 10);
|
| 93 |
- |
263 |
}
|
| 95 |
- |
264 |
|
|
|
265 |
$p = strpos($str, "Number of Discs:");
|
|
|
266 |
if ($p === 0) {
|
|
|
267 |
$noDiscs = substr($str, 17);
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
$p = strpos($str, "Label:");
|
|
|
271 |
if ($p === 0) {
|
|
|
272 |
$label = substr($str, 7);
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
$p = strpos($str, "Edition:");
|
|
|
276 |
if ($p === 0) {
|
|
|
277 |
$edition = substr($str, 9);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
$p = strpos($str, "SPARS Code:");
|
|
|
281 |
if ($p === 0) {
|
|
|
282 |
$edition = (strlen($edition) > 0 ? ", " : "") . substr($str, 12);
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
$p = strpos($str, "Format:");
|
|
|
286 |
if ($p === 0) {
|
|
|
287 |
$edition = (strlen($edition) > 0 ? ", " : "") . substr($str, 8);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
$p = strpos($str, "Performer:");
|
|
|
291 |
if ($p === 0) {
|
|
|
292 |
$artists = substr($str, 11);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
$p = strpos($str, "Original Release Date:");
|
|
|
296 |
if ($p === 0) {
|
|
|
297 |
$releaseDate = substr($str, 23);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
if (strpos($str, "Amazon Best Sellers Rank:") === 0) {
|
|
|
301 |
$pieces = explode("\n", $str);
|
|
|
302 |
$genres = [];
|
|
|
303 |
foreach($pieces as $piece) {
|
|
|
304 |
$piece = trim($piece);
|
|
|
305 |
$p1 = strpos($piece, "in ");
|
|
|
306 |
$p2 = strpos($piece, " (CDs & Vinyl)") ;
|
|
|
307 |
if ($p1 === 0 && $p2 > 0) {
|
|
|
308 |
$genres[] = substr($piece, 4, $p2 - 4);
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
$genre = join(", ", $genres);
|
|
|
312 |
}
|
| 93 |
- |
313 |
}
|
| 95 |
- |
314 |
|
|
|
315 |
$item = new SimpleXMLElement("<item></item>");
|
|
|
316 |
$item->addChild('ASIN', $asin);
|
|
|
317 |
$item->addChild('DetailPageURL', $url);
|
|
|
318 |
$item->addChild('MediumImage');
|
|
|
319 |
$item->MediumImage->addChild('URL', $pic);
|
|
|
320 |
|
|
|
321 |
$nodes = $xpath->query('//table[@id="dmusic_tracklist_content"]//div[contains(concat(" ", normalize-space(@class), " "), " a-section ")]//a[contains(concat(" ", normalize-space(@class), " "), " TitleLink ")]');
|
|
|
322 |
if ($nodes->length > 0) {
|
|
|
323 |
$item->addChild('Tracks');
|
|
|
324 |
$item->Tracks->addChild('Disc', '1');
|
|
|
325 |
|
|
|
326 |
foreach($nodes as $node) {
|
|
|
327 |
$line = trim(preg_replace("/[\n\r]/","", $node->nodeValue));
|
|
|
328 |
$item->Tracks->Disc->addChild('Track', $line);
|
|
|
329 |
}
|
|
|
330 |
} else {
|
|
|
331 |
$nodes = $xpath->query('//div[@id="dmusic_tracklist_player"]//div[contains(concat(" ", normalize-space(@class), " "), " a-row ")]');
|
|
|
332 |
if ($nodes->length > 0) {
|
|
|
333 |
$item->addChild('Tracks');
|
|
|
334 |
$item->Tracks->addChild('Disc', '1');
|
|
|
335 |
|
|
|
336 |
foreach($nodes as $node) {
|
|
|
337 |
$line = trim($node->nodeValue);
|
|
|
338 |
if ($noDiscs == 1 && strpos($line, "Disc") === 0) {
|
|
|
339 |
continue;
|
|
|
340 |
}
|
|
|
341 |
$line = trim(preg_replace("/[\n\r]/","", $line));
|
|
|
342 |
if (bin2hex(substr($line, 0, 2)) == "c2a0") {
|
|
|
343 |
$line = trim(substr($line, 2));
|
|
|
344 |
}
|
|
|
345 |
$item->Tracks->Disc->addChild('Track', $line);
|
|
|
346 |
}
|
|
|
347 |
}
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
$item->addChild('ItemAttributes');
|
|
|
351 |
$item->ItemAttributes->addChild('Title', $title);
|
|
|
352 |
$item->ItemAttributes->addChild('Artist', $artists);
|
|
|
353 |
$item->ItemAttributes->addChild('Edition', $edition);
|
|
|
354 |
$item->ItemAttributes->addChild('Genre', $genre);
|
|
|
355 |
$item->ItemAttributes->addChild('Label', $label);
|
|
|
356 |
$item->ItemAttributes->addChild('MediaType', $mediaType);
|
|
|
357 |
$item->ItemAttributes->addChild('NumberOfDiscs', $noDiscs);
|
|
|
358 |
$item->ItemAttributes->addChild('ReleaseDate', $releaseDate);
|
|
|
359 |
$item->ItemAttributes->addChild('RunningTime', (int)$runTime);
|
|
|
360 |
|
|
|
361 |
$_SESSION["discogs"] .= addMatch($item, $cnt, $mediaType);
|
|
|
362 |
}
|