Skip to content

Commit

Permalink
[IdealoBridge] Fix Feed items & Feed title customisation (#4013)
Browse files Browse the repository at this point in the history
- Feed items with new price tracking had "Max Price Used" instead of
  "Max Price New"
- Feed Title is now customised with the product name and the Price
  limits
- Fixed logic for saving prices in cache
- remove undefined variable notices
  • Loading branch information
sysadminstory authored Mar 13, 2024
1 parent 4bad1c1 commit e6cb5fd
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions bridges/IdealoBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,47 @@ public function getIcon()
return 'https://cdn.idealo.com/storage/ids-assets/ico/favicon.ico';
}

/**
* Returns the RSS Feed title when a RSS feed is rendered
* @return string the RSS feed Title
*/
private function getFeedTitle()
{
$cacheDuration = 604800;
$link = $this->getInput('Link');
$keyTITLE = $link . 'TITLE';
$product = $this->loadCacheValue($keyTITLE, $cacheDuration);

// The cache does not contain the title of the bridge, we must get it and save it in the cache
if ($product === null) {
$header = [
'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15'
];
$html = getSimpleHTMLDOM($link, $header);
$product = $html->find('.oopStage-title', 0)->find('span', 0)->plaintext;
$this->saveCacheValue($keyTITLE, $product);
}

$MaxPriceUsed = $this->getInput('MaxPriceUsed');
$MaxPriceNew = $this->getInput('MaxPriceNew');
$titleParts = [];

$titleParts[] = $product;

// Add Max Prices to the title
if ($MaxPriceUsed !== null) {
$titleParts[] = 'Max Price Used : ' . $MaxPriceUsed . '';
}
if ($MaxPriceNew !== null) {
$titleParts[] = 'Max Price New : ' . $MaxPriceNew . '';
}

$title = implode(' ', $titleParts);


return $title . ' - ' . $this::NAME;
}

public function collectData()
{
// Needs header with user-agent to function properly.
Expand Down Expand Up @@ -69,12 +110,16 @@ public function collectData()
$FirstButton = $html->find('.oopStage-conditionButton-wrapper-text', 0);
if ($FirstButton) {
$PriceNew = $FirstButton->find('strong', 0)->plaintext;
// Save current price
$this->saveCacheValue($KeyNEW, $PriceNew);
}

// Second Button is used
$SecondButton = $html->find('.oopStage-conditionButton-wrapper-text', 1);
if ($SecondButton) {
$PriceUsed = $SecondButton->find('strong', 0)->plaintext;
// Save current price
$this->saveCacheValue($KeyUSED, $PriceUsed);
}

// Only continue if a price has changed
Expand All @@ -83,16 +128,16 @@ public function collectData()
$image = $html->find('.datasheet-cover-image', 0)->src;

// Generate Content
if ($PriceNew > 1) {
if (isset($PriceNew) && $PriceNew > 1) {
$content = "<p><b>Price New:</b><br>$PriceNew</p>";
$content .= "<p><b>Price New before:</b><br>$OldPriceNew</p>";
}

if ($this->getInput('MaxPriceNew') != '') {
$content .= sprintf('<p><b>Max Price Used:</b><br>%s,00 €</p>', $this->getInput('MaxPriceNew'));
$content .= sprintf('<p><b>Max Price New:</b><br>%s,00 €</p>', $this->getInput('MaxPriceNew'));
}

if ($PriceUsed > 1) {
if (isset($PriceUsed) && $PriceUsed > 1) {
$content .= "<p><b>Price Used:</b><br>$PriceUsed</p>";
$content .= "<p><b>Price Used before:</b><br>$OldPriceUsed</p>";
}
Expand All @@ -110,8 +155,8 @@ public function collectData()

// Currently under Max new price
if ($this->getInput('MaxPriceNew') != '') {
if ($PriceNew < $this->getInput('MaxPriceNew')) {
$title = sprintf($Pricealarm, 'Used', $PriceNew, $Productname, $now);
if (isset($PriceNew) && $PriceNew < $this->getInput('MaxPriceNew')) {
$title = sprintf($Pricealarm, 'New', $PriceNew, $Productname, $now);
$item = [
'title' => $title,
'uri' => $link,
Expand All @@ -124,7 +169,7 @@ public function collectData()

// Currently under Max used price
if ($this->getInput('MaxPriceUsed') != '') {
if ($PriceUsed < $this->getInput('MaxPriceUsed')) {
if (isset($PriceUsed) && $PriceUsed < $this->getInput('MaxPriceUsed')) {
$title = sprintf($Pricealarm, 'Used', $PriceUsed, $Productname, $now);
$item = [
'title' => $title,
Expand All @@ -143,23 +188,23 @@ public function collectData()
(!$this->getInput('ExcludeNew') && $PriceNew != $OldPriceNew ) ||
(!$this->getInput('ExcludeUsed') && $PriceUsed != $OldPriceUsed )
) {
$title .= 'Priceupdate! ';
$title = 'Priceupdate! ';

if (!$this->getInput('ExcludeNew')) {
if ($PriceNew < $OldPriceNew) {
if (isset($PriceNew) && $PriceNew < $OldPriceNew) {
$title .= 'NEW:&#11015 '; // Arrow Down Emoji
}
if ($PriceNew > $OldPriceNew) {
if (isset($PriceNew) && $PriceNew > $OldPriceNew) {
$title .= 'NEW:&#11014 '; // Arrow Up Emoji
}
}


if (!$this->getInput('ExcludeUsed')) {
if ($PriceUsed < $OldPriceUsed) {
if (isset($PriceUsed) && $PriceUsed < $OldPriceUsed) {
$title .= 'USED:&#11015 '; // Arrow Down Emoji
}
if ($PriceUsed > $OldPriceUsed) {
if (isset($PriceUsed) && $PriceUsed > $OldPriceUsed) {
$title .= 'USED:&#11014 '; // Arrow Up Emoji
}
}
Expand All @@ -177,9 +222,19 @@ public function collectData()
}
}
}
}

// Save current price
$this->saveCacheValue($KeyNEW, $PriceNew);
$this->saveCacheValue($KeyUSED, $PriceUsed);
/**
* Returns the RSS Feed title according to the parameters
* @return string the RSS feed Tile
*/
public function getName()
{
switch ($this->queriedContext) {
case '0':
return $this->getFeedTitle();
default:
return parent::getName();
}
}
}

0 comments on commit e6cb5fd

Please sign in to comment.