2021-05-20 18:03:45 +02:00
package main
import (
"regexp"
log "github.com/sirupsen/logrus"
)
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
// DefaultCurrency to fallback when no currency is provided
const DefaultCurrency = "USD"
2021-05-20 18:03:45 +02:00
// RangeFilter to store the pattern to match product model and price limits
type RangeFilter struct {
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
model * regexp . Regexp
min float64
max float64
currency string
converter * CurrencyConverter
2021-05-20 18:03:45 +02:00
}
// NewRangeFilter to create a RangeFilter
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
func NewRangeFilter ( regex string , min float64 , max float64 , currency string , converter * CurrencyConverter ) ( * RangeFilter , error ) {
2021-05-20 18:03:45 +02:00
var err error
var compiledRegex * regexp . Regexp
log . Debugf ( "compiling model filter regex" )
if regex != "" {
compiledRegex , err = regexp . Compile ( regex )
if err != nil {
return nil , err
}
}
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
var detectedCurrency string
if currency != "" {
detectedCurrency = currency
} else {
detectedCurrency = DefaultCurrency
}
2021-05-20 18:03:45 +02:00
return & RangeFilter {
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
model : compiledRegex ,
min : min ,
max : max ,
converter : converter ,
currency : detectedCurrency ,
2021-05-20 18:03:45 +02:00
} , nil
}
// Include returns false when a product name matches the model regex and price is outside of the range
// implements the Filter interface
func ( f * RangeFilter ) Include ( product * Product ) bool {
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
// include products with a missing model regex
2021-05-20 18:03:45 +02:00
if f . model == nil {
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
log . Debugf ( "product %s included because range filter model is missing" , product . Name )
return true
}
// include products with a different model
if ! f . model . MatchString ( product . Name ) {
log . Debugf ( "product %s included because range filter model hasn't been detected in the product name" , product . Name )
2021-05-20 18:03:45 +02:00
return true
}
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
// convert price to the filter currency
convertedPrice , err := f . converter . Convert ( product . Price , product . PriceCurrency , f . currency )
if err != nil {
log . Warnf ( "could not convert price %.2f %s to %s for range filter: %s" , product . Price , product . PriceCurrency , f . currency , err )
return true
2021-05-20 18:03:45 +02:00
}
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
// include prices with unlimited maximum if min is respected
if f . max == 0 && convertedPrice > f . max && f . min <= convertedPrice {
log . Debugf ( "product %s included because max value is unlimited and converted price of %.2f%s is higher than lower limit of %.2f%s" , product . Name , convertedPrice , f . currency , f . min , f . currency )
return true
}
// include prices inside the range
if f . min <= convertedPrice && convertedPrice <= f . max {
log . Debugf ( "product %s included because range filter model matches and converted price is inside of the range" , product . Name )
return true
}
log . Debugf ( "product %s excluded because range filter model matches and converted price is outside of the range" , product . Name )
return false
2021-05-20 18:03:45 +02:00
}