fix: various bug fixes (#3102)

* fix: Undefined offset: 4

* fix: Trying to access array offset on value of type bool

* fix: Undefined variable: photo at bridges/TelegramBridge.php line 287

* fix: Trying to get property innertext of non-object at bridges/ZDNetBridge.php line 186

* fix: Undefined index: Category at bridges/UnraidCommunityApplicationsBridge.php line 42

* fix: Undefined index: fullUrl at bridges/EuronewsBridge.php line 61
This commit is contained in:
Dag 2022-10-16 20:26:33 +02:00 committed by GitHub
parent ffbc107687
commit 37f1ab726b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 71 deletions

View file

@ -187,6 +187,12 @@ EOT;
private function scrapePriceGeneric($html)
{
$default = [
'price' => null,
'displayPrice' => null,
'currency' => null,
'shipping' => null,
];
$priceDiv = null;
foreach (self::PRICE_SELECTORS as $sel) {
@ -197,58 +203,48 @@ EOT;
}
if (!$priceDiv) {
return false;
return $default;
}
$priceString = str_replace(str_split(self::WHITESPACE), '', $priceDiv->plaintext);
preg_match('/(\d+\.\d{0,2})/', $priceString, $matches);
$price = $matches[0];
$price = $matches[0] ?? null;
$currency = str_replace($price, '', $priceString);
if ($price != null && $currency != null) {
return [
'price' => $price,
'displayPrice' => null,
'currency' => $currency,
'shipping' => '0'
];
}
return false;
return $default;
}
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;
}
/**
* Scrape method for Amazon product page
* @return [type] [description]
*/
public function collectData()
{
$html = $this->getHtml();
$this->title = $this->getTitle($html);
$imageTag = $this->getImage($html);
$image = $this->getImage($html);
$data = $this->scrapePriceGeneric($html);
// render
$content = '';
$price = $data['displayPrice'];
if (!$price) {
$price = sprintf('%s %s', $data['price'], $data['currency']);
}
$content .= sprintf('%s<br>Price: %s', $image, $price);
if ($data['shipping'] !== '0') {
$content .= sprintf('<br>Shipping: %s %s</br>', $data['shipping'], $data['currency']);
}
$item = [
'title' => $this->title,
'uri' => $this->getURI(),
'content' => $this->renderContent($imageTag, $data),
'content' => $content,
// This is to ensure that feed readers notice the price change
'uid' => md5($data['price'])
];