Skip to content

Commit

Permalink
Fix for updated div (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanglie committed Aug 1, 2023
1 parent 5fcc12c commit 646723d
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 74 deletions.
8 changes: 4 additions & 4 deletions br.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type Currency string
// City type.
type City string

// Rates by banks and their branches.
type Rates struct {
// Banks or branches.
type Branches struct {
Currency Currency `json:"currency"`
City City `json:"city"`
Branches []Branch `json:"branches"`
Items []Branch `json:"items"`
}

// NewBranch creates a new Branch instance.
Expand All @@ -51,7 +51,7 @@ func (s BySellSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s BySellSorter) Less(i, j int) bool { return s[i].Sell < s[j].Sell }

// String representation of cash currency exchange rates.
func (r *Rates) String() string {
func (r *Branches) String() string {
b, err := json.Marshal(r)
if err != nil {
fmt.Println(err)
Expand Down
10 changes: 5 additions & 5 deletions br_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func TestRates_String(t *testing.T) {
81.64,
time.Date(2023, time.January, 24, 16, 54, 0, 0, loc))

r := &Rates{}
r := &Branches{}
r.Currency = currency
r.City = Novosibirsk
r.Branches = []Branch{b}
r.Items = []Branch{b}

got := r.String()
want := `{"currency":"USD","city":"novosibirsk","branches":[{` +
want := `{"currency":"USD","city":"novosibirsk","items":[{` +
`"bank":"Банк «Открытие»","subway":"м. Октябрьская",` +
`"currency":"USD","buy":79.61,"sell":81.64,"updated":"2023-01-24T16:54:00+03:00"}]}`

Expand All @@ -87,12 +87,12 @@ func TestRates_String(t *testing.T) {
}

// Errors
r.Branches[0].Buy = math.NaN()
r.Items[0].Buy = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}

r.Branches[0].Sell = math.NaN()
r.Items[0].Sell = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}
Expand Down
53 changes: 28 additions & 25 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewClient() *Client {
}

// Rates USDRUB by and city (Moscow, if empty).
func (c *Client) Rates(ct City) (*Rates, error) {
func (c *Client) Rates(ct City) (*Branches, error) {
if len(ct) > 0 {
c.city = ct
}
Expand All @@ -93,12 +93,12 @@ func (c *Client) Rates(ct City) (*Rates, error) {
log.Printf("[DEBUG] Fetching the currency rate from %s", c.buildURL())
}

r := &Rates{Currency: currency, City: ct}
r := &Branches{Currency: currency, City: ct}
b, err := c.parseBranches()
if err != nil {
r = nil
} else {
r.Branches = b
r.Items = b
}

if Debug {
Expand Down Expand Up @@ -142,24 +142,22 @@ func (c *Client) parseBranches() ([]Branch, error) {

// parseBranch parses branch info from the HTML element.
func parseBranch(e *colly.HTMLElement) (Branch, error) {
bank := sanitaze(e.ChildText(".dPnGDN"))

updatedDateString := sanitaze(e.ChildText(".cURBaH"))
if len(updatedDateString) == 0 {
updatedDateString = sanitaze(e.ChildText(".kiIzbr"))
sUpdatedDate := sanitaze(e.ChildText(".hDxmZl"))
if len(sUpdatedDate) == 0 {
return Branch{}, fmt.Errorf("can't find element .hDxmZl")
}

s := strings.Split(updatedDateString, " ")
s := strings.Split(sUpdatedDate, " ")
if count := len(s); count >= 3 {
updatedDateString = strings.Join(s[count-2:], " ")
sUpdatedDate = strings.Join(s[count-2:], " ")
}

loc, err := time.LoadLocation("Europe/Moscow")
if err != nil {
return Branch{}, err
}

updatedDate, err := time.ParseInLocation("02.01.2006 15:04", updatedDateString, loc)
updatedDate, err := time.ParseInLocation("02.01.2006 15:04", sUpdatedDate, loc)
if err != nil {
return Branch{}, err
}
Expand All @@ -168,30 +166,35 @@ func parseBranch(e *colly.HTMLElement) (Branch, error) {
return Branch{}, fmt.Errorf("exchange rate is out of date for 24 hours: %v", updatedDate)
}

ratesString := sanitaze(e.ChildText(".fvORFF"))
if len(ratesString) == 0 {
ratesString = sanitaze(e.ChildText(".guOAzm"))
sRates := sanitaze(e.ChildText(".jzaqdw"))
if len(sRates) == 0 {
return Branch{}, fmt.Errorf("can't find element .jzaqdw")
}

var buyRateString, sellRateString string
if s := strings.Split(ratesString, "₽"); len(s) >= 2 {
buyRateString = s[0]
sellRateString = s[1]
var sBuyRate, sSellRate string
if s := strings.Split(sRates, "₽"); len(s) >= 2 {
sBuyRate = s[0]
sSellRate = s[1]
}

buyRateString = strings.Replace(buyRateString, " ", "", -1)
buyRate, err := strconv.ParseFloat(strings.ReplaceAll(buyRateString, ",", "."), 64)
if err != nil || buyRate <= 0 {
sBuyRate = strings.Replace(sBuyRate, " ", "", -1)
buyRate, err := strconv.ParseFloat(strings.ReplaceAll(sBuyRate, ",", "."), 64)
if err != nil {
return Branch{}, err
}

sellRateString = strings.Replace(sellRateString, " ", "", -1)
sellRate, err := strconv.ParseFloat(strings.ReplaceAll(sellRateString, ",", "."), 64)
if err != nil || sellRate <= 0 {
if buyRate <= 0 {
return Branch{}, fmt.Errorf("buy rate is zero or less: %v", buyRate)
}

sSellRate = strings.Replace(sSellRate, " ", "", -1)
sellRate, err := strconv.ParseFloat(strings.ReplaceAll(sSellRate, ",", "."), 64)
if err != nil {
return Branch{}, err
}

subway := sanitaze(e.ChildText(".eybsgm"))
bank := sanitaze(e.ChildText(".gfTHqP"))
subway := sanitaze(e.ChildText(".dJGHYE"))

return newBranch(bank, subway, currency, buyRate, sellRate, updatedDate), nil
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestClient_Rates(t *testing.T) {
t.Error(err)
}

b := r.Branches
b := r.Items
if len(b) == 0 {
t.Error("b is empty")
}
Expand Down
Loading

0 comments on commit 646723d

Please sign in to comment.