-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcmbgo.go
91 lines (78 loc) · 2.34 KB
/
tcmbgo.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
88
89
90
91
package tcmbgo
import (
"encoding/xml"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
)
// XML Struct
type tarihDate struct {
XMLname xml.Name `xml:"Tarih_Date"`
Date string `xml:"Date,attr"`
Currency []currency
}
type currency struct {
Kod string `xml:"Kod,attr"`
CurrencyCode string `xml:"CurrencyCode,attr"`
CurrencyName string `xml:"CurrencyName"`
ForexBuying string `xml:"ForexBuying"`
ForexSelling string `xml:"ForexSelling"`
BanknoteBuying string `xml:"BanknoteBuying"`
BanknoteSelling string `xml:"BanknoteSelling"`
CrossRateUSD string `xml:"CrossRateUSD"`
CrossRateOther string `xml:"CrossRateOther"`
}
type ExchangeRates struct {
Date string
Currency []Currencies
}
type Currencies struct {
CurrencyCode string
Name string
ForexBuying float64
ForexSelling float64
BanknoteBuying float64
BanknoteSelling float64
CrossRateUSD float64
CrossRateOther float64
}
const url = "https://www.tcmb.gov.tr/kurlar/"
// Get data from TCMB using timestamp.
func GetData(timestamp int64) *ExchangeRates {
date := time.Unix(timestamp, 0)
fDate := date.Format("2006.01.02")
dateS := strings.Split(fDate, ".")
fUrl := url + dateS[0] + dateS[1] + "/" + dateS[2] + dateS[1] + dateS[0] + ".xml"
res, err := http.Get(fUrl)
if err != nil {
log.Fatal(err)
}
body, _ := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
log.Fatalf("Response failed code: %d", res.StatusCode)
}
data := parseBody(body)
return data
}
func parseBody(body []byte) *ExchangeRates {
var tarihDate tarihDate
xml.Unmarshal(body, &tarihDate)
format := new(ExchangeRates)
format.Date = tarihDate.Date
format.Currency = make([]Currencies, len(tarihDate.Currency))
for i, c := range tarihDate.Currency {
format.Currency[i].CurrencyCode = c.CurrencyCode
format.Currency[i].Name = c.CurrencyName
format.Currency[i].ForexBuying, _ = strconv.ParseFloat(c.ForexBuying, 64)
format.Currency[i].ForexSelling, _ = strconv.ParseFloat(c.ForexSelling, 64)
format.Currency[i].BanknoteBuying, _ = strconv.ParseFloat(c.BanknoteBuying, 64)
format.Currency[i].BanknoteSelling, _ = strconv.ParseFloat(c.BanknoteSelling, 64)
format.Currency[i].CrossRateUSD, _ = strconv.ParseFloat(c.CrossRateUSD, 64)
format.Currency[i].CrossRateOther, _ = strconv.ParseFloat(c.CrossRateOther, 64)
}
return format
}