2016-12-06 17:44:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AmazonBridge extends BridgeAbstract {
|
|
|
|
|
2016-12-13 00:43:38 +01:00
|
|
|
const MAINTAINER = 'Alexis CHEMEL';
|
|
|
|
const NAME = 'Amazon';
|
|
|
|
const URI = 'https://www.amazon.com/';
|
2016-12-06 17:44:56 +01:00
|
|
|
const CACHE_TIMEOUT = 3600; // 1h
|
2016-12-13 00:43:38 +01:00
|
|
|
const DESCRIPTION = 'Returns products from Amazon search';
|
2016-12-06 17:44:56 +01:00
|
|
|
|
|
|
|
const PARAMETERS = array(array(
|
|
|
|
'q' => array(
|
|
|
|
'name' => 'Keyword',
|
|
|
|
'required' => true,
|
|
|
|
),
|
|
|
|
'sort' => array(
|
|
|
|
'name' => 'Sort by',
|
|
|
|
'type' => 'list',
|
|
|
|
'required' => false,
|
|
|
|
'values' => array(
|
2016-12-12 22:28:05 +01:00
|
|
|
'Relevance' => 'relevanceblender',
|
2016-12-13 00:43:38 +01: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 17:44:56 +01:00
|
|
|
),
|
2016-12-13 00:43:38 +01:00
|
|
|
'defaultValue' => 'relevanceblender',
|
|
|
|
),
|
|
|
|
'tld' => array(
|
|
|
|
'name' => 'Country',
|
|
|
|
'type' => 'list',
|
|
|
|
'required' => true,
|
|
|
|
'values' => array(
|
|
|
|
'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',
|
|
|
|
'Spain' => 'es',
|
|
|
|
'United Kingdom' => 'co.uk',
|
|
|
|
'United States' => 'com',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'com',
|
|
|
|
),
|
2016-12-06 17:44:56 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
public function getName(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('tld')) && !is_null($this->getInput('q'))) {
|
2018-11-05 12:55:58 +01:00
|
|
|
return 'Amazon.' . $this->getInput('tld') . ': ' . $this->getInput('q');
|
2017-02-14 22:20:55 +01:00
|
|
|
}
|
2016-12-06 17:44:56 +01:00
|
|
|
|
2017-02-14 22:20:55 +01:00
|
|
|
return parent::getName();
|
2016-12-06 17:44:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function collectData() {
|
|
|
|
|
2018-11-05 12:55:58 +01:00
|
|
|
$uri = 'https://www.amazon.' . $this->getInput('tld') . '/';
|
|
|
|
$uri .= 's/?field-keywords=' . urlencode($this->getInput('q')) . '&sort=' . $this->getInput('sort');
|
2016-12-06 17:44:56 +01:00
|
|
|
|
2016-12-13 00:43:38 +01:00
|
|
|
$html = getSimpleHTMLDOM($uri)
|
2016-12-06 17:44:56 +01:00
|
|
|
or returnServerError('Could not request Amazon.');
|
|
|
|
|
|
|
|
foreach($html->find('li.s-result-item') as $element) {
|
|
|
|
|
|
|
|
$item = array();
|
|
|
|
|
|
|
|
// Title
|
|
|
|
$title = $element->find('h2', 0);
|
|
|
|
|
|
|
|
$item['title'] = html_entity_decode($title->innertext, ENT_QUOTES);
|
|
|
|
|
|
|
|
// Url
|
|
|
|
$uri = $title->parent()->getAttribute('href');
|
|
|
|
$uri = substr($uri, 0, strrpos($uri, '/'));
|
|
|
|
|
|
|
|
$item['uri'] = substr($uri, 0, strrpos($uri, '/'));
|
|
|
|
|
|
|
|
// Content
|
|
|
|
$image = $element->find('img', 0);
|
|
|
|
$price = $element->find('span.s-price', 0);
|
|
|
|
$price = ($price) ? $price->innertext : '';
|
|
|
|
|
2018-11-05 12:55:58 +01:00
|
|
|
$item['content'] = '<img src="' . $image->getAttribute('src') . '" /><br />' . $price;
|
2016-12-06 17:44:56 +01:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|