forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
206 lines (169 loc) · 5.32 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
package steam
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"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/?"
)
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"`
}
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"`
}
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
}
io.Copy(ioutil.Discard, resp.Body)
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 {
io.Copy(ioutil.Discard, resp.Body)
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 {
io.Copy(ioutil.Discard, resp.Body)
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 {
io.Copy(ioutil.Discard, resp.Body)
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
}