Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/parser.go
Julien Riou ab5abcd171
Select or create shop before parsing
A shop map was created to group URLs by shops and process them in order. Now
that we have Amazon and each URL can be parsed independently, there is no need
to group them anymore. Moreover, shops were passed as an argument to the
handleProducts function. Shop name can be deduced by the parser itself. The
parser has a reference to the database. The parser now select or create the shop
before parsing products.

Signed-off-by: Julien Riou <julien@riou.xyz>
2021-04-01 17:50:50 +02:00

48 lines
1.4 KiB
Go

package main
import (
"regexp"
log "github.com/sirupsen/logrus"
)
// Parser interface to parse an external service and return a list of products
type Parser interface {
Parse() ([]*Product, error)
String() string
ShopName() (string, error)
}
// filterInclusive returns a list of products matching the include regex
func filterInclusive(includeRegex *regexp.Regexp, products []*Product) []*Product {
var filtered []*Product
if includeRegex != nil {
for _, product := range products {
if includeRegex.MatchString(product.Name) {
log.Debugf("product %s included because it matches the include regex", product.Name)
filtered = append(filtered, product)
} else {
log.Debugf("product %s excluded because it does not match the include regex", product.Name)
}
}
return filtered
}
return products
}
// filterExclusive returns a list of products that don't match the exclude regex
func filterExclusive(excludeRegex *regexp.Regexp, products []*Product) []*Product {
var filtered []*Product
if excludeRegex != nil {
for _, product := range products {
if excludeRegex.MatchString(product.Name) {
log.Debugf("product %s excluded because it matches the exclude regex", product.Name)
} else {
log.Debugf("product %s included because it does not match the exclude regex", product.Name)
filtered = append(filtered, product)
}
}
return filtered
}
return products
}