forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 2
/
market.go
296 lines (255 loc) · 7.03 KB
/
market.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
package steam
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
const (
CurrencyUSD = "1"
CurrencyGBP = "2"
CurrencyEUR = "3"
CurrencyCHF = "4"
CurrencyRUB = "5"
CurrencyPLN = "6"
CurrencyBRL = "7"
CurrencyJPY = "8"
CurrencyNOK = "9"
CurrencyIDR = "10"
CurrencyMYR = "11"
CurrencyPHP = "12"
CurrencySGD = "13"
CurrencyTHB = "14"
CurrencyVND = "15"
CurrencyKRW = "16"
CurrencyTRY = "17"
CurrencyUAH = "18"
CurrencyMXN = "19"
CurrencyCAD = "20"
CurrencyAUD = "21"
CurrencyNZD = "22"
CurrencyCNY = "23"
CurrencyINR = "24"
CurrencyCLP = "25"
CurrencyPEN = "26"
CurrencyCOP = "27"
CurrencyZAR = "28"
CurrencyHKD = "29"
CurrencyTWD = "30"
CurrencySAR = "31"
CurrencyAED = "32"
CurrencyARS = "34"
CurrencyILS = "35"
CurrencyBYN = "36"
CurrencyKZT = "37"
CurrencyKWD = "38"
CurrencyQAR = "39"
CurrencyCRC = "40"
CurrencyUYU = "41"
CurrencyRMB = "9000"
)
type MarketItemPriceOverview struct {
Success bool `json:"success"`
LowestPrice string `json:"lowest_price"`
MedianPrice string `json:"median_price"`
Volume string `json:"volume"`
}
type MarketItemPrice struct {
Date string
Price float64
Count string
}
type MarketItemResponse struct {
Success bool `json:"success"`
PricePrefix string `json:"price_prefix"`
PriceSuffix string `json:"price_suffix"`
Prices interface{} `json:"prices"`
}
type MarketSellResponse struct {
Success bool `json:"success"`
RequiresConfirmation uint32 `json:"requires_confirmation"`
MobileConfirmationRequired bool `json:"needs_mobile_confirmation"`
EmailConfirmationRequired bool `json:"needs_email_confirmation"`
EmailDomain string `json:"email_domain"`
}
type MarketBuyOrderResponse struct {
ErrCode int `json:"success"`
ErrMsg string `json:"message"` // Set if ErrCode != 1
OrderID uint64 `json:"buy_orderid,string"`
}
var (
ErrCannotLoadPrices = errors.New("unable to load prices at this time")
//ErrInvalidPriceResponse = errors.New("invalid market pricehistory response")
)
func (session *Session) GetMarketItemPriceHistory(appID uint64, marketHashName string) ([]*MarketItemPrice, error) {
resp, err := session.client.Get("https://steamcommunity.com/market/pricehistory/?" + url.Values{
"appid": {strconv.FormatUint(appID, 10)},
"market_hash_name": {marketHashName},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
response := MarketItemResponse{}
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
if !response.Success {
return nil, ErrCannotLoadPrices
}
var prices []interface{}
var ok bool
if prices, ok = response.Prices.([]interface{}); !ok {
return nil, ErrCannotLoadPrices
}
items := []*MarketItemPrice{}
for _, v := range prices {
if v, ok := v.([]interface{}); ok {
item := &MarketItemPrice{}
for _, val := range v {
switch val := val.(type) {
case string:
if len(item.Date) != 0 {
item.Count = val
} else {
item.Date = val
}
case float64:
item.Price = val
}
}
items = append(items, item)
}
}
return items, nil
}
func (session *Session) GetMarketItemPriceOverview(appID uint64, country, currencyID, marketHashName string) (*MarketItemPriceOverview, error) {
resp, err := session.client.Get("https://steamcommunity.com/market/priceoverview/?" + url.Values{
"appid": {strconv.FormatUint(appID, 10)},
"country": {country},
"currencyID": {currencyID},
"market_hash_name": {marketHashName},
}.Encode())
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
overview := &MarketItemPriceOverview{}
if err = json.NewDecoder(resp.Body).Decode(overview); err != nil {
return nil, err
}
return overview, nil
}
func (session *Session) SellItem(item *InventoryItem, amount, price uint64) (*MarketSellResponse, error) {
req, err := http.NewRequest(
http.MethodPost,
"https://steamcommunity.com/market/sellitem/",
strings.NewReader(url.Values{
"amount": {strconv.FormatUint(amount, 10)},
"appid": {strconv.FormatUint(uint64(item.AppID), 10)},
"assetid": {strconv.FormatUint(item.AssetID, 10)},
"contextid": {strconv.FormatUint(item.ContextID, 10)},
"price": {strconv.FormatUint(price, 10)},
"sessionid": {session.sessionID},
}.Encode()),
)
if err != nil {
return nil, err
}
profileURL, err := session.GetProfileURL()
if err != nil {
return nil, err
}
req.Header.Add("Referer", profileURL+"inventory/")
resp, err := session.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
response := &MarketSellResponse{}
if err = json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}
func (session *Session) PlaceBuyOrder(appid uint64, priceTotal float64, quantity uint64, currencyID, marketHashName string) (*MarketBuyOrderResponse, error) {
req, err := http.NewRequest(
http.MethodPost,
"https://steamcommunity.com/market/createbuyorder/",
strings.NewReader(url.Values{
"appid": {strconv.FormatUint(appid, 10)},
"currency": {currencyID},
"market_hash_name": {marketHashName},
"price_total": {strconv.FormatUint(uint64(priceTotal*100), 10)},
"quantity": {strconv.FormatUint(quantity, 10)},
"sessionid": {session.sessionID},
}.Encode()),
)
if err != nil {
return nil, err
}
var referer string
referer = strings.Replace(marketHashName, " ", "%20", -1)
referer = strings.Replace(referer, "#", "%23", -1)
req.Header.Add(
"Referer",
fmt.Sprintf("https://steamcommunity.com/market/listings/%d/%s", appid, referer),
)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := session.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
response := &MarketBuyOrderResponse{}
if err = json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}
func (session *Session) CancelBuyOrder(orderid uint64) error {
req, err := http.NewRequest(
http.MethodPost,
"https://steamcommunity.com/market/cancelbuyorder/",
strings.NewReader(url.Values{
"sessionid": {session.sessionID},
"buy_orderid": {strconv.FormatUint(orderid, 10)},
}.Encode()),
)
if err != nil {
return err
}
req.Header.Add("Referer", "https://steamcommunity.com/market")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := session.client.Do(req)
if resp != nil {
resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("cannot cancel %d: %d", orderid, resp.StatusCode)
}
return nil
}