1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-05 08:07:33 +02:00

[AmazonPriceTracker] Fix price not shown, new default source (#4631)

Fixes issue #4586

Co-authored-by: Simone Dotto <simonedotto@proton.me>
This commit is contained in:
Simone Dotto
2025-08-04 14:31:43 +02:00
committed by GitHub
parent aba38845d2
commit b5f90f8d47

View File

@@ -2,7 +2,7 @@
class AmazonPriceTrackerBridge extends BridgeAbstract class AmazonPriceTrackerBridge extends BridgeAbstract
{ {
const MAINTAINER = 'captn3m0, sal0max'; const MAINTAINER = 'captn3m0, sal0max, bagnacauda';
const NAME = 'Amazon Price Tracker'; const NAME = 'Amazon Price Tracker';
const URI = 'https://www.amazon.com/'; const URI = 'https://www.amazon.com/';
const CACHE_TIMEOUT = 3600; // 1h const CACHE_TIMEOUT = 3600; // 1h
@@ -13,7 +13,7 @@ class AmazonPriceTrackerBridge extends BridgeAbstract
'asin' => [ 'asin' => [
'name' => 'ASIN', 'name' => 'ASIN',
'required' => true, 'required' => true,
'exampleValue' => 'B071GB1VMQ', 'exampleValue' => 'B0923XT6K7',
// https://stackoverflow.com/a/12827734 // https://stackoverflow.com/a/12827734
'pattern' => 'B[\dA-Z]{9}|\d{9}(X|\d)', 'pattern' => 'B[\dA-Z]{9}|\d{9}(X|\d)',
], ],
@@ -169,19 +169,23 @@ EOT;
private function scrapePriceTwister($html) private function scrapePriceTwister($html)
{ {
$str = $html->find('.twister-plus-buying-options-price-data', 0); $json = $html->find('.twister-plus-buying-options-price-data', 0);
if ($json == null) {
return null;
}
$data = json_decode($str->innertext, true); $data = json_decode($json->innertext, true);
if (count($data) === 1) { foreach ($data as $key => $value) {
$data = $data[0]; $value = $value[0];
return [ return [
'displayPrice' => $data['displayPrice'], 'displayPrice' => $value['displayPrice'],
'currency' => $data['currency'], 'price' => $value['priceAmount'],
'shipping' => '0', 'currency' => $value['currencySymbol'],
'shipping' => null,
]; ];
} }
return false; return null;
} }
private function scrapePriceGeneric($html) private function scrapePriceGeneric($html)
@@ -206,9 +210,21 @@ EOT;
} }
$priceString = str_replace(str_split(self::WHITESPACE), '', $priceDiv->plaintext); $priceString = str_replace(str_split(self::WHITESPACE), '', $priceDiv->plaintext);
preg_match('/(\d+\.\d{0,2})/', $priceString, $matches); $price = null;
$priceFound = false;
// find longest repeated string
for ($offset = 0; $offset < strlen($priceString); $offset++) {
for ($length = 1; substr_count($priceString, substr($priceString, $offset, $length + 1)) >= 2; $length++) {
$priceFound = true;
}
if ($priceFound) {
$price = substr($priceString, $offset, $length);
break;
}
}
$price = $matches[0] ?? null;
$currency = str_replace($price, '', $priceString); $currency = str_replace($price, '', $priceString);
if ($price != null && $currency != null) { if ($price != null && $currency != null) {
@@ -216,7 +232,7 @@ EOT;
'price' => $price, 'price' => $price,
'displayPrice' => null, 'displayPrice' => null,
'currency' => $currency, 'currency' => $currency,
'shipping' => '0' 'shipping' => null
]; ];
} }
return $default; return $default;
@@ -227,7 +243,7 @@ EOT;
$html = $this->getHtml(); $html = $this->getHtml();
$this->title = $this->getTitle($html); $this->title = $this->getTitle($html);
$image = $this->getImage($html); $image = $this->getImage($html);
$data = $this->scrapePriceGeneric($html); $data = $this->scrapePriceTwister($html) ?? $this->scrapePriceGeneric($html);
// render // render
$content = ''; $content = '';
@@ -236,7 +252,7 @@ EOT;
$price = sprintf('%s %s', $data['price'], $data['currency']); $price = sprintf('%s %s', $data['price'], $data['currency']);
} }
$content .= sprintf('%s<br>Price: %s', $image, $price); $content .= sprintf('%s<br>Price: %s', $image, $price);
if ($data['shipping'] !== '0') { if ($data['shipping'] !== null) {
$content .= sprintf('<br>Shipping: %s %s</br>', $data['shipping'], $data['currency']); $content .= sprintf('<br>Shipping: %s %s</br>', $data['shipping'], $data['currency']);
} }