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>
This commit is contained in:
parent
ba791435b7
commit
da532104f8
9 changed files with 231 additions and 28 deletions
|
@ -6,15 +6,20 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// DefaultCurrency to fallback when no currency is provided
|
||||
const DefaultCurrency = "USD"
|
||||
|
||||
// RangeFilter to store the pattern to match product model and price limits
|
||||
type RangeFilter struct {
|
||||
model *regexp.Regexp
|
||||
min float64
|
||||
max float64
|
||||
model *regexp.Regexp
|
||||
min float64
|
||||
max float64
|
||||
currency string
|
||||
converter *CurrencyConverter
|
||||
}
|
||||
|
||||
// NewRangeFilter to create a RangeFilter
|
||||
func NewRangeFilter(regex string, min float64, max float64) (*RangeFilter, error) {
|
||||
func NewRangeFilter(regex string, min float64, max float64, currency string, converter *CurrencyConverter) (*RangeFilter, error) {
|
||||
var err error
|
||||
var compiledRegex *regexp.Regexp
|
||||
|
||||
|
@ -26,23 +31,56 @@ func NewRangeFilter(regex string, min float64, max float64) (*RangeFilter, error
|
|||
}
|
||||
}
|
||||
|
||||
var detectedCurrency string
|
||||
if currency != "" {
|
||||
detectedCurrency = currency
|
||||
} else {
|
||||
detectedCurrency = DefaultCurrency
|
||||
}
|
||||
|
||||
return &RangeFilter{
|
||||
model: compiledRegex,
|
||||
min: min,
|
||||
max: max,
|
||||
model: compiledRegex,
|
||||
min: min,
|
||||
max: max,
|
||||
converter: converter,
|
||||
currency: detectedCurrency,
|
||||
}, 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 {
|
||||
// include products with a missing model regex
|
||||
if f.model == nil {
|
||||
log.Debugf("product %s included because range filter model is missing", product.Name)
|
||||
return true
|
||||
}
|
||||
if f.model.MatchString(product.Name) && product.Price < f.min || product.Price > f.max {
|
||||
log.Debugf("product %s excluded because price for the model is outside of the range [%.2f-%.2f]", product.Name, f.min, f.max)
|
||||
return false
|
||||
|
||||
// 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)
|
||||
return true
|
||||
}
|
||||
log.Debugf("product %s included because price range filter is not applicable", product.Name)
|
||||
return true
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
Reference in a new issue