-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcbr.go
87 lines (77 loc) · 1.98 KB
/
cbr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cbr
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
"golang.org/x/text/encoding/charmap"
)
const dateFormat = "02/01/2006"
const baseURL = "http://www.cbr.ru/scripts/XML_daily.asp"
// Currency is a currency item
type Currency struct {
ID string `xml:"ID,attr"`
NumCode uint `xml:"NumCode"`
CharCode string `xml:"CharCode"`
Nom uint `xml:"Nominal"`
Name string `xml:"Name"`
Value string `xml:"Value"`
}
// Result is a result representation
type Result struct {
XMLName xml.Name `xml:"ValCurs"`
Date string `xml:"Date,attr"`
Currencies []Currency `xml:"Valute"`
}
func getCurrencyRate(currency string, t time.Time, fetch FetchFunction) (float64, error) {
log.Printf("Fetching the currency rate for %s at %v\n", currency, t.Format("02.01.2006"))
var result Result
err := getCurrencies(&result, t, fetch)
if err != nil {
return 0, err
}
for _, v := range result.Currencies {
if v.CharCode == currency {
return getCurrencyRateValue(v)
}
}
return 0, fmt.Errorf("Unknown currency: %s", currency)
}
func getCurrencyRateValue(v Currency) (float64, error) {
properFormattedValue := strings.Replace(v.Value, ",", ".", -1)
floatValue, err := strconv.ParseFloat(properFormattedValue, 64)
if err != nil {
return 0, err
}
return floatValue / float64(v.Nom), nil
}
func getCurrencies(v *Result, t time.Time, fetch FetchFunction) error {
url := baseURL + "?date_req=" + t.Format(dateFormat)
resp, err := fetch(url)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
decoder := xml.NewDecoder(bytes.NewReader(body))
decoder.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
switch charset {
case "windows-1251":
return charmap.Windows1251.NewDecoder().Reader(input), nil
default:
return nil, fmt.Errorf("Unknown charset: %s", charset)
}
}
err = decoder.Decode(&v)
if err != nil {
return err
}
return nil
}