Sandboxed gopher-lua with time, memory and call-stack limits from [sandbox], a goquery-backed doc API, price parsing, and the 'check', 'plugin test' and 'fixtures' commands. Sellers are first_party, fulfilled (shipped by the shop) or third_party, and items accept fulfilled by default; used-only offers are not available. Plugin tests run on real pages that 'dibs fixtures' fetches into an untracked directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
// Package price reads prices as shops print them.
|
|
package price
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// symbols maps currency signs and local abbreviations to ISO 4217 codes.
|
|
// Ambiguous ones such as "kr" are left out on purpose.
|
|
var symbols = []struct{ sign, code string }{
|
|
{"€", "EUR"},
|
|
{"£", "GBP"},
|
|
{"US$", "USD"},
|
|
{"$", "USD"},
|
|
{"¥", "JPY"},
|
|
{"zł", "PLN"},
|
|
{"Kč", "CZK"},
|
|
{"Fr.", "CHF"},
|
|
}
|
|
|
|
var (
|
|
codeRe = regexp.MustCompile(`\b(EUR|GBP|USD|CHF|PLN|SEK|NOK|DKK|CZK|HUF|JPY|CAD|AUD)\b`)
|
|
numberRe = regexp.MustCompile(`\d[\d.,' \x{00a0}\x{202f}]*`)
|
|
)
|
|
|
|
// Parse reads the first price in s, such as "1.299,99 €", "£1,299.99",
|
|
// "1 299,99 €" or "EUR 12.50". It returns the amount, the ISO 4217
|
|
// currency code if `s` names one (or "" if not), and whether a price was
|
|
// found.
|
|
func Parse(s string) (float64, string, bool) {
|
|
num := strings.TrimRight(numberRe.FindString(s), ".,' \u00a0\u202f")
|
|
if num == "" { return 0, "", false }
|
|
|
|
v, err := strconv.ParseFloat(normalize(num), 64)
|
|
if err != nil { return 0, "", false }
|
|
|
|
return v, currency(s), true
|
|
}
|
|
|
|
// normalize turns a localized number into the form ParseFloat accepts.
|
|
func normalize(num string) string {
|
|
// Spaces and apostrophes only ever group thousands.
|
|
num = strings.NewReplacer(" ", "", "\u00a0", "", "\u202f", "", "'", "").Replace(num)
|
|
|
|
dot, comma := strings.LastIndex(num, "."), strings.LastIndex(num, ",")
|
|
|
|
switch {
|
|
case dot >= 0 && comma >= 0:
|
|
// Both present: the last one is the decimal separator.
|
|
if dot > comma { return strings.ReplaceAll(num, ",", "") }
|
|
|
|
return strings.ReplaceAll(strings.ReplaceAll(num, ".", ""), ",", ".")
|
|
case comma >= 0:
|
|
return decimalOrGrouping(num, ",")
|
|
case dot >= 0:
|
|
return decimalOrGrouping(num, ".")
|
|
default:
|
|
return num
|
|
}
|
|
}
|
|
|
|
// decimalOrGrouping handles a number with only one kind of separator. Used
|
|
// more than once, or followed by exactly three digits, it groups thousands
|
|
// ("1.299", "1,299,000"). Otherwise it is the decimal point ("12,5", "9.99").
|
|
func decimalOrGrouping(num, sep string) string {
|
|
last := num[strings.LastIndex(num, sep)+1:]
|
|
if strings.Count(num, sep) > 1 || len(last) == 3 { return strings.ReplaceAll(num, sep, "") }
|
|
|
|
return strings.Replace(num, sep, ".", 1)
|
|
}
|
|
|
|
func currency(s string) string {
|
|
if m := codeRe.FindString(s); m != "" { return m }
|
|
|
|
for _, sym := range symbols {
|
|
if strings.Contains(s, sym.sign) { return sym.code }
|
|
}
|
|
|
|
return ""
|
|
}
|