/
battlenet.go
260 lines (225 loc) · 5.51 KB
/
battlenet.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
neturl "net/url"
"strings"
"github.com/ansel1/merry"
"github.com/gosexy/to"
)
var (
appearanceMap = map[string]string{
"faceVariation": "face",
"skinColor": "skin",
"hairVariation": "hair",
"hairColor": "haircolor",
//"featureVariation": ,
//"showHelm": ,
//"showCloak": ,
}
)
func apicall(u *neturl.URL) (*http.Response, error) {
apikeys := []string{
"sehm98r8ss3fddjfs8rwxur435xjav94",
"trtrn4tdhzeruuypctm55a8m5td9z4ce",
"cc7qrdaxn9h47ds39nmgzb9hnutxfsrr",
"cbygdtw624xmfn2hr2sw3ayaeekg9cvy",
"7zsnd2q7npg5bch8hacae7t28xskn8vr",
}
// if val, ok := options["apikey"]; ok {
// apikeys = append([]string{to.String(val)}, apikeys...)
// }
rawQuery := u.RawQuery
if len(rawQuery) > 0 {
rawQuery += "&apikey="
} else {
rawQuery += "apikey="
}
var resp *http.Response
var apierr error
for _, apikey := range apikeys {
u.RawQuery = rawQuery + apikey
resp, apierr = http.Get(u.String())
if apierr == nil {
return resp, nil
}
}
return nil, merry.Wrap(apierr)
}
// Get detailed codes from a character's armory page.
func wowarmory(options map[string]interface{}) (TMorphItems, error) {
u, err := neturl.Parse(to.String(options["url"]))
if err != nil {
return nil, merry.Wrap(err)
}
parts := strings.Split(u.Path, "/")
loc := -1
for x := 0; x < len(parts); x++ {
if parts[x] == "character" {
loc = x + 1
break
}
}
if loc == -1 {
return nil, errors.New("Could not parse battle.net URL")
}
// FIXME: this isn't exactly correct, because you can be in the US and want
// to get the tmorph codes of a person in EU/China. So we need to probably
// have settings to where the user of TMorphGen is.
hostParts := strings.Split(u.Host, ".")
if hostParts[0] == "cn" {
} else if len(hostParts) == 2 {
u.Host = "us.api." + strings.Join(hostParts, ".")
} else {
u.Host = hostParts[0] + ".api." + strings.Join(hostParts[1:], ".")
}
u.Scheme = "https"
u.Path = fmt.Sprintf("/wow/character/%s/%s", parts[loc], parts[loc+1])
u.RawQuery = "fields=items,appearance&locale=en_US"
resp, err := apicall(u)
if err != nil {
return nil, merry.Wrap(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, merry.Wrap(err)
}
resp.Body.Close()
var tmorphItems TMorphItems
var datam map[string]interface{}
err = json.Unmarshal(data, &datam)
if err != nil {
return nil, merry.Wrap(err)
}
// get all armor, weapons, and enchants
items := Map(datam["items"])
for k, v := range items {
if v, ok := v.(map[string]interface{}); ok {
if canDisplayName(k) {
id := to.Int64(v["id"])
tooltipParams := Map(v["tooltipParams"])
if !to.Bool(options["notmog"]) {
transmogItem := tooltipParams["transmogItem"]
if transmogItem != nil {
id = to.Int64(transmogItem)
}
}
tmorphItems = append(tmorphItems, &TMorphItem{
Type: "item",
Args: []int{nameSlotMap[k], int(id)},
})
// get enchants off the weapons
if k == "mainHand" || k == "offHand" {
if tooltipParams["enchant"] != nil {
which := 1
if k == "offHand" {
which = 2
}
tmorphItems = append(tmorphItems, &TMorphItem{
Type: "enchant",
Args: []int{which, int(to.Int64(tooltipParams["enchant"]))},
})
}
}
}
}
}
// set offhand to 0 if there is none.
// TODO: maybe there should be defaults?
if items["offHand"] == nil {
tmorphItems = append(tmorphItems, &TMorphItem{
Type: "item",
Args: []int{nameSlotMap["offHand"], 0},
})
}
// appearance stuff
appearance := Map(datam["appearance"])
for k, v := range appearance {
if typ, ok := appearanceMap[k]; ok {
tmorphItems = append(tmorphItems, &TMorphItem{
Type: typ,
Args: []int{int(to.Int64(v))},
})
}
}
tmorphItems = append(tmorphItems, &TMorphItem{
Type: "race",
Args: []int{int(to.Int64(datam["race"]))},
}, &TMorphItem{
Type: "gender",
Args: []int{int(to.Int64(datam["gender"]))},
})
return tmorphItems, nil
}
// Get the codes for the list of ids via the wow api.
func wowapi(ids []string) (TMorphItems, error) {
var tmorphItems TMorphItems
idslen := len(ids)
errChan := make(chan error)
doneChan := make(chan bool, idslen)
for _, id := range ids {
go func(id string) {
defer func() {
doneChan <- true
}()
contextUrl := ""
REDO:
// FIXME: using US api, ignoring user's location
u, err := neturl.Parse("https://us.api.battle.net/wow/item/" + id + contextUrl)
if err != nil {
errChan <- err
return
}
resp, err := apicall(u)
if err != nil {
errChan <- err
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
errChan <- err
return
}
resp.Body.Close()
var datam map[string]interface{}
err = json.Unmarshal(data, &datam)
if err != nil {
errChan <- err
return
}
if _, ok := datam["inventoryType"]; !ok {
if contexts, ok := datam["availableContexts"]; ok {
ctxs := contexts.([]interface{})
contextUrl = "/" + ctxs[0].(string)
goto REDO
}
}
slot := int(to.Int64(datam["inventoryType"]))
if v, ok := slotMap[slot]; ok {
slot = v
}
if canDisplaySlot(slot) {
tmorphItems = append(tmorphItems, &TMorphItem{
Type: "item",
Args: []int{slot, int(to.Int64(id))},
})
}
}(id)
}
count := 0
for count < idslen {
select {
case err := <-errChan:
return nil, merry.Wrap(err)
case <-doneChan:
count++
if count >= idslen {
return tmorphItems, nil
}
}
}
return nil, nil
}