-
Notifications
You must be signed in to change notification settings - Fork 0
/
skinport.go
441 lines (379 loc) · 13.4 KB
/
skinport.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"encoding/json"
"errors"
api2captcha "github.com/2captcha/2captcha-go"
"github.com/gorilla/websocket"
"io/ioutil"
"math"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
)
var defaultGetHeaders = map[string]string{
"accept": "application/json, text/plain, */*",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8,lt;q=0.7",
"cache-control": "no-cache",
"pragma": "no-cache",
"referer": "https://skinport.com/item/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
}
const SKINPORT_IMAGE_URL = "https://community.cloudflare.steamstatic.com/economy/image/class/730/"
const SKINPORT_PURCHASE_URL = "https://skinport.com/item/"
const MANUAL_LOGIN = true
var GBPinUSD float64
var jar, _ = cookiejar.New(nil)
var checkoutClient = &http.Client{
Jar: jar,
}
func ConnectWs() (*websocket.Conn, error) {
InfoLogger.Println("Connecting to SkinPort ws...")
client, _, err := websocket.DefaultDialer.Dial("wss://skinport.com/socket.io/?EIO=4&transport=websocket", nil)
if err != nil {
ReportError(err)
return nil, err
}
client.ReadMessage()
client.WriteMessage(websocket.TextMessage, []byte("40"))
client.ReadMessage()
client.WriteMessage(websocket.TextMessage, []byte("42[\"saleFeedJoin\",{\"appid\":730,\"currency\":\"USD\",\"locale\":\"en\"}]"))
client.ReadMessage()
client.ReadMessage()
client.WriteMessage(websocket.TextMessage, []byte("3"))
return client, nil
}
func RunSkinport() {
client, err := ConnectWs()
if err != nil {
panic(err)
}
attempts := 0
for {
_, message, err := client.ReadMessage()
if err != nil {
if attempts >= 5 {
panic(err)
}
ErrorLogger.Println("Reconnecting to SkinPort ws...")
client.Close()
client, err = ConnectWs()
attempts++
continue
}
attempts = 0
strMessage := string(message)
if strMessage == "2" {
InfoLogger.Println("Ponging SkinPort WS")
client.WriteMessage(websocket.TextMessage, []byte("3"))
} else if strings.Contains(strMessage, "42") {
var response SkinportPayload
strMessage = strings.TrimSuffix(strMessage[14:], "]")
json.Unmarshal([]byte(strMessage), &response)
for _, item := range response.Sales {
InfoLogger.Println("Found product " + item.MarketName)
buffPrice := GetBuffPrice(item.MarketName, item.Version)
numPrice := float64(item.SalePrice) / 100
if (PercentageDifference(numPrice, buffPrice) >= config.MinimumProfitPercentage) && numPrice >= config.MinimumPrice && numPrice <= config.MaximumPrice && !strings.Contains(item.MarketName, "StatTrak") {
SendSkinportProduct(item.MarketName, SKINPORT_IMAGE_URL+item.Classid, item.Link, numPrice, SKINPORT_PURCHASE_URL+item.URL+"/"+strconv.Itoa(item.SaleID), buffPrice, "SkinPort")
gbp := convertToGbp(item.SalePrice)
go addToCart(strconv.Itoa(item.SaleID), int(math.Round(gbp*100)))
}
}
} else {
ReportError(errors.New(strMessage))
}
}
}
func login() error {
checkoutClient.Get("https://skinport.com/")
if !MANUAL_LOGIN {
captcha := generateCaptcha("https://skinport.com/signin")
payload := url.Values{}
payload.Set("email", config.SkinportUsername)
payload.Set("password", config.SkinportPassword)
payload.Set("g-recaptcha-response", captcha)
payload.Set("_csrf", getCsrfToken())
checkoutClient.Get("https://skinport.com/api/home")
loginReq, _ := http.NewRequest(http.MethodPost, "https://skinport.com/api/auth/login", strings.NewReader(payload.Encode()))
loginReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
for header, value := range defaultGetHeaders {
loginReq.Header.Add(header, value)
}
loginResponse, err := checkoutClient.Do(loginReq)
if err != nil {
ReportError(err)
return err
}
body, err := ioutil.ReadAll(loginResponse.Body)
loginResponse.Body.Close()
var loginResponseObject LoginResponse
json.Unmarshal(body, &loginResponseObject)
if !loginResponseObject.Success {
ReportError(errors.New(loginResponseObject.Message))
return errors.New(loginResponseObject.Message)
}
if (loginResponseObject.State == 8) && (loginResponseObject.Key != "") {
authUrl := GetUserInput("Waiting for auth URL (Check Email)...")
authResponse, err := checkoutClient.Get(authUrl)
if err != nil {
ReportError(err)
return err
}
body, err := ioutil.ReadAll(authResponse.Body)
authResponse.Body.Close()
var authResponseObject AuthResponse
json.Unmarshal(body, &authResponseObject)
}
return nil
}
authCookie := GetUserInput("Waiting for connect.sid cookie...")
site, _ := url.Parse("https://skinport.com")
cookie := &http.Cookie{
Name: "connect.sid",
Value: authCookie,
Path: "/",
Domain: ".skinport.com",
}
checkoutClient.Jar.SetCookies(site, []*http.Cookie{cookie})
return nil
}
func getCsrfToken() string {
dataReq, _ := http.NewRequest("GET", "https://skinport.com/api/data?v=939402949c4961a7af31&t="+time.Now().UTC().String(), nil)
for header, value := range defaultGetHeaders {
dataReq.Header.Add(header, value)
}
response, err := checkoutClient.Do(dataReq)
if err != nil {
ReportError(err)
return ""
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if response.StatusCode != 200 {
ErrorLogger.Println("Erroneous response received")
return ""
}
var apiData APIDataResponse
json.Unmarshal(body, &apiData)
GBPinUSD = apiData.Rates.USD
return apiData.Csrf
}
func addToCart(saleId string, price int) error {
payload := url.Values{}
payload.Set("sales[0][id]", saleId)
payload.Set("sales[0][price]", strconv.Itoa(price))
payload.Set("_csrf", getCsrfToken())
atcReq, _ := http.NewRequest(http.MethodPost, "https://skinport.com/api/cart/add", strings.NewReader(payload.Encode()))
atcReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
for header, value := range defaultGetHeaders {
atcReq.Header.Add(header, value)
}
atcResponse, err := checkoutClient.Do(atcReq)
if err != nil {
ReportError(err)
return err
}
body, err := ioutil.ReadAll(atcResponse.Body)
atcResponse.Body.Close()
var atcResponseObject ATCResponse
json.Unmarshal(body, &atcResponseObject)
if !atcResponseObject.Success {
if atcResponseObject.Message == "MUST_LOGIN" {
err = errors.New("Login expired at ATC")
login()
} else if atcResponseObject.Message == "ITEM_NOT_LISTED" {
err = errors.New(saleId + " is now OOS")
} else {
err = errors.New(atcResponseObject.Message)
}
ReportError(err)
return err
}
ReportATC()
return nil
}
// Broken due to updated captcha
/*func submitOrder(saleId string) error {
captcha := generateCaptcha("https://skinport.com/cart")
payload := url.Values{}
payload.Set("sales[0]", saleId)
payload.Set("g-recaptcha-response", captcha)
payload.Set("_csrf", getCsrfToken())
submitReq, _ := http.NewRequest(http.MethodPost, "https://skinport.com/api/checkout/create-order", strings.NewReader(payload.Encode()))
submitReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
for header, value := range defaultGetHeaders {
submitReq.Header.Add(header, value)
}
submitResponse, err := checkoutClient.Do(submitReq)
if err != nil {
ReportError(err)
return err
}
body, err := ioutil.ReadAll(submitResponse.Body)
submitResponse.Body.Close()
return nil
}*/
func convertToGbp(usd int) float64 {
return (float64(usd) / 100) / GBPinUSD
}
func generateCaptcha(url string) string {
captchaClient := api2captcha.NewClient(config.TwoCaptchaKey)
cap := api2captcha.ReCaptcha{
SiteKey: "6Ldo-yEgAAAAAIBUo13yCs0Pjek0XuIKUIS6lHFJ",
Url: url,
Version: "v3",
Score: 0.3,
}
req := cap.ToRequest()
code, err := captchaClient.Solve(req)
if err != nil {
ReportError(err)
}
return code
}
type ATCResponse struct {
RequestID string `json:"requestId"`
Success bool `json:"success"`
Message string `json:"message"`
Notification interface{} `json:"notification"`
}
type AuthResponse struct {
RequestID string `json:"requestId"`
Success bool `json:"success"`
Message string `json:"message"`
User struct {
ID int `json:"id"`
Username string `json:"username"`
Avatar string `json:"avatar"`
SteamAccounts int `json:"steamAccounts"`
Balance int `json:"balance"`
WithdrawAble int `json:"withdrawAble"`
Trusted bool `json:"trusted"`
TwoFactor bool `json:"twoFactor"`
Password bool `json:"password"`
VoucherAccess bool `json:"voucherAccess"`
APIAccess bool `json:"apiAccess"`
BillingAddress bool `json:"billingAddress"`
Affiliate bool `json:"affiliate"`
UnreadNotifications int `json:"unreadNotifications"`
} `json:"user"`
}
type LoginResponse struct {
RequestID string `json:"requestId"`
Success bool `json:"success"`
Message string `json:"message"`
State int `json:"state"`
Email string `json:"email"`
Key string `json:"key"`
}
type APIDataResponse struct {
RequestID string `json:"requestId"`
Success bool `json:"success"`
Message interface{} `json:"message"`
Csrf string `json:"csrf"`
Country string `json:"country"`
Currency string `json:"currency"`
Rate float64 `json:"rate"`
Rates struct {
EUR float64 `json:"EUR"`
DKK float64 `json:"DKK"`
HRK float64 `json:"HRK"`
CZK float64 `json:"CZK"`
NOK float64 `json:"NOK"`
PLN float64 `json:"PLN"`
SEK float64 `json:"SEK"`
USD float64 `json:"USD"`
CAD float64 `json:"CAD"`
CHF float64 `json:"CHF"`
AUD float64 `json:"AUD"`
BRL float64 `json:"BRL"`
GBP int `json:"GBP"`
CNY float64 `json:"CNY"`
RUB float64 `json:"RUB"`
TRY float64 `json:"TRY"`
SGD float64 `json:"SGD"`
NZD float64 `json:"NZD"`
HKD float64 `json:"HKD"`
} `json:"rates"`
Locale string `json:"locale"`
Tags []struct {
Tag string `json:"tag"`
Appid int `json:"appid"`
} `json:"tags"`
Limits struct {
MinOrderValue int `json:"minOrderValue"`
KycTier1PayoutMax int `json:"kycTier1PayoutMax"`
MinSaleValue int `json:"minSaleValue"`
SaleFeeReduced int `json:"saleFeeReduced"`
MaxOrderValue int `json:"maxOrderValue"`
MinPayoutValue int `json:"minPayoutValue"`
KycTier2PayoutMax int `json:"kycTier2PayoutMax"`
} `json:"limits"`
PaymentMethods []string `json:"paymentMethods"`
Following []interface{} `json:"following"`
}
type SkinportPayload struct {
EventType string `json:"eventType"`
Sales []SkinportProduct `json:"sales"`
}
type SkinportProduct struct {
ID int `json:"id"`
SaleID int `json:"saleId"`
ProductID int `json:"productId"`
AssetID int `json:"assetId"`
ItemID int `json:"itemId"`
Appid int `json:"appid"`
Steamid string `json:"steamid"`
URL string `json:"url"`
Family string `json:"family"`
FamilyLocalized string `json:"family_localized"`
Name string `json:"name"`
Title string `json:"title"`
Text string `json:"text"`
MarketName string `json:"marketName"`
MarketHashName string `json:"marketHashName"`
Color string `json:"color"`
BgColor interface{} `json:"bgColor"`
Image string `json:"image"`
Classid string `json:"classid"`
Assetid string `json:"assetid"`
Lock time.Time `json:"lock"`
Version string `json:"version"`
VersionType string `json:"versionType"`
StackAble bool `json:"stackAble"`
SuggestedPrice int `json:"suggestedPrice"`
SalePrice int `json:"salePrice"`
Currency string `json:"currency"`
SaleStatus string `json:"saleStatus"`
SaleType string `json:"saleType"`
Category string `json:"category"`
CategoryLocalized string `json:"category_localized"`
SubCategory string `json:"subCategory"`
SubCategoryLocalized string `json:"subCategory_localized"`
Pattern int `json:"pattern"`
Finish int `json:"finish"`
CustomName interface{} `json:"customName"`
Wear float64 `json:"wear"`
Link string `json:"link"`
Type string `json:"type"`
Exterior string `json:"exterior"`
Quality string `json:"quality"`
Rarity string `json:"rarity"`
RarityLocalized string `json:"rarity_localized"`
RarityColor string `json:"rarityColor"`
Collection interface{} `json:"collection"`
CollectionLocalized interface{} `json:"collection_localized"`
Stickers []interface{} `json:"stickers"`
CanHaveScreenshots bool `json:"canHaveScreenshots"`
Screenshots []interface{} `json:"screenshots"`
Souvenir bool `json:"souvenir"`
Stattrak bool `json:"stattrak"`
Tags []struct {
Name string `json:"name"`
NameLocalized string `json:"name_localized"`
} `json:"tags"`
OwnItem bool `json:"ownItem"`
}