2018-07-16 15:54:52 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AmazonPriceTrackerBridge extends BridgeAbstract {
|
2021-07-05 23:26:08 +03:00
|
|
|
const MAINTAINER = 'captn3m0, sal0max';
|
2018-07-16 15:54:52 +03:00
|
|
|
const NAME = 'Amazon Price Tracker';
|
|
|
|
const URI = 'https://www.amazon.com/';
|
|
|
|
const CACHE_TIMEOUT = 3600; // 1h
|
|
|
|
const DESCRIPTION = 'Tracks price for a single product on Amazon';
|
|
|
|
|
|
|
|
const PARAMETERS = array(
|
|
|
|
array(
|
|
|
|
'asin' => array(
|
|
|
|
'name' => 'ASIN',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'B071GB1VMQ',
|
|
|
|
// https://stackoverflow.com/a/12827734
|
|
|
|
'pattern' => 'B[\dA-Z]{9}|\d{9}(X|\d)',
|
|
|
|
),
|
|
|
|
'tld' => array(
|
|
|
|
'name' => 'Country',
|
|
|
|
'type' => 'list',
|
|
|
|
'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',
|
2020-11-16 20:13:23 +03:00
|
|
|
'Sweden' => 'se',
|
2018-07-16 15:54:52 +03:00
|
|
|
'United Kingdom' => 'co.uk',
|
|
|
|
'United States' => 'com',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'com',
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
2021-07-12 20:49:29 +03:00
|
|
|
const PRICE_SELECTORS = array(
|
|
|
|
'#priceblock_ourprice',
|
|
|
|
'.priceBlockBuyingPriceString',
|
|
|
|
'#newBuyBoxPrice',
|
|
|
|
'#tp_price_block_total_price_ww',
|
|
|
|
'span.offer-price',
|
|
|
|
'.a-color-price',
|
|
|
|
);
|
|
|
|
|
2022-04-04 20:41:40 +03:00
|
|
|
const WHITESPACE = " \t\n\r\0\x0B\xC2\xA0";
|
|
|
|
|
2018-07-16 15:54:52 +03:00
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates domain name given a amazon TLD
|
|
|
|
*/
|
|
|
|
private function getDomainName() {
|
|
|
|
return 'https://www.amazon.' . $this->getInput('tld');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates URI for a Amazon product page
|
|
|
|
*/
|
|
|
|
public function getURI() {
|
|
|
|
if (!is_null($this->getInput('asin'))) {
|
2021-07-12 20:49:29 +03:00
|
|
|
return $this->getDomainName() . '/dp/' . $this->getInput('asin');
|
2018-07-16 15:54:52 +03:00
|
|
|
}
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrapes the product title from the html page
|
|
|
|
* returns the default title if scraping fails
|
|
|
|
*/
|
|
|
|
private function getTitle($html) {
|
|
|
|
$titleTag = $html->find('#productTitle', 0);
|
|
|
|
|
|
|
|
if (!$titleTag) {
|
|
|
|
return $this->getDefaultTitle();
|
|
|
|
} else {
|
|
|
|
return trim(html_entity_decode($titleTag->innertext, ENT_QUOTES));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Title used by the feed if none could be found
|
|
|
|
*/
|
|
|
|
private function getDefaultTitle() {
|
|
|
|
return 'Amazon.' . $this->getInput('tld') . ': ' . $this->getInput('asin');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns name for the feed
|
|
|
|
* Uses title (already scraped) if it has one
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
if (isset($this->title)) {
|
|
|
|
return $this->title;
|
|
|
|
} else {
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 22:44:37 +03:00
|
|
|
private function parseDynamicImage($attribute) {
|
|
|
|
$json = json_decode(html_entity_decode($attribute), true);
|
|
|
|
|
|
|
|
if ($json and count($json) > 0) {
|
|
|
|
return array_keys($json)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 15:54:52 +03:00
|
|
|
/**
|
|
|
|
* Returns a generated image tag for the product
|
|
|
|
*/
|
|
|
|
private function getImage($html) {
|
|
|
|
$imageSrc = $html->find('#main-image-container img', 0);
|
|
|
|
|
|
|
|
if ($imageSrc) {
|
2018-07-31 22:44:37 +03:00
|
|
|
$hiresImage = $imageSrc->getAttribute('data-old-hires');
|
|
|
|
$dynamicImageAttribute = $imageSrc->getAttribute('data-a-dynamic-image');
|
|
|
|
$image = $hiresImage ?: $this->parseDynamicImage($dynamicImageAttribute);
|
2018-07-16 15:54:52 +03:00
|
|
|
}
|
2018-07-31 22:44:37 +03:00
|
|
|
$image = $image ?: 'https://placekitten.com/200/300';
|
|
|
|
|
|
|
|
return <<<EOT
|
|
|
|
<img width="300" style="max-width:300;max-height:300" src="$image" alt="{$this->title}" />
|
|
|
|
EOT;
|
2018-07-16 15:54:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return \simple_html_dom object
|
|
|
|
* for the entire html of the product page
|
|
|
|
*/
|
|
|
|
private function getHtml() {
|
|
|
|
$uri = $this->getURI();
|
|
|
|
|
|
|
|
return getSimpleHTMLDOM($uri) ?: returnServerError('Could not request Amazon.');
|
|
|
|
}
|
|
|
|
|
2018-07-31 22:44:37 +03:00
|
|
|
private function scrapePriceFromMetrics($html) {
|
|
|
|
$asinData = $html->find('#cerberus-data-metrics', 0);
|
|
|
|
|
|
|
|
// <div id="cerberus-data-metrics" style="display: none;"
|
|
|
|
// data-asin="B00WTHJ5SU" data-asin-price="14.99" data-asin-shipping="0"
|
|
|
|
// data-asin-currency-code="USD" data-substitute-count="-1" ... />
|
|
|
|
if ($asinData) {
|
2019-11-01 20:06:38 +03:00
|
|
|
return array(
|
2018-07-31 22:44:37 +03:00
|
|
|
'price' => $asinData->getAttribute('data-asin-price'),
|
|
|
|
'currency' => $asinData->getAttribute('data-asin-currency-code'),
|
|
|
|
'shipping' => $asinData->getAttribute('data-asin-shipping')
|
2019-11-01 20:06:38 +03:00
|
|
|
);
|
2018-07-31 22:44:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-04 20:41:40 +03:00
|
|
|
private function scrapePriceTwister($html) {
|
|
|
|
$str = $html->find('.twister-plus-buying-options-price-data', 0);
|
|
|
|
|
|
|
|
$data = json_decode($str->innertext, true);
|
|
|
|
if(count($data) === 1) {
|
|
|
|
$data = $data[0];
|
|
|
|
return array(
|
|
|
|
'displayPrice' => $data['displayPrice'],
|
|
|
|
'currency' => $data['currency'],
|
|
|
|
'shipping' => '0',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-31 22:44:37 +03:00
|
|
|
private function scrapePriceGeneric($html) {
|
2021-07-12 20:49:29 +03:00
|
|
|
$priceDiv = null;
|
|
|
|
|
|
|
|
foreach(self::PRICE_SELECTORS as $sel) {
|
|
|
|
$priceDiv = $html->find($sel, 0);
|
|
|
|
if ($priceDiv) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$priceDiv) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-04 20:41:40 +03:00
|
|
|
$priceString = str_replace(str_split(self::WHITESPACE), '', $priceDiv->plaintext);
|
|
|
|
preg_match('/(\d+\.\d{0,2})/', $priceString, $matches);
|
2018-07-31 22:44:37 +03:00
|
|
|
|
2021-07-05 23:26:08 +03:00
|
|
|
$price = $matches[0];
|
2022-04-04 20:41:40 +03:00
|
|
|
$currency = str_replace($price, '', $priceString);
|
2021-07-05 23:26:08 +03:00
|
|
|
|
|
|
|
if ($price != null && $currency != null) {
|
2019-11-01 20:06:38 +03:00
|
|
|
return array(
|
2021-07-05 23:26:08 +03:00
|
|
|
'price' => $price,
|
|
|
|
'currency' => $currency,
|
2018-07-31 22:44:37 +03:00
|
|
|
'shipping' => '0'
|
2019-11-01 20:06:38 +03:00
|
|
|
);
|
2018-07-31 22:44:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-04 20:41:40 +03:00
|
|
|
private function renderContent($image, $data) {
|
|
|
|
$price = $data['displayPrice'];
|
|
|
|
if (!$price) {
|
|
|
|
$price = "{$data['price']} {$data['currency']}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = "$image<br>Price: $price";
|
|
|
|
|
|
|
|
if ($data['shipping'] !== '0') {
|
|
|
|
$html .= "<br>Shipping: {$data['shipping']} {$data['currency']}</br>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2018-07-16 15:54:52 +03:00
|
|
|
/**
|
|
|
|
* Scrape method for Amazon product page
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
|
|
|
public function collectData() {
|
|
|
|
$html = $this->getHtml();
|
|
|
|
$this->title = $this->getTitle($html);
|
|
|
|
$imageTag = $this->getImage($html);
|
|
|
|
|
2022-04-04 20:41:40 +03:00
|
|
|
$data = $this->scrapePriceGeneric($html);
|
2018-07-16 15:54:52 +03:00
|
|
|
|
|
|
|
$item = array(
|
|
|
|
'title' => $this->title,
|
|
|
|
'uri' => $this->getURI(),
|
2022-04-04 20:41:40 +03:00
|
|
|
'content' => $this->renderContent($imageTag, $data),
|
2021-07-12 20:49:29 +03:00
|
|
|
// This is to ensure that feed readers notice the price change
|
|
|
|
'uid' => md5($data['price'])
|
2018-07-16 15:54:52 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|