-
Notifications
You must be signed in to change notification settings - Fork 1
/
steam.go
341 lines (266 loc) · 6.83 KB
/
steam.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package scraper
import (
"encoding/json"
"errors"
"strconv"
"strings"
"time"
)
type PersonaState int
const (
PersonaStateOffline PersonaState = 0
PersonaStateOnline PersonaState = 1
PersonaStateBusy PersonaState = 2
PersonaStateAway PersonaState = 3
PersonaStateSnooze PersonaState = 4
PersonaStateLTTrade PersonaState = 5
PersonaStateLTPlay PersonaState = 6
)
var PersonaStatesStr = [...]string{
"Offline",
"Online",
"Busy",
"Away",
"Snooze",
"Looking to Trade",
"Looking to Play",
}
func (ps *PersonaState) String() string {
return PersonaStatesStr[int(*ps)]
}
type PlayerInfo struct {
// name
Name string
Personaname string
Realname string
// profile
Profileurl string
Personastate PersonaState
Profilestate int
Commentpermission string
// steam
Steamid string
Communityvisibilitystate int
Visibility string
// time creaTed
Timecreated int
// location
Loccountrycode string
Locstatecode string
Loccityid int
// logoff
Lastlogoff int
// avatar
Avatar string
Avatarfull string
Avatarmedium string
}
// get player "timecreated" as Time
func (p *PlayerInfo) GetTimeCreated() time.Time {
return time.Unix(int64(p.Timecreated), 0)
}
// get player "lastlogoff" as Time
func (p *PlayerInfo) GetLogoffTime() time.Time {
return time.Unix(int64(p.Lastlogoff), 0)
}
func tryParsingString(arg interface{}) string {
res, ok := arg.(string)
if !ok {
return ""
}
return res
}
func tryParsingNumber(arg interface{}) (int, error) {
json, ok := arg.(json.Number)
if !ok {
return 0, errors.New("Not a json number")
}
res, err := json.Int64()
if err != nil {
return 0, err
}
return int(res), nil
}
// parse player json
func (p *PlayerInfo) Parse(elem map[string]interface{}) error {
p.Steamid = elem["steamid"].(string)
p.Profileurl = elem["profileurl"].(string)
// profile visibility
visibility := elem["communityvisibilitystate"].(json.Number).String()
var vState string
switch {
case visibility == "1":
vState = "private"
case visibility == "3":
vState = "public"
}
// same as communityvisibilitystate
// but as string (public or private)
p.Visibility = vState
playerV, vErr := strconv.Atoi(visibility)
if vErr != nil {
return vErr
}
// int as str
p.Communityvisibilitystate = playerV
// Logoff
playerL, pErr := tryParsingNumber(elem["lastlogoff"])
p.Lastlogoff = playerL
if pErr != nil {
p.Lastlogoff = -1
}
// if the account has a steam community profile set then this should be 1
profileState, stErr := tryParsingNumber(elem["profilestate"])
if stErr != nil {
return stErr
}
p.Profilestate = profileState
// variables that are only available when
// the profile visibility is set to public
if vState == "public" {
p.Realname = tryParsingString(elem["realname"])
// user is online, offline...
playerState, pstErr := tryParsingNumber(elem["personastate"])
if pstErr != nil {
return pstErr
}
p.Personastate = PersonaState(playerState)
// timecreated
playerTC, tcErr := tryParsingNumber(elem["timecreated"])
if tcErr != nil {
return tcErr
}
p.Timecreated = playerTC
// location
p.Loccountrycode = tryParsingString(elem["loccountrycode"])
p.Locstatecode = tryParsingString(elem["locstatecode"])
cityId, cErr := tryParsingNumber(elem["loccityid"])
if cErr != nil {
cityId = 0
}
p.Loccityid = cityId
}
// avatar
p.Avatar = elem["avatar"].(string)
p.Avatarfull = elem["avatarfull"].(string)
p.Avatarmedium = elem["avatarmedium"].(string)
// name
p.Personaname = elem["personaname"].(string)
p.Name = p.Personaname // alias
return nil
}
var steamApiKey string
var ErrNoGamesOwned = errors.New("No Steam game in player's profile.")
func SetSteamApiKey(key string) {
steamApiKey = key
}
func GetTF2Hours(steamid string) (int, error) {
games, err := GetSteamGamesOwned(steamid)
if err != nil {
if err == ErrNoGamesOwned {
return 0, nil
}
return 0, err
}
return GetTF2HoursFromGamesOwned(games), nil
}
func GetTF2HoursFromGamesOwned(res *map[string]string) int {
hours, ok := (*res)["440"]
if !ok {
return 0
}
num, err := strconv.ParseInt(hours, 10, 32)
if err != nil {
return 0
}
return int(num) / 60
}
func GetSteamGamesOwned(steamid string) (*map[string]string, error) {
url := "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" +
steamApiKey + "&steamid=" + steamid + "&include_played_free_games=1&format=json1"
response, err := getJsonFromUrl(url)
if err != nil {
return nil, err
}
res := make(map[string]string)
ps, err := response.Get("response").Get("games").Array()
count, _ := response.Get("response").Get("game_count").Int()
if err != nil && count == 0 {
return nil, ErrNoGamesOwned
}
for _, _elem := range ps {
elem := _elem.(map[string]interface{})
name := elem["appid"].(json.Number).String()
value := elem["playtime_forever"].(json.Number).String()
res[name] = value
}
return &res, nil
}
func GetTF2Stats(steamid string) (*map[string]string, error) {
url := "http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=440&key=" +
steamApiKey + "&steamid=" + steamid
res := make(map[string]string)
response, err := getJsonFromUrl(url)
if err != nil {
return nil, err
}
ps, err := response.Get("playerstats").Get("stats").Array()
if err != nil {
return nil, err
}
for _, _elem := range ps {
elem := _elem.(map[string]interface{})
name, _ := elem["name"].(string)
value := elem["value"].(json.Number).String()
res[name] = value
}
if err != nil {
return nil, err
}
return &res, nil
}
// https://developer.valvesoftware.com/wiki/Steam_Web_API#GetPlayerSummaries_.28v0002.29
func GetPlayersInfo(steamids []string) (map[string]*PlayerInfo, error) {
url := "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" +
steamApiKey + "&steamids=" + strings.Join(steamids, ",")
profiles := make(map[string]*PlayerInfo)
response, err := getJsonFromUrl(url)
if err != nil {
return nil, err
}
ps, err := response.Get("response").Get("players").Array()
if err != nil {
return nil, err
}
for _, _elem := range ps {
elem := _elem.(map[string]interface{})
player := new(PlayerInfo)
pErr := player.Parse(elem)
if pErr != nil {
return nil, pErr
}
profiles[player.Steamid] = player
}
return profiles, nil
}
func GetPlayerInfo(steamid string) (*PlayerInfo, error) {
url := "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" +
steamApiKey + "&steamids=" + steamid
player := new(PlayerInfo)
response, err := getJsonFromUrl(url)
if err != nil {
return nil, err
}
ps, err := response.Get("response").Get("players").Array()
if err != nil {
return nil, err
}
if len(ps) == 1 {
elem := ps[0].(map[string]interface{})
pErr := player.Parse(elem)
if pErr != nil {
return nil, pErr
}
}
return player, nil
}