steamutils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/steamutils.iml +12 -0
- package/.idea/vcs.xml +6 -0
- package/axios.js +91 -0
- package/cheerio.js +71 -0
- package/example.js +12 -0
- package/index.js +4065 -0
- package/package.json +16 -0
- package/string.js +96 -0
- package/utils.js +100 -0
- package/xml2json.js +50 -0
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/steamutils.iml" filepath="$PROJECT_DIR$/.idea/steamutils.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="WEB_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager">
|
4
|
+
<content url="file://$MODULE_DIR$">
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
8
|
+
</content>
|
9
|
+
<orderEntry type="inheritedJdk" />
|
10
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
11
|
+
</component>
|
12
|
+
</module>
|
package/.idea/vcs.xml
ADDED
package/axios.js
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
import * as _axios from 'axios'
|
2
|
+
import cheerio from 'cheerio'
|
3
|
+
import {console_log} from './utils.js'
|
4
|
+
|
5
|
+
export class Header {
|
6
|
+
headers = {}
|
7
|
+
|
8
|
+
constructor(headers) {
|
9
|
+
if(typeof headers === 'object' && Object.keys(headers).length) {
|
10
|
+
this.headers = this.validate(headers)
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
validate(headers) {
|
15
|
+
headers = Object.keys(headers).reduce(function (previousValue, currentValue, currentIndex, array) {
|
16
|
+
previousValue[currentValue.toLowerCase()] = headers[currentValue].trim()
|
17
|
+
return previousValue
|
18
|
+
}, {})
|
19
|
+
if(headers.cookie === undefined || headers.cookie === null) {
|
20
|
+
headers.cookie = ''
|
21
|
+
}
|
22
|
+
return headers
|
23
|
+
}
|
24
|
+
|
25
|
+
get(key) {
|
26
|
+
if(key === undefined) {
|
27
|
+
return {...this.headers}
|
28
|
+
}
|
29
|
+
else {
|
30
|
+
return this.headers[key.toLowerCase()]
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
set(key, value) {
|
35
|
+
if(typeof key === 'string') {
|
36
|
+
this.headers[key.toLowerCase()] = value
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
for (let _key in key) {
|
40
|
+
this.headers[_key.toLowerCase()] = key[_key]
|
41
|
+
}
|
42
|
+
}
|
43
|
+
this.headers = this.validate(this.headers)
|
44
|
+
return this
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
const axios = _axios.default.create({
|
49
|
+
baseURL: '/',
|
50
|
+
timeout: 60000,
|
51
|
+
headers: {
|
52
|
+
'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',
|
53
|
+
},
|
54
|
+
validateStatus: () => true,
|
55
|
+
withCredentials: true,
|
56
|
+
maxContentLength: Infinity,
|
57
|
+
maxBodyLength: Infinity,
|
58
|
+
|
59
|
+
})
|
60
|
+
|
61
|
+
//debug
|
62
|
+
axios.interceptors.request.use(function (config) {
|
63
|
+
console_log(`[axios]`, config.method, config.url)
|
64
|
+
return config
|
65
|
+
}, function (error) {
|
66
|
+
return Promise.reject(error)
|
67
|
+
})
|
68
|
+
|
69
|
+
export async function request(config) {
|
70
|
+
try {
|
71
|
+
if(!config?.headers?.cookie) {
|
72
|
+
try {
|
73
|
+
delete config.headers.cookie
|
74
|
+
} catch (e) {
|
75
|
+
}
|
76
|
+
}
|
77
|
+
const result = await axios.request(config)
|
78
|
+
if(result.data && typeof result.data === 'string') {
|
79
|
+
result._$ = function () {
|
80
|
+
return cheerio.load(result.data
|
81
|
+
.replaceAll(/[\t\n\r]/gi, '')
|
82
|
+
.trim())
|
83
|
+
}
|
84
|
+
}
|
85
|
+
return result
|
86
|
+
} catch (e) {
|
87
|
+
return {
|
88
|
+
error: e
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
package/cheerio.js
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
export function tables2json($, tables, getValue) {
|
2
|
+
const tablesData = []
|
3
|
+
$(tables).each(function (index, el) {
|
4
|
+
tablesData.push(table2json($, el, getValue))
|
5
|
+
})
|
6
|
+
return tablesData
|
7
|
+
}
|
8
|
+
|
9
|
+
export function table2json($, table, getValue = function ($, el) {
|
10
|
+
return $(el).text().trim()
|
11
|
+
}) {
|
12
|
+
const data = []
|
13
|
+
const header = []
|
14
|
+
$($(table)[0]).find('tr').each(function (trIndex, trEl) {
|
15
|
+
const tr = $(trEl)
|
16
|
+
const row = []
|
17
|
+
tr.children().each(function (tdIndex, tdEl) {
|
18
|
+
const isHeader = tdEl.name.toLowerCase() === 'th'
|
19
|
+
tdEl = $(tdEl)
|
20
|
+
const tdContent = getValue($, tdEl, isHeader);
|
21
|
+
(isHeader ? header : row).push(tdContent)
|
22
|
+
})
|
23
|
+
if(row.length) {
|
24
|
+
data.push(row)
|
25
|
+
}
|
26
|
+
})
|
27
|
+
|
28
|
+
if(!header.length) return data
|
29
|
+
|
30
|
+
for (let i = 0; i < data.length; i++) {
|
31
|
+
const obj = {}
|
32
|
+
for (let j = 0; j < header.length; j++) {
|
33
|
+
obj[header[j]] = data[i][j]
|
34
|
+
}
|
35
|
+
data[i] = obj
|
36
|
+
}
|
37
|
+
|
38
|
+
return data
|
39
|
+
}
|
40
|
+
|
41
|
+
export function getTableHasHeaders($, $tables, headers = []) {
|
42
|
+
headers = headers.slice()
|
43
|
+
const tables = []
|
44
|
+
$($tables).each((tableIndex, tableEl) => {
|
45
|
+
const _headers = headers.slice()
|
46
|
+
let table
|
47
|
+
if(table) {
|
48
|
+
return
|
49
|
+
}
|
50
|
+
const $tableEl = $(tableEl)
|
51
|
+
$tableEl.find('tr').children().each(function (trIndex, trEl) {
|
52
|
+
if(table) {
|
53
|
+
return
|
54
|
+
}
|
55
|
+
const $trEl = $(trEl)
|
56
|
+
let text = $trEl.text().trim()
|
57
|
+
|
58
|
+
if(_headers.includes(text)) {
|
59
|
+
_headers.splice(_headers.indexOf(text), 1)
|
60
|
+
|
61
|
+
if(!_headers.length) {
|
62
|
+
table = $tableEl
|
63
|
+
}
|
64
|
+
}
|
65
|
+
})
|
66
|
+
if(table) {
|
67
|
+
tables.push(table)
|
68
|
+
}
|
69
|
+
})
|
70
|
+
return tables
|
71
|
+
}
|
package/example.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
import SteamUser from "./index.js";
|
2
|
+
|
3
|
+
(async function () {
|
4
|
+
const loginResult = await SteamUser.communityLogin({
|
5
|
+
username: "abc",
|
6
|
+
password: "xyz",
|
7
|
+
})
|
8
|
+
console.log(loginResult.cookie);
|
9
|
+
const user = new SteamUser(loginResult.cookie)
|
10
|
+
const friendList = await user.getFriendsList()
|
11
|
+
console.log(friendList);
|
12
|
+
})()
|