1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
<?php
class IdealoBridge extends BridgeAbstract
{
const NAME = 'idealo.de / idealo.fr / idealo.es Bridge';
const URI = 'https://www.idealo.de';
const DESCRIPTION = 'Tracks the price for a product on idealo.de / idealo.fr / idealo.es. Pricealarm if specific price is set';
const MAINTAINER = 'SebLaus';
const CACHE_TIMEOUT = 60 * 30; // 30 min
const PARAMETERS = [
[
'Link' => [
'name' => 'idealo.de / idealo.fr / idealo.es Link to productpage',
'required' => true,
'exampleValue' => 'https://www.idealo.de/preisvergleich/OffersOfProduct/202007367_-s7-pro-ultra-roborock.html'
],
'ExcludeNew' => [
'name' => 'Priceupdate: Do not track new items',
'type' => 'checkbox',
'value' => 'c'
],
'ExcludeUsed' => [
'name' => 'Priceupdate: Do not track used items',
'type' => 'checkbox',
'value' => 'uc'
],
'MaxPriceNew' => [
'name' => 'Pricealarm: Maximum price for new Product',
'type' => 'number'
],
'MaxPriceUsed' => [
'name' => 'Pricealarm: Maximum price for used Product',
'type' => 'number'
],
]
];
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);
// 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;
}
/**
* Returns the Price as float
* @return float rhe price converted in float
*/
private function convertPriceToFloat($price)
{
// Every price is stored / displayed as "xxx,xx €", but PHP can't convert it as float
if ($price !== null) {
// Convert comma as dot
$price = str_replace(',', '.', $price);
// Remove the '€' char
$price = str_replace('€', '', $price);
// Convert to float
return floatval($price);
} else {
return $price;
}
}
/**
* Returns the Price Trend emoji
* @return string the Price Trend Emoji
*/
private function getPriceTrend($NewPrice, $OldPrice)
{
$NewPrice = $this->convertPriceToFloat($NewPrice);
$OldPrice = $this->convertPriceToFloat($OldPrice);
// In case there is no old Price, then show no trend
if ($OldPrice === null || $OldPrice == 0) {
$trend = '';
} else if ($NewPrice > $OldPrice) {
$trend = '↗';
} else if ($NewPrice == $OldPrice) {
$trend = '➡';
} else if ($NewPrice < $OldPrice) {
$trend = '↘';
}
return $trend;
}
public function collectData()
{
// Needs header with user-agent to function properly.
$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'
];
$link = $this->getInput('Link');
$html = getSimpleHTMLDOM($link, $header);
// Get Productname
$titleobj = $html->find('.oopStage-title', 0);
$Productname = $titleobj->find('span', 0)->plaintext;
// Create product specific Cache Keys with the link
$KeyNEW = $link;
$KeyNEW .= 'NEW';
$KeyUSED = $link;
$KeyUSED .= 'USED';
// Load previous Price
$OldPriceNew = $this->loadCacheValue($KeyNEW);
$OldPriceUsed = $this->loadCacheValue($KeyUSED);
// First button contains the new price. Found at oopStage-conditionButton-wrapper-text class (.)
$ActualNewPrice = $html->find('div[id=oopStage-conditionButton-new]', 0);
// Second Button contains the used product price
$ActualUsedPrice = $html->find('div[id=oopStage-conditionButton-used]', 0);
// Get the first item of the offers list to have an option if there is no New/Used Button available
$altPrice = $html->find('.productOffers-listItemOfferPrice', 0);
if ($ActualNewPrice) {
$PriceNew = $ActualNewPrice->find('strong', 0)->plaintext;
// Save current price
$this->saveCacheValue($KeyNEW, $PriceNew);
} else if ($altPrice) {
// Get price from first List item if no New/used Buttons available
$PriceNew = trim($altPrice->plaintext);
$this->saveCacheValue($KeyNEW, $PriceNew);
} else if (($ActualNewPrice === null || $altPrice === null) && $ActualUsedPrice !== null) {
// In case there is no actual New Price and a Used Price exists, then delete the previous value in the cache
$this->cache->delete($this->getShortName() . '_' . $KeyNEW);
}
// Second Button contains the used product price
if ($ActualUsedPrice) {
$PriceUsed = $ActualUsedPrice->find('strong', 0)->plaintext;
// Save current price
$this->saveCacheValue($KeyUSED, $PriceUsed);
} else if ($ActualUsedPrice === null && ($ActualNewPrice !== null || $altPrice !== null)) {
// In case there is no actual Used Price and a New Price exists, then delete the previous value in the cache
$this->cache->delete($this->getShortName() . '_' . $KeyUSED);
}
// Only continue if a price has changed and there exists a New, Used or Alternative price (sometimes no new Price _and_ Used Price are shown)
if (!($ActualNewPrice === null && $ActualUsedPrice === null && $altPrice === null) && ($PriceNew != $OldPriceNew || $PriceUsed != $OldPriceUsed)) {
// Get Product Image
$image = $html->find('.datasheet-cover-image', 0)->src;
$content = '';
// Generate Content
if (isset($PriceNew) && $this->convertPriceToFloat($PriceNew) > 0) {
$content .= sprintf('<p><b>Price New:</b><br>%s %s</p>', $PriceNew, $this->getPriceTrend($PriceNew, $OldPriceNew));
$content .= "<p><b>Price New before:</b><br>$OldPriceNew</p>";
}
if ($this->getInput('MaxPriceNew') != '') {
$content .= sprintf('<p><b>Max Price New:</b><br>%s,00 €</p>', $this->getInput('MaxPriceNew'));
}
if (isset($PriceUsed) && $this->convertPriceToFloat($PriceUsed) > 0) {
$content .= sprintf('<p><b>Price Used:</b><br>%s %s</p>', $PriceUsed, $this->getPriceTrend($PriceUsed, $OldPriceUsed));
$content .= "<p><b>Price Used before:</b><br>$OldPriceUsed</p>";
}
if ($this->getInput('MaxPriceUsed') != '') {
$content .= sprintf('<p><b>Max Price Used:</b><br>%s,00 €</p>', $this->getInput('MaxPriceUsed'));
}
$content .= "<img src=$image>";
$now = date('d/m/Y H:i');
$Pricealarm = 'Pricealarm %s: %s %s - %s';
// Currently under Max new price
if ($this->getInput('MaxPriceNew') != '') {
if (isset($PriceNew) && $this->convertPriceToFloat($PriceNew) < $this->getInput('MaxPriceNew')) {
$title = sprintf($Pricealarm, 'New', $PriceNew, $Productname, $now);
$item = [
'title' => $title,
'uri' => $link,
'content' => $content,
'uid' => md5($title)
];
$this->items[] = $item;
}
}
// Currently under Max used price
if ($this->getInput('MaxPriceUsed') != '') {
if (isset($PriceUsed) && $this->convertPriceToFloat($PriceUsed) < $this->getInput('MaxPriceUsed')) {
$title = sprintf($Pricealarm, 'Used', $PriceUsed, $Productname, $now);
$item = [
'title' => $title,
'uri' => $link,
'content' => $content,
'uid' => md5($title)
];
$this->items[] = $item;
}
}
// General Priceupdate Without any Max Price for new and Used product
if ($this->getInput('MaxPriceUsed') == '' && $this->getInput('MaxPriceNew') == '') {
// check if a relevant pricechange happened
if (
(!$this->getInput('ExcludeNew') && $PriceNew != $OldPriceNew ) ||
(!$this->getInput('ExcludeUsed') && $PriceUsed != $OldPriceUsed )
) {
$title = 'Priceupdate! ';
if (!$this->getInput('ExcludeNew') && isset($PriceNew)) {
$title .= 'NEW' . $this->getPriceTrend($PriceNew, $OldPriceNew) . ' ';
}
if (!$this->getInput('ExcludeUsed') && isset($PriceUsed)) {
$title .= 'USED' . $this->getPriceTrend($PriceUsed, $OldPriceUsed) . ' ';
}
$title .= $Productname;
$title .= ' - ';
$title .= $now;
$item = [
'title' => $title,
'uri' => $link,
'content' => $content,
'uid' => md5($title)
];
$this->items[] = $item;
}
}
}
}
/**
* 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();
}
}
/**
* Returns the RSS Feed URL according to the parameters
* @return string the RSS feed URL
*/
public function getURI()
{
switch ($this->queriedContext) {
case '0':
return $this->getInput('Link');
default:
return parent::getURI();
}
}
}
|