-
Notifications
You must be signed in to change notification settings - Fork 0
/
doviz.go
97 lines (83 loc) · 2.14 KB
/
doviz.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
92
93
94
95
package tcmb
import (
"fmt"
"encoding/xml"
"net/http"
"io/ioutil"
)
type currency struct {
Kod string `xml:"Kod,attr"`
CrossOrder string `xml:"CrossOrder,attr"`
CurrencyCode string `xml:"CurrencyCode,attr"`
Unit string `xml:"Unit"`
Isim string `xml:"Isim"`
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 tarih_Date struct {
XMLName xml.Name `xml:"Tarih_Date"`
Tarih string `xml:"Tarih,attr"`
Date string `xml:"Date,attr"`
BultenNo string `xml:"Bulten_No,attr"`
Currency []currency
}
func GetXML(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("Status error: %v", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("Read body: %v", err)
}
return data, nil
}
func GetCurrencies() tarih_Date{
var result tarih_Date
if xmlBytes, err := GetXML("https://www.tcmb.gov.tr/kurlar/today.xml"); err != nil {
fmt.Printf("Failed to get XML: %v", err)
} else {
xml.Unmarshal(xmlBytes, &result)
// fmt.Printf("%+v\n",result)
}
return result
}
func GetUSD() currency{
usd:=GetCurrenyByName("USD")
return usd
}
func GetCurrenyByName(name string) currency{
currencies:=GetCurrencies()
// usd:=currencies.Currency[0]
var curr currency
for _,c:=range currencies.Currency{
if(c.Kod==name){
fmt.Printf("%+v\n",c)
curr= c
}
}
return curr
}
func (tD *tarih_Date) pretty() string{
out, err:= xml.MarshalIndent(tD, " ", " ")
if err!=nil{
fmt.Printf("error on pretty: %+v\n",err)
}
return string(out)
}
func (c *currency) pretty() string{
out, err:= xml.MarshalIndent(c, " ", " ")
if err!=nil{
fmt.Printf("error on pretty: %+v\n",err)
}
return string(out)
}