Add Lua plugin host, built-in amazon plugin and dibs check.
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>
This commit is contained in:
1 parent
f90ef18f90
commit
bb010356a0
28 files changed
+2096
-26
No files matched your search
@@ -0,0 +1,82 @@
|
||||
// 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 ""
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package price
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
for in, want := range map[string]struct {
|
||||
value float64
|
||||
currency string
|
||||
}{
|
||||
"1.299,99 €": {1299.99, "EUR"},
|
||||
"£1,299.99": {1299.99, "GBP"},
|
||||
"1 299,99 €": {1299.99, "EUR"},
|
||||
"1\u00a0299,99\u00a0€": {1299.99, "EUR"},
|
||||
"1\u202f299,99 €": {1299.99, "EUR"},
|
||||
"129,99 €": {129.99, "EUR"},
|
||||
"129,99€": {129.99, "EUR"},
|
||||
"€129.99": {129.99, "EUR"},
|
||||
"$1,299.99": {1299.99, "USD"},
|
||||
"US$ 49.00": {49, "USD"},
|
||||
"EUR 12,50": {12.5, "EUR"},
|
||||
"12.50 GBP": {12.5, "GBP"},
|
||||
"CHF 1'299.90": {1299.9, "CHF"},
|
||||
"1.299 €": {1299, "EUR"},
|
||||
"1,299": {1299, ""},
|
||||
"1.299.000,00 €": {1299000, "EUR"},
|
||||
"12,5": {12.5, ""},
|
||||
"9.99": {9.99, ""},
|
||||
"49": {49, ""},
|
||||
"Preis: 59,99 € inkl. MwSt.": {59.99, "EUR"},
|
||||
" £7. ": {7, "GBP"},
|
||||
"99,00 zł": {99, "PLN"},
|
||||
} {
|
||||
v, c, ok := Parse(in)
|
||||
if !ok || v != want.value || c != want.currency { t.Errorf("Parse(%q) = %v, %q, %v; want %v, %q", in, v, c, ok, want.value, want.currency) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNoPrice(t *testing.T) {
|
||||
for _, in := range []string{"", "Derzeit nicht verfügbar", "€", "kostenlos"} {
|
||||
if v, c, ok := Parse(in); ok { t.Errorf("Parse(%q) = %v, %q, true; want no price", in, v, c) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user