steamutils 1.1.16 → 1.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/example.js +100 -10
  2. package/index.js +117 -93
  3. package/package.json +1 -1
package/example.js CHANGED
@@ -1,14 +1,104 @@
1
1
  import SteamUser from "./index.js";
2
- import Steamutils from "./index.js";
2
+ import axios from "axios";
3
+ import * as cherrio from "cheerio";
3
4
 
4
5
  (async function () {
5
- const loginResult = await SteamUser.communityLogin({
6
- username: "abc",
7
- password: "xyz",
8
- })
9
- console.log(loginResult.cookie);
10
- const user = new SteamUser(loginResult.cookie)
11
- const friendList = await user.turningSteamGuardOff()
12
- // const friendList = await user.turningEmailAuthenticatorCheckOn()
13
- console.log(friendList);
6
+ // const loginResult = await SteamUser.communityLogin({
7
+ // username: "abc",
8
+ // password: "xyz",
9
+ // })
10
+ // console.log(loginResult.cookie);
11
+ // const user = new SteamUser(loginResult.cookie)
12
+
13
+
14
+ // let cookies = `timezoneOffset=25200,0; _ga=GA1.2.187887660.1678676715; browserid=2823232875076552481; strInventoryLastContext=730_2; recentlyVisitedAppHubs=730%2C1859270; sessionid=83e614b63d78d0d2b4d93680; steamCountry=VN%7C3c7bd5df99a265a3e5102a225d1ab62d; steamLoginSecure=76561199040402348%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MEQwM18yMjM1ODQ2MF8xM0YyNCIsICJzdWIiOiAiNzY1NjExOTkwNDA0MDIzNDgiLCAiYXVkIjogWyAid2ViIiBdLCAiZXhwIjogMTY4NTA2NTUzMiwgIm5iZiI6IDE2NzYzMzc4MDcsICJpYXQiOiAxNjg0OTc3ODA3LCAianRpIjogIjBEMkRfMjI5NkUxQkNfQzNERjUiLCAib2F0IjogMTY3ODY3NjgzMywgInJ0X2V4cCI6IDE2OTY4NjYxMDEsICJwZXIiOiAwLCAiaXBfc3ViamVjdCI6ICIxNC4yMzIuMjE0LjE4NiIsICJpcF9jb25maXJtZXIiOiAiMTQuMjMyLjIxNC4xODYiIH0.AhLuV9mQzv2j2CEgBe5ZpGyUqpHXogHFClfdsd3Ve7-Dt4aHagrJhOsgLewh1gmkO5ZewAN4FO4Y5S3D7u4FCw; webTradeEligibility=%7B%22allowed%22%3A1%2C%22allowed_at_time%22%3A0%2C%22steamguard_required_days%22%3A15%2C%22new_device_cooldown_days%22%3A0%2C%22time_checked%22%3A1684981543%7D; tsTradeOffersLastRead=1684321053`;
15
+ // const user = new SteamUser("timezoneOffset=25200,0; _ga=GA1.2.187887660.1678676715; browserid=2823232875076552481; strInventoryLastContext=730_2; recentlyVisitedAppHubs=730%2C1859270; sessionid=83e614b63d78d0d2b4d93680; steamCountry=VN%7C3c7bd5df99a265a3e5102a225d1ab62d; steamLoginSecure=76561199040402348%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MEQwM18yMjM1ODQ2MF8xM0YyNCIsICJzdWIiOiAiNzY1NjExOTkwNDA0MDIzNDgiLCAiYXVkIjogWyAid2ViIiBdLCAiZXhwIjogMTY4NTA2NTUzMiwgIm5iZiI6IDE2NzYzMzc4MDcsICJpYXQiOiAxNjg0OTc3ODA3LCAianRpIjogIjBEMkRfMjI5NkUxQkNfQzNERjUiLCAib2F0IjogMTY3ODY3NjgzMywgInJ0X2V4cCI6IDE2OTY4NjYxMDEsICJwZXIiOiAwLCAiaXBfc3ViamVjdCI6ICIxNC4yMzIuMjE0LjE4NiIsICJpcF9jb25maXJtZXIiOiAiMTQuMjMyLjIxNC4xODYiIH0.AhLuV9mQzv2j2CEgBe5ZpGyUqpHXogHFClfdsd3Ve7-Dt4aHagrJhOsgLewh1gmkO5ZewAN4FO4Y5S3D7u4FCw; webTradeEligibility=%7B%22allowed%22%3A1%2C%22allowed_at_time%22%3A0%2C%22steamguard_required_days%22%3A15%2C%22new_device_cooldown_days%22%3A0%2C%22time_checked%22%3A1684981543%7D; tsTradeOffersLastRead=1684321053")
16
+ // const friendList = await user.getTradeURL()
17
+
18
+ function CookieManager(cookie) {
19
+ const _CookieObj = {}
20
+ if (typeof cookie === "string") {
21
+ cookie.split(";").forEach(function (_cookie) {
22
+ setCookie(_cookie.split("=")[0].trim(), _cookie.split("=")[1].trim())
23
+ })
24
+ } else if (Array.isArray(cookie)) {
25
+ cookie.forEach(function (_cookie) {
26
+ if (typeof _cookie === "string") {
27
+ setCookie(_cookie.split("=")[0].trim(), _cookie.split("=")[1].trim())
28
+ } else {
29
+ //to do
30
+ }
31
+ })
32
+ } else if (typeof cookie === "object" && Object.keys(cookie).length) {
33
+ for (const key in cookie) {
34
+ setCookie(key, cookie[key])
35
+ }
36
+ } else {
37
+ //to do
38
+ }
39
+
40
+ function setCookie(name, value) {
41
+ const existedKey = Object.keys(_CookieObj).find(k => k.toLowerCase() === name.toLowerCase())
42
+ if (existedKey) {
43
+ delete _CookieObj[existedKey]
44
+ }
45
+ _CookieObj[name] = value
46
+ }
47
+
48
+ function setCookies(cookies) {
49
+ if (Array.isArray(cookies)) {
50
+ for (let cookie of cookies) {
51
+ if (typeof cookie === "string") {
52
+ cookie = cookie.split(";")[0].trim()
53
+ setCookie(cookie.split("=")[0].trim(), cookie.split("=")[1].trim())
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ function getCookie(name) {
60
+ const existedKey = Object.keys(_CookieObj).find(k => k.toLowerCase() === name.toLowerCase())
61
+ return _CookieObj[existedKey]
62
+ }
63
+
64
+
65
+ function toString() {
66
+ return Object.keys(_CookieObj).map(k => `${k}=${_CookieObj[k]}`).join(';')
67
+ }
68
+
69
+ return {
70
+ setCookie,
71
+ setCookies,
72
+ getCookie,
73
+ toString,
74
+ }
75
+ }
76
+
77
+ const cookiesObj = new CookieManager("steamLoginSecure=76561199490333788%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MEQyRF8yMjhEQTcxQV9DOEZGRCIsICJzdWIiOiAiNzY1NjExOTk0OTAzMzM3ODgiLCAiYXVkIjogWyAid2ViIiwgIm1vYmlsZSIgXSwgImV4cCI6IDE2ODUwNjY0MzgsICJuYmYiOiAxNjc2MzQwMDAwLCAiaWF0IjogMTY4NDk4MDAwMCwgImp0aSI6ICIwRDJCXzIyOTZFMUJGXzUyNEU2IiwgIm9hdCI6IDE2ODQzODUwODksICJydF9leHAiOiAxNzAyNzQzMDU3LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTQuMjMyLjIxNC4xODYiLCAiaXBfY29uZmlybWVyIjogIjE0LjIzMi4yMTQuMTg2IiB9.t884pdEgtcWk8Apy1HrzhQixy0u-RQFM-qb3JuaYMZjCwTRXdCnOZFbCxFe2JesC5xfFttHe4ufqtyizcEmVDA")
78
+
79
+ let url = `my/tradeoffers/privacy`
80
+ while (true) {
81
+ const response = await axios({
82
+ baseURL: "https://steamcommunity.com",
83
+ url,
84
+ validateStatus: () => true,
85
+ headers: {
86
+ cookie: cookiesObj.toString()
87
+ },
88
+ maxRedirects: 0,
89
+ })
90
+ cookiesObj.setCookies(response.headers["set-cookie"])
91
+ if (response.status === 302) {
92
+ url = response.headers.location
93
+ console.log(`Redirect to ${url}`)
94
+ } else if (response.status === 200) {
95
+ const $ = cherrio.load(response.data)
96
+ const trade_offer_access_url = $('#trade_offer_access_url').attr('value')
97
+ console.log(trade_offer_access_url);
98
+ return response
99
+ } else {
100
+ console.log(response.status);
101
+ }
102
+ }
103
+
14
104
  })()
package/index.js CHANGED
@@ -15,6 +15,7 @@ import { getTableHasHeaders, table2json } from './cheerio.js'
15
15
  import { getJSObjectFronXML } from './xml2json.js'
16
16
  import axios from 'axios'
17
17
  import url from 'url'
18
+ import * as cherrio from "cheerio";
18
19
 
19
20
  const MAX_RETRY = 10
20
21
  export const MatchHistoryType = {
@@ -497,6 +498,65 @@ const EdaySinceLastBanRegExp = Object.freeze([
497
498
  '(\\d+) nap az utolsó kitiltás óta'])
498
499
  const SteamImageCDN = ['community.cloudflare.steamstatic.com', 'community.akamai.steamstatic.com'].map(cdn => `https://${cdn}`)
499
500
 
501
+ function CookieManager(cookie) {
502
+ const _CookieObj = {}
503
+ if (typeof cookie === "string") {
504
+ cookie.split(";").forEach(function (_cookie) {
505
+ setCookie(_cookie.split("=")[0].trim(), _cookie.split("=")[1].trim())
506
+ })
507
+ } else if (Array.isArray(cookie)) {
508
+ cookie.forEach(function (_cookie) {
509
+ if (typeof _cookie === "string") {
510
+ setCookie(_cookie.split("=")[0].trim(), _cookie.split("=")[1].trim())
511
+ } else {
512
+ //to do
513
+ }
514
+ })
515
+ } else if (typeof cookie === "object" && Object.keys(cookie).length) {
516
+ for (const key in cookie) {
517
+ setCookie(key, cookie[key])
518
+ }
519
+ } else {
520
+ //to do
521
+ }
522
+
523
+ function setCookie(name, value) {
524
+ const existedKey = Object.keys(_CookieObj).find(k => k.toLowerCase() === name.toLowerCase())
525
+ if (existedKey) {
526
+ delete _CookieObj[existedKey]
527
+ }
528
+ _CookieObj[name] = value
529
+ }
530
+
531
+ function setCookies(cookies) {
532
+ if (Array.isArray(cookies)) {
533
+ for (let cookie of cookies) {
534
+ if (typeof cookie === "string") {
535
+ cookie = cookie.split(";")[0].trim()
536
+ setCookie(cookie.split("=")[0].trim(), cookie.split("=")[1].trim())
537
+ }
538
+ }
539
+ }
540
+ }
541
+
542
+ function getCookie(name) {
543
+ const existedKey = Object.keys(_CookieObj).find(k => k.toLowerCase() === name.toLowerCase())
544
+ return _CookieObj[existedKey]
545
+ }
546
+
547
+
548
+ function toString() {
549
+ return Object.keys(_CookieObj).map(k => `${k}=${_CookieObj[k]}`).join(';')
550
+ }
551
+
552
+ return {
553
+ setCookie,
554
+ setCookies,
555
+ getCookie,
556
+ toString,
557
+ }
558
+ }
559
+
500
560
  class SteamUser {
501
561
  Steam_Language = ELanguage.english
502
562
 
@@ -512,27 +572,8 @@ class SteamUser {
512
572
  Public: 1,
513
573
  })
514
574
 
515
- constructor (cookies, steamMachineAuth) {
516
- this.setCookies(cookies, steamMachineAuth)
517
- }
518
-
519
- setSteamLanguage (language) {
520
- language = language?.toLowerCase()
521
- if (!Object.hasOwn(ELanguage, language)) {
522
- return
523
- }
524
- this.Steam_Language = language
525
- this._cookies = SteamUser.generateCookie({
526
- ...SteamUser.parseCookie(this._cookies),
527
- Steam_Language: this.Steam_Language,
528
- })
529
- }
530
-
531
- setCookies (cookies, _steamMachineAuth) {
532
- if (!cookies) {
533
- return
534
- }
535
- cookies = Array.isArray(cookies) ? cookies.join(';') : cookies
575
+ constructor (cookies) {
576
+ this._cookies = new CookieManager(cookies)
536
577
 
537
578
  let {
538
579
  steamMachineAuth,
@@ -541,25 +582,13 @@ class SteamUser {
541
582
  steamID,
542
583
  miniprofile,
543
584
  sessionid,
544
- } = SteamUser.parseCookie(cookies)
585
+ } = SteamUser.parseCookie(this._cookies.toString())
545
586
 
546
- if (!steamMachineAuth && _steamMachineAuth) {
547
- steamMachineAuth = _steamMachineAuth
548
- }
549
587
  if (!sessionid) {
550
588
  sessionid = SteamUser.generateSessionID()
589
+ this._cookies.setCookie("sessionid", sessionid)
551
590
  }
552
591
 
553
- this._cookies = SteamUser.generateCookie({
554
- steamMachineAuth,
555
- steamLoginSecure,
556
- steamRememberLogin,
557
- steamID,
558
- miniprofile,
559
- sessionid,
560
- Steam_Language: this.Steam_Language,
561
- })
562
-
563
592
  this._sessionid = sessionid
564
593
  this._steamid_user = steamID
565
594
  this._miniprofile_user = miniprofile
@@ -568,6 +597,15 @@ class SteamUser {
568
597
  this._steamRememberLogin = steamRememberLogin
569
598
  }
570
599
 
600
+ setSteamLanguage (language) {
601
+ language = language?.toLowerCase()
602
+ if (!Object.hasOwn(ELanguage, language)) {
603
+ return
604
+ }
605
+ this.Steam_Language = language
606
+ this._cookies.setCookie("Steam_Language", language)
607
+ }
608
+
571
609
  static parseCookie (cookies) {
572
610
  if (!Array.isArray(cookies)) {
573
611
  cookies = cookies.split(';')
@@ -618,7 +656,7 @@ class SteamUser {
618
656
  }
619
657
 
620
658
  getCookies () {
621
- return this._cookies
659
+ return this._cookies.toString()
622
660
  }
623
661
 
624
662
  getSteamidUser () {
@@ -1377,21 +1415,8 @@ class SteamUser {
1377
1415
  }
1378
1416
 
1379
1417
  params.headers.set({
1380
- Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
1381
- 'Accept-Language': 'en-US,en;q=0.9',
1382
- 'Cache-Control': 'no-cache',
1383
- Connection: 'keep-alive',
1384
- Pragma: 'no-cache',
1385
- 'Sec-Fetch-Dest': 'document',
1386
- 'Sec-Fetch-Mode': 'navigate',
1387
- 'Sec-Fetch-Site': 'none',
1388
- 'Sec-Fetch-User': '?1',
1389
- 'Upgrade-Insecure-Requests': '1',
1390
1418
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
1391
- 'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
1392
- 'sec-ch-ua-mobile': '?0',
1393
- 'sec-ch-ua-platform': '"Windows"',
1394
- Cookie: this._cookies,
1419
+ Cookie: this._cookies.toString(),
1395
1420
  })
1396
1421
 
1397
1422
  let referer = params.headers.get('referer')
@@ -1431,8 +1456,6 @@ class SteamUser {
1431
1456
  }
1432
1457
 
1433
1458
  async _httpRequest (params) {
1434
- let result = null,
1435
- i = 0
1436
1459
  params = this._formatHttpRequest(params)
1437
1460
 
1438
1461
  if (!this._cookies) {
@@ -1461,58 +1484,48 @@ class SteamUser {
1461
1484
  if (params.data instanceof URLSearchParams) {
1462
1485
  params.headers.set('content-type', 'application/x-www-form-urlencoded')
1463
1486
  }
1464
-
1465
1487
  }
1466
1488
 
1467
- while (!result && i++ < MAX_RETRY) {
1489
+ let redirectURL = null
1490
+ let response = null
1491
+ let retry = 21
1492
+ while (--retry) {
1468
1493
  const config = {
1469
- baseURL: SteamcommunityURL, ...params,
1494
+ baseURL: SteamcommunityURL,
1495
+ ...params,
1470
1496
  data: typeof params.data === 'object' && !Object.keys(params.data).length ? undefined : params.data,
1471
- headers: params.headers.get(),
1472
- beforeRedirect: (options) => {
1473
- options.headers = options.headers || {}
1474
- if (options?.hostname === 'steamcommunity.com') {
1475
- options.headers.cookie = this._cookies
1476
- }
1477
- if (options.headers.location?.startsWith('https://login.steampowered.com/jwt/refresh')) {
1478
- //should login first
1479
- }
1497
+ validateStatus: () => true,
1498
+ headers: {
1499
+ ...(params.headers.get()),
1500
+ cookie: this._cookies.toString()
1480
1501
  },
1502
+ maxRedirects: 0,
1503
+ url: redirectURL || params.url
1481
1504
  }
1505
+ response = await axios.request(config)
1482
1506
 
1483
- let plsLoginFirst = false
1484
-
1485
- result = await request(config)
1486
- if (result.status === 429) {
1507
+ this._cookies.setCookies(response.headers["set-cookie"])
1508
+ if (response.status === 302) {
1509
+ redirectURL = response.headers.location
1510
+ console.log(`Redirect to ${redirectURL}`)
1511
+ } else if (response.status === 200) {
1512
+ break
1513
+ } else if (response.status === 429) {
1487
1514
  console.log('Too Many Requests')
1488
- await sleep(60000)
1489
- result = await request(config)
1490
- } else if (result.status === 401) {
1515
+ await sleep(30000)
1516
+ break
1517
+ } else if (response.status === 401) {
1491
1518
  console.log('Unauthorized')
1492
1519
  if (params.method.toUpperCase() === 'POST') {
1493
- config.headers['Content-Type'] = 'multipart/form-data'
1494
- result = await request(config)
1495
- }
1496
- }
1497
- if (result?.error?.code === 'ERR_FR_TOO_MANY_REDIRECTS') {
1498
- //Maximum number of redirects exceeded
1499
- plsLoginFirst = true
1500
- }
1501
-
1502
- if (SteamUser._isPlsLoginFirst(result?.data?.data)) {
1503
- plsLoginFirst = true
1504
- }
1505
-
1506
- if (plsLoginFirst) {
1507
- console.error(params.url, `Please login first`)
1508
- return {
1509
- data: null,
1510
- headers: config.headers,
1520
+ config.headers['Content-Type'] = config.headers['content-type'] = 'multipart/form-data'
1521
+ retry = 1
1511
1522
  }
1523
+ break
1524
+ } else {
1525
+ console.log(response.status);
1512
1526
  }
1513
1527
  }
1514
-
1515
- return result
1528
+ return response
1516
1529
  }
1517
1530
 
1518
1531
  async _httpRequestAjax (params) {
@@ -4954,10 +4967,14 @@ class SteamUser {
4954
4967
 
4955
4968
  async getFullInventoryHistory() {
4956
4969
  let result = null
4970
+ const tradehistory = []
4957
4971
  do {
4958
- result = await this.getInventoryHistory(result?.cursor)
4959
- result.tradehistory.forEach(r => console.log(r))
4972
+ result = await this.getInventoryHistory(result?.cursor)
4973
+ if (Array.isArray(result?.tradehistory)) {
4974
+ tradehistory.push(...result.tradehistory)
4975
+ }
4960
4976
  } while (result?.cursor)
4977
+ return tradehistory
4961
4978
  }
4962
4979
  async getInventoryHistory(cursor) {
4963
4980
  if (!cursor) {
@@ -5177,6 +5194,13 @@ class SteamUser {
5177
5194
  },
5178
5195
  ]
5179
5196
  }
5197
+
5198
+ async getTradeURL(){
5199
+ const result = await this._httpRequest(`${this.getMySteamUserProfileURL()}/tradeoffers/privacy`)
5200
+ const $ = cherrio.load(result.data)
5201
+ const trade_offer_access_url = $('#trade_offer_access_url').attr('value')
5202
+ return trade_offer_access_url
5203
+ }
5180
5204
  }
5181
5205
 
5182
5206
  export default SteamUser
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "steamutils",
3
- "version": "1.1.16",
3
+ "version": "1.1.17",
4
4
  "main":"index.js",
5
5
  "dependencies": {
6
6
  "axios": "^1.3.4",