2016-12-06 19:44:56 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AmazonBridge extends BridgeAbstract
|
|
|
|
{
|
2016-12-13 02:43:38 +03:00
|
|
|
const MAINTAINER = 'Alexis CHEMEL';
|
|
|
|
const NAME = 'Amazon';
|
|
|
|
const URI = 'https://www.amazon.com/';
|
2016-12-06 19:44:56 +03:00
|
|
|
const CACHE_TIMEOUT = 3600; // 1h
|
2016-12-13 02:43:38 +03:00
|
|
|
const DESCRIPTION = 'Returns products from Amazon search';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-12-06 19:44:56 +03:00
|
|
|
const PARAMETERS = [[
|
|
|
|
'q' => [
|
|
|
|
'name' => 'Keyword',
|
|
|
|
'required' => true,
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'watch',
|
2016-12-06 19:44:56 +03:00
|
|
|
],
|
|
|
|
'sort' => [
|
|
|
|
'name' => 'Sort by',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => [
|
2016-12-13 00:28:05 +03:00
|
|
|
'Relevance' => 'relevanceblender',
|
2016-12-13 02:43:38 +03:00
|
|
|
'Price: Low to High' => 'price-asc-rank',
|
|
|
|
'Price: High to Low' => 'price-desc-rank',
|
|
|
|
'Average Customer Review' => 'review-rank',
|
|
|
|
'Newest Arrivals' => 'date-desc-rank',
|
2016-12-06 19:44:56 +03:00
|
|
|
],
|
2016-12-13 02:43:38 +03:00
|
|
|
'defaultValue' => 'relevanceblender',
|
|
|
|
],
|
|
|
|
'tld' => [
|
|
|
|
'name' => 'Country',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => [
|
|
|
|
'Australia' => 'com.au',
|
|
|
|
'Brazil' => 'com.br',
|
|
|
|
'Canada' => 'ca',
|
|
|
|
'China' => 'cn',
|
|
|
|
'France' => 'fr',
|
|
|
|
'Germany' => 'de',
|
|
|
|
'India' => 'in',
|
|
|
|
'Italy' => 'it',
|
|
|
|
'Japan' => 'co.jp',
|
|
|
|
'Mexico' => 'com.mx',
|
|
|
|
'Netherlands' => 'nl',
|
2022-07-21 21:41:15 +03:00
|
|
|
'Poland' => 'pl',
|
2016-12-13 02:43:38 +03:00
|
|
|
'Spain' => 'es',
|
2022-06-04 22:59:52 +03:00
|
|
|
'Sweden' => 'se',
|
|
|
|
'Turkey' => 'com.tr',
|
2016-12-13 02:43:38 +03:00
|
|
|
'United Kingdom' => 'co.uk',
|
|
|
|
'United States' => 'com',
|
|
|
|
],
|
|
|
|
'defaultValue' => 'com',
|
|
|
|
],
|
2016-12-06 19:44:56 +03:00
|
|
|
]];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-12-06 19:44:56 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2022-04-13 00:34:40 +03:00
|
|
|
$baseUrl = sprintf('https://www.amazon.%s', $this->getInput('tld'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$url = sprintf(
|
|
|
|
'%s/s/?field-keywords=%s&sort=%s',
|
|
|
|
$baseUrl,
|
|
|
|
urlencode($this->getInput('q')),
|
|
|
|
$this->getInput('sort')
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$dom = getSimpleHTMLDOM($url);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$elements = $dom->find('div.s-result-item');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
foreach ($elements as $element) {
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-12-06 19:44:56 +03:00
|
|
|
$title = $element->find('h2', 0);
|
2022-04-13 00:34:40 +03:00
|
|
|
if (!$title) {
|
2019-01-06 20:38:53 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$item['title'] = $title->innertext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$itemUrl = $element->find('a', 0)->href;
|
|
|
|
$item['uri'] = urljoin($baseUrl, $itemUrl);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-12-06 19:44:56 +03:00
|
|
|
$image = $element->find('img', 0);
|
2022-04-13 00:34:40 +03:00
|
|
|
if ($image) {
|
|
|
|
$item['content'] = '<img src="' . $image->getAttribute('src') . '" /><br />';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
$price = $element->find('span.a-price > .a-offscreen', 0);
|
|
|
|
if ($price) {
|
|
|
|
$item['content'] .= $price->innertext;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-12-06 19:44:56 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2016-12-06 19:44:56 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('tld')) && !is_null($this->getInput('q'))) {
|
|
|
|
return 'Amazon.' . $this->getInput('tld') . ': ' . $this->getInput('q');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-13 00:34:40 +03:00
|
|
|
return parent::getName();
|
|
|
|
}
|
2016-12-06 19:44:56 +03:00
|
|
|
}
|