-
Notifications
You must be signed in to change notification settings - Fork 22
/
profile.go
312 lines (255 loc) · 7.92 KB
/
profile.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
package steam
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
const (
PrivacyStatePrivate = 1
PrivacyStateFriendsOnly = 2
PrivacyStatePublic = 3
)
const (
CommentSettingSelf = "commentselfonly"
CommentSettingFriends = "commentfriendsonly"
CommentSettingPublic = "commentanyone"
)
const (
apiGetPlayerSummaries = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?"
apiGetOwnedGames = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?"
apiGetPlayerBans = "https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?"
apiGetPlayerFriends = "https://api.steampowered.com/ISteamUser/GetFriendList/v1/?"
apiResolveVanityURL = "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?"
)
var ErrCannotFindVanityMatch = errors.New("no match for the vanity URL")
type PlayerSummary struct {
SteamID SteamID `json:"steamid,string"`
VisibilityState uint32 `json:"communityvisibilitystate"`
ProfileState uint32 `json:"profilestate"`
PersonaName string `json:"personaname"`
PersonaState uint32 `json:"personastate"`
PersonaStateFlags uint32 `json:"personastateflags"`
RealName string `json:"realname"`
LastLogoff int64 `json:"lastlogoff"`
ProfileURL string `json:"profileurl"`
AvatarURL string `json:"avatar"`
AvatarMediumURL string `json:"avatarmedium"`
AvatarFullURL string `json:"avatarfull"`
PrimaryClanID uint64 `json:"primaryclanid,string"`
TimeCreated int64 `json:"timecreated"`
LocCountryCode string `json:"loccountrycode"`
LocStateCode string `json:"locstatecode"`
LocCityID uint32 `json:"loccityid"`
GameID uint64 `json:"gameid,string"`
GameServerIP string `json:"gameserverip"`
GameExtraInfo string `json:"gameextrainfo"`
}
type Game struct {
AppID uint32 `json:"appid"`
PlaytimeForever int64 `json:"playtime_forever"`
Playtime2Weeks int64 `json:"playtime_2weeks"`
}
type OwnedGamesResponse struct {
Count uint32 `json:"game_count"`
Games []*Game `json:"games"`
}
type PlayerBan struct {
SteamID uint64 `json:"SteamId,string"`
CommunityBanned bool `json:"CommunityBanned"`
VACBanned bool `json:"VACBanned"`
NumberOfVACBans int `json:"NumberOfVACBans"`
DaysSinceLastBan int `json:"DaysSinceLastBan"`
NumberOfGameBans int `json:"NumberOfGameBans"`
EconomyBan string `json:"EconomyBan"`
}
type Friend struct {
SteamID uint64 `json:"steamid,string"`
Relationship string `json:"relationship"`
FriendSince int64 `json:"friend_since"`
}
func (session *Session) GetProfileURL() (string, error) {
tmpClient := http.Client{Jar: session.client.Jar}
/* We do not follow redirect, we want to know where it'd redirect us. */
tmpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("do not redirect")
}
/* Query normal, this will redirect us. */
resp, err := tmpClient.Get("https://steamcommunity.com/my")
if resp == nil {
return "", err
}
resp.Body.Close()
if resp.StatusCode != http.StatusFound {
return "", fmt.Errorf("http error: %d", resp.StatusCode)
}
/* We now have a few useful variables in header, for now, we will just grap "Location". */
return resp.Header.Get("Location"), nil
}
func (session *Session) SetupProfile(profileURL string) error {
resp, err := session.client.Get(profileURL + "/edit?welcomed=1")
if resp != nil {
resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
return nil
}
func (session *Session) SetProfileInfo(profileURL string, values *map[string][]string) error {
(*values)["sessionID"] = []string{session.sessionID}
(*values)["type"] = []string{"profileSave"}
resp, err := session.client.PostForm(profileURL+"/edit", *values)
if resp != nil {
resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
return nil
}
func (session *Session) SetProfilePrivacy(profileURL string, commentPrivacy string, privacy uint8) error {
resp, err := session.client.PostForm(profileURL+"/edit/settings", url.Values{
"sessionID": {session.sessionID},
"type": {"profileSettings"},
"commentSetting": {commentPrivacy},
"privacySetting": {strconv.FormatUint(uint64(privacy&0x3), 10)},
"inventoryPrivacySetting": {strconv.FormatUint(uint64((privacy>>2)&0x3), 10)},
"inventoryGiftPrivacy": {strconv.FormatUint(uint64((privacy>>4)&0x3), 10)},
})
if resp != nil {
resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
return nil
}
func (session *Session) GetPlayerSummaries(steamids string) ([]*PlayerSummary, error) {
resp, err := session.client.Get(apiGetPlayerSummaries + url.Values{
"key": {session.apiKey},
"steamids": {steamids},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Players struct {
Summaries []*PlayerSummary `json:"players"`
}
type Response struct {
Inner Players `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response.Inner.Summaries, nil
}
func (session *Session) GetOwnedGames(sid SteamID, freeGames bool, appInfo bool) (*OwnedGamesResponse, error) {
resp, err := session.client.Get(apiGetOwnedGames + url.Values{
"key": {session.apiKey},
"steamid": {sid.ToString()},
"format": {"json"},
"include_appinfo": {strconv.FormatBool(appInfo)},
"include_played_free_games": {strconv.FormatBool(freeGames)},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Response struct {
Inner *OwnedGamesResponse `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response.Inner, nil
}
func (session *Session) GetPlayerBans(steamids string) ([]*PlayerBan, error) {
resp, err := session.client.Get(apiGetPlayerBans + url.Values{
"key": {session.apiKey},
"steamids": {steamids},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Response struct {
Inner []*PlayerBan `json:"players"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response.Inner, nil
}
func (session *Session) GetFriends(sid SteamID) ([]*Friend, error) {
resp, err := session.client.Get(apiGetPlayerFriends + url.Values{
"key": {session.apiKey},
"steamid": {sid.ToString()},
"format": {"json"},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Friends struct {
Friends []*Friend `json:"friends"`
}
type FriendsList struct {
Inner Friends `json:"friendslist"`
}
var friendsList FriendsList
if err = json.NewDecoder(resp.Body).Decode(&friendsList); err != nil {
return nil, err
}
return friendsList.Inner.Friends, nil
}
func (session *Session) ResolveVanityURL(vanityURL string) (uint64, error) {
resp, err := session.client.Get(apiResolveVanityURL + url.Values{
"key": {session.apiKey},
"vanityurl": {vanityURL},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return 0, err
}
type VanityData struct {
Success uint32 `json:"success"`
SteamID uint64 `json:"steamid,string"`
}
type Response struct {
Inner VanityData `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return 0, err
}
if response.Inner.Success != 1 {
return 0, ErrCannotFindVanityMatch
}
return response.Inner.SteamID, nil
}