Subversion Repositories cheapmusic

Rev

Rev 93 | Rev 97 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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');
89
            $pic = $nodes->item(0)->getAttribute("src");
90
 
91
            $nodes = $xpath->query('//div[@id="olpProductDetails"]/h1');
92
            $title = trim($nodes->item(0)->nodeValue);
95 - 93
            $fullTitle = $title;
91 - 94
 
95
            $nodes = $xpath->query('//div[@id="olpProductByline"]');
96
            if ($nodes->length > 0) {
97
                $artists = trim($nodes->item(0)->nodeValue);
95 - 98
                $artists = str_replace(" (Artist)", "", $artists);
99
                if (strpos($artists, "~ ") === 0) {
100
                    $artists = substr($artists, 2);
101
                }
102
                $fullTitle = $title . " by " . $artists;
91 - 103
            }
104
 
95 - 105
            if (strpos($format, "Audio CD") === 0 ||
106
                strpos($format, "Vinyl") === 0) {
107
                if ($needMatches) {
108
                    addMatch_scrape($xpathPrd, ++$cnt, $title, $artists, $format, $releaseDate, $asin, $url, $pic);
109
                }
110
            }
111
 
91 - 112
            $listings = $xpath->query('//div[contains(concat(" ", normalize-space(@class), " "), " olpOffer ")]');
113
 
95 - 114
            $listingCnt = 0;
91 - 115
            foreach($listings as $listing) {
116
                $nodes = $xpath->query('.//h3[contains(concat(" ", normalize-space(@class), " "), " olpSellerName ")]', $listing);
117
                $str = trim($nodes->item(0)->nodeValue);
118
                $sellerName = (empty($str) ? "Amazon" : $str);
119
                $merchantName = "Amazon";
120
                $feedbackPercent = -1;
121
                $feedbackScore = -1;
122
 
123
                if ($sellerName != "Amazon") {
124
                    $merchantName .= " Marketplace";
125
                    $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpSellerColumn ")]//p', $listing);
126
                    if ($nodes->length > 0) {
127
                        $str = trim($nodes->item(0)->nodeValue);
128
                        $sellerrating = substr($str, 17);
129
                        $num = preg_match_all('/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/', $sellerrating, $matches);
130
                        if ($num == 3) {
131
                            $feedbackPercent = (int)$matches[0][0];
132
                            $feedbackScore = (int)str_replace( ',', '', $matches[0][2]);
133
                        }
134
                    }
135
                }
136
 
137
                $nodes = $xpath->query('.//span[contains(concat(" ", normalize-space(@class), " "), " olpCondition ")]', $listing);
138
                $str = trim($nodes->item(0)->nodeValue);
139
                $pos = strpos($str, " - ");
140
                if ($pos !== false) {
141
                    $condition = trim(substr($str, 0, $pos));
142
                    $detailCondition = trim(substr($str, $pos+3));
143
                } else {
144
                    $condition = $str;
145
                    $detailCondition = $str;
146
                }
147
                if ($condition == "Collectible" || $condition == "Refurbished") {
148
                    $condition = 'Used';
149
                }
150
 
151
                $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpConditionColumn ")]//div[contains(concat(" ", normalize-space(@class), " "), " comments ")]', $listing);
152
                if ($nodes->length > 0) {
153
                    $conditionComment = trim($nodes->item(0)->nodeValue);
154
                }
155
 
156
                $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//span[contains(concat(" ", normalize-space(@class), " "), " olpOfferPrice ")]', $listing);
157
                $price = substr(trim($nodes->item(0)->nodeValue), 1);
158
                $currency = 'USD';
159
 
160
                $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//span[contains(concat(" ", normalize-space(@class), " "), " olpShippingPrice ")]', $listing);
161
                if ($nodes->length > 0) {
162
                    $shippingCost = substr(trim($nodes->item(0)->nodeValue), 1);
163
                    $shippingCurrency = 'USD';
164
                    $freeShippingCap = 0;
165
                } else {
166
                    $shippingCost = 0.00;
167
                    $shippingCurrency = 'USD';
168
                    $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//p[contains(concat(" ", normalize-space(@class), " "), " olpShippingInfo ")]', $listing);
169
                    $str= trim($nodes->item(0)->nodeValue);
170
                    if (strpos($str, "FREE Shipping") !== false) {
171
                        $freeShippingCap = 0.00;
172
                    }
173
                    if (strpos($str, "on orders over") !== false) {
174
                        $freeShippingCap = 25.00;
175
                    }
176
                }
177
 
178
                $country = 'US';
179
                $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpDeliveryColumn ")]//ul/li', $listing);
180
                foreach($nodes as $node) {
181
                    $str = trim($node->nodeValue);
182
                    if (strpos($str, "Ships from") === 0) {
183
                        $p = strpos($str, ".");
184
                        $country = getCountryCode(substr($str, 11, $p-11));
185
                    }
186
                }
187
 
188
                $nodes = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " olpPriceColumn ")]//i[contains(concat(" ", normalize-space(@class), " "), " a-icon-prime ")]', $listing);
189
                if ($nodes->length > 0) {
190
                    $sellerName .= " Prime";
191
                }
192
 
95 - 193
                if (++$listingCnt > $numListings) {
194
                    continue;
195
                }
196
 
91 - 197
                $arr[] = array(
198
                    "Merchant" => $merchantName,
199
                    "Condition" => $condition,
95 - 200
                    "Title" => $fullTitle,
91 - 201
                    "Barcode" => "",
202
                    "BarcodeType" => "",
203
                    "Image" => $pic,
204
                    "URL" => $url,
205
                    "MediaType" => $mediaType,
206
                    "DetailCondition" => $detailCondition,
207
                    "Country" => $country,
208
                    "BestOffer" => false,
209
                    "TimeLeft" => 0,
210
                    "Price" => $price,
211
                    "Currency" => $currency,
212
                    "ListingType" => "Fixed",
213
                    "Location" => "US",
214
                    "Zip" => "",
215
                    "FeedbackScore" => $feedbackScore,
216
                    "FeedbackPercent" => $feedbackPercent,
217
                    "SellerName" => $sellerName,
218
                    "HandlingTime" => 1,
219
                    "ShippingCost" => $shippingCost,
220
                    "ShippingEstimated" => false,
221
                    "ShippingCurrency" => $shippingCurrency,
222
                    "FreeShippingCap" => $freeShippingCap,
223
                    "Show" => true
224
                );
225
 
226
            }
227
        }
228
 
229
    }
230
 
95 - 231
    if ($needMatches) {
232
        if ($cnt = 0) {
233
            $_SESSION["discogs"] = "";
234
        } else {
235
            $_SESSION["discogs"] .= endMatches();
236
        }
237
    }
238
 
91 - 239
    return ($arr);
93 - 240
}
241
 
95 - 242
function addMatch_scrape($xpath, $cnt, $title, $artists, $mediaType, $releaseDate, $asin, $url, $pic) {
243
    $nodes = $xpath->query('//table[@id="productDetailsTable"]//ul/li');
244
    if ($nodes->length < 1) {
245
        return;
246
    }
93 - 247
 
95 - 248
    $runTime = "";
249
    $noDiscs = "";
250
    $label = "";
251
    $edition = "";
252
    $genre = "";
253
 
254
    foreach($nodes as $node) {
255
        $str = trim($node->nodeValue);
256
 
257
        $p = strpos($str, "Run Time:");
258
        if ($p === 0) {
259
            $runTime = substr($str, 10);
93 - 260
        }
95 - 261
 
262
        $p = strpos($str, "Number of Discs:");
263
        if ($p === 0) {
264
            $noDiscs = substr($str, 17);
265
        }
266
 
267
        $p = strpos($str, "Label:");
268
        if ($p === 0) {
269
            $label = substr($str, 7);
270
        }
271
 
272
        $p = strpos($str, "Edition:");
273
        if ($p === 0) {
274
            $edition = substr($str, 9);
275
        }
276
 
277
        $p = strpos($str, "SPARS Code:");
278
        if ($p === 0) {
279
            $edition = (strlen($edition) > 0 ? ", " : "") . substr($str, 12);
280
        }
281
 
282
        $p = strpos($str, "Format:");
283
        if ($p === 0) {
284
            $edition = (strlen($edition) > 0 ? ", " : "") . substr($str, 8);
285
        }
286
 
287
        $p = strpos($str, "Performer:");
288
        if ($p === 0) {
289
            $artists = substr($str, 11);
290
        }
291
 
292
        $p = strpos($str, "Original Release Date:");
293
        if ($p === 0) {
294
            $releaseDate = substr($str, 23);
295
        }
296
 
297
        if (strpos($str, "Amazon Best Sellers Rank:") === 0) {
298
            $pieces = explode("\n", $str);
299
            $genres = [];
300
            foreach($pieces as $piece) {
301
                $piece = trim($piece);
302
                $p1 = strpos($piece, "in ");
303
                $p2 = strpos($piece, " (CDs & Vinyl)") ;
304
                if ($p1 === 0 && $p2 > 0) {
305
                    $genres[] = substr($piece, 4, $p2 - 4);
306
                }
307
            }
308
            $genre = join(", ", $genres);
309
        }
93 - 310
    }
95 - 311
 
312
    $item = new SimpleXMLElement("<item></item>");
313
    $item->addChild('ASIN', $asin);
314
    $item->addChild('DetailPageURL', $url);
315
    $item->addChild('MediumImage');
316
    $item->MediumImage->addChild('URL', $pic);
317
 
318
    $nodes = $xpath->query('//table[@id="dmusic_tracklist_content"]//div[contains(concat(" ", normalize-space(@class), " "), " a-section ")]//a[contains(concat(" ", normalize-space(@class), " "), " TitleLink ")]');
319
    if ($nodes->length > 0) {
320
        $item->addChild('Tracks');
321
        $item->Tracks->addChild('Disc', '1');
322
 
323
        foreach($nodes as $node) {
324
            $line = trim(preg_replace("/[\n\r]/","", $node->nodeValue));
325
            $item->Tracks->Disc->addChild('Track', $line);
326
        }
327
    } else {
328
        $nodes = $xpath->query('//div[@id="dmusic_tracklist_player"]//div[contains(concat(" ", normalize-space(@class), " "), " a-row ")]');
329
        if ($nodes->length > 0) {
330
            $item->addChild('Tracks');
331
            $item->Tracks->addChild('Disc', '1');
332
 
333
            foreach($nodes as $node) {
334
                $line = trim($node->nodeValue);
335
                if ($noDiscs == 1 && strpos($line, "Disc") === 0) {
336
                    continue;
337
                }
338
                $line = trim(preg_replace("/[\n\r]/","", $line));
339
                if (bin2hex(substr($line, 0, 2)) == "c2a0") {
340
                    $line = trim(substr($line, 2));
341
                }
342
                $item->Tracks->Disc->addChild('Track', $line);
343
            }
344
        }
345
    }
346
 
347
    $item->addChild('ItemAttributes');
348
    $item->ItemAttributes->addChild('Title', $title);
349
    $item->ItemAttributes->addChild('Artist', $artists);
350
    $item->ItemAttributes->addChild('Edition', $edition);
351
    $item->ItemAttributes->addChild('Genre', $genre);
352
    $item->ItemAttributes->addChild('Label', $label);
353
    $item->ItemAttributes->addChild('MediaType', $mediaType);
354
    $item->ItemAttributes->addChild('NumberOfDiscs', $noDiscs);
355
    $item->ItemAttributes->addChild('ReleaseDate', $releaseDate);
356
    $item->ItemAttributes->addChild('RunningTime', (int)$runTime);
357
 
358
    $_SESSION["discogs"] .= addMatch($item, $cnt, $mediaType);
359
}