From 90dd06676ce26298b92f4abbd2c4fc85b102aa28 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Sat, 11 Jan 2025 13:48:17 +0900 Subject: [PATCH] feat: add currency code alias Specifically, CNH -> CNY --- mycurrencynet.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mycurrencynet.go b/mycurrencynet.go index e4cd50f..6e42c1a 100644 --- a/mycurrencynet.go +++ b/mycurrencynet.go @@ -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 }