feat: add currency code alias

Specifically, CNH -> CNY
This commit is contained in:
Yiyang Kang 2025-01-11 13:48:17 +09:00
parent 5a03b38b32
commit 90dd06676c
Signed by: kkyy
GPG Key ID: 80FD317ECAF06CC3
1 changed files with 11 additions and 3 deletions

View File

@ -15,7 +15,10 @@ import (
)
var (
updateUrl = "https://www.mycurrency.net/=US"
updateUrl = "https://www.mycurrency.net/=US"
codeAliases = map[string]string{
"CNH": "CNY",
}
client = resty.New()
)
@ -63,14 +66,19 @@ func parse(body []byte) (map[string]*Currency, error) {
doc.Find("tr.country").Each(func(_ int, s *goquery.Selection) {
var (
c Currency
code string
isoC iso.ISOCurrency
ok bool
err error
rate string
)
if c.Code, ok = s.Attr("data-currency-code"); !ok {
if code, ok = s.Attr("data-currency-code"); !ok {
return
}
if c.Code, ok = codeAliases[code]; !ok {
c.Code = code
}
if _, exist := ret[c.Code]; exist {
return
}
@ -145,7 +153,7 @@ func (m *MyCurrencyNet) Get(codes ...string) ([]*Currency, error) {
ret[i] = &cp
}
if len(errors) > 0 {
err = fmt.Errorf(strings.Join(errors, ", "))
err = fmt.Errorf("%s", strings.Join(errors, ", "))
}
return ret, err
}