|
1
|
|
|
package currency |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"io/ioutil" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"strconv" |
|
9
|
|
|
|
|
10
|
|
|
"github.com/FlameInTheDark/dtbot/bot" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
// Data currency structure |
|
14
|
|
|
type Data struct { |
|
15
|
|
|
Date string `json:"Date"` |
|
16
|
|
|
PreviousDate string `json:"PreviousDate"` |
|
17
|
|
|
PreviousURL string `json:"PreviousURL"` |
|
18
|
|
|
Timestamp string `json:"Timestamp"` |
|
19
|
|
|
Currencies map[string]Currency `json:"Valute"` |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// CurrencyCheck returns true if currency is real |
|
23
|
|
|
func (d *Data) CurrencyCheck(currency string) bool { |
|
24
|
|
|
if currency == "RUB" { |
|
25
|
|
|
return true |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if d.Currencies[currency].Value > 0 { |
|
29
|
|
|
return true |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return false |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Currency structure |
|
36
|
|
|
type Currency struct { |
|
37
|
|
|
ID string |
|
38
|
|
|
NumCode string |
|
39
|
|
|
CharCode string |
|
40
|
|
|
Nominal int |
|
41
|
|
|
Name string |
|
42
|
|
|
Value float32 |
|
43
|
|
|
Previous float32 |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// GetCurrency returns string of parsed currency data |
|
47
|
|
|
func GetCurrency(ctx *bot.Context) (response string) { |
|
48
|
|
|
var ( |
|
49
|
|
|
newData Data |
|
50
|
|
|
args = ctx.Conf.Currency.Default |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
if len(ctx.Args) > 0 { |
|
54
|
|
|
args = ctx.Args |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
resp, err := http.Get("https://www.cbr-xml-daily.ru/daily_json.js") |
|
58
|
|
|
if err != nil { |
|
59
|
|
|
response = fmt.Sprintf("API error: %v", err) |
|
60
|
|
|
return |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
bbytes, berr := ioutil.ReadAll(resp.Body) |
|
64
|
|
|
if berr != nil { |
|
65
|
|
|
response = fmt.Sprintf("%v: %v", ctx.Loc("curr_resp_read_err"), berr) |
|
66
|
|
|
return |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
jerr := json.Unmarshal(bbytes, &newData) |
|
70
|
|
|
if jerr != nil { |
|
71
|
|
|
response = fmt.Sprintf("%v: %v", ctx.Loc("curr_resp_parse_err"), jerr) |
|
72
|
|
|
return |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
newData.Currencies["RUB"] = Currency{"R00000", "000", "RUB", 1, "Российский Рубль", 1, 1} |
|
76
|
|
|
|
|
77
|
|
|
response = "" |
|
78
|
|
|
|
|
79
|
|
|
// List of currencies |
|
80
|
|
|
if args[0] == "list" { |
|
81
|
|
|
response = fmt.Sprintf("%v: ", ctx.Loc("available_currencies")) |
|
82
|
|
|
for key := range newData.Currencies { |
|
83
|
|
|
response = fmt.Sprintf("%v %v", response, key) |
|
84
|
|
|
} |
|
85
|
|
|
return |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Converting currencies |
|
89
|
|
|
if len(args) > 3 && args[0] == "conv" { |
|
90
|
|
|
count, err := strconv.ParseFloat(args[2], 64) |
|
91
|
|
|
if err != nil { |
|
92
|
|
|
response = fmt.Sprintf("$v: %v", ctx.Loc("error"), ctx.Loc("nan")) |
|
|
|
|
|
|
93
|
|
|
return |
|
94
|
|
|
} |
|
95
|
|
|
if newData.Currencies[args[1]].Value > 0 && newData.Currencies[args[2]].Value > 0 { |
|
96
|
|
|
converted := (newData.Currencies[args[1]].Value / float32(newData.Currencies[args[1]].Nominal)) * float32(count) |
|
97
|
|
|
response = fmt.Sprintf("`%v %v = %0.2f RUB`\n", args[2], args[1], converted) |
|
98
|
|
|
} |
|
99
|
|
|
return |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
var arrow string |
|
103
|
|
|
// Current currency |
|
104
|
|
|
for _, arg := range args { |
|
105
|
|
|
if newData.Currencies[arg].Value > 0 { |
|
106
|
|
|
if newData.Currencies[arg].Value > newData.Currencies[arg].Previous { |
|
107
|
|
|
arrow = "▲" |
|
108
|
|
|
} else { |
|
109
|
|
|
arrow = "▼" |
|
110
|
|
|
} |
|
111
|
|
|
response = fmt.Sprintf("%v%v\n`%v %v = %v RUB %v %0.2v`\n", |
|
112
|
|
|
response, |
|
113
|
|
|
newData.Currencies[arg].Name, |
|
114
|
|
|
newData.Currencies[arg].Nominal, |
|
115
|
|
|
newData.Currencies[arg].CharCode, |
|
116
|
|
|
newData.Currencies[arg].Value, |
|
117
|
|
|
arrow, |
|
118
|
|
|
newData.Currencies[arg].Value-newData.Currencies[arg].Previous) |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
return |
|
122
|
|
|
} |
|
123
|
|
|
|