Add lbToKg pp action to the scraper (#1337)

This commit is contained in:
bnkai
2021-04-26 06:31:25 +03:00
committed by GitHub
parent 2eb2d865dc
commit aedadc3857
4 changed files with 44 additions and 6 deletions

View File

@@ -462,12 +462,25 @@ func (p *postProcessFeetToCm) Apply(value string, q mappedQuery) string {
return strconv.Itoa(int(math.Round(centimeters)))
}
type postProcessLbToKg bool
func (p *postProcessLbToKg) Apply(value string, q mappedQuery) string {
const lb_in_kg = 0.45359237
w, err := strconv.ParseFloat(value, 64)
if err == nil {
w = w * lb_in_kg
value = strconv.Itoa(int(math.Round(w)))
}
return value
}
type mappedPostProcessAction struct {
ParseDate string `yaml:"parseDate"`
Replace mappedRegexConfigs `yaml:"replace"`
SubScraper *mappedScraperAttrConfig `yaml:"subScraper"`
Map map[string]string `yaml:"map"`
FeetToCm bool `yaml:"feetToCm"`
LbToKg bool `yaml:"lbToKg"`
}
func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error) {
@@ -511,6 +524,14 @@ func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error
action := postProcessFeetToCm(a.FeetToCm)
ret = &action
}
if a.LbToKg {
if found != "" {
return nil, fmt.Errorf("post-process actions must have a single field, found %s and %s", found, "lbToKg")
}
found = "lbToKg"
action := postProcessLbToKg(a.LbToKg)
ret = &action
}
if ret == nil {
return nil, errors.New("invalid post-process action")