-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
258 lines (220 loc) 路 11.7 KB
/
index.js
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
/*
* File: index.js
* Project: steamid-resolver
* Created Date: 05.04.2023 19:04:56
* Author: 3urobeat
*
* Last Modified: 30.06.2023 09:58:40
* Modified By: 3urobeat
*
* Copyright (c) 2023 3urobeat <https://github.com/3urobeat>
*
* Licensed under the MIT license: https://opensource.org/licenses/MIT
* Permission is granted to use, copy, modify, and redistribute the work.
* Full license information available in the project LICENSE file.
*/
// Source: https://github.com/3urobeat/node-steamid-resolver
const { _parseParam } = require("./helpers/parseParam.js");
const { _parseXML } = require("./helpers/parseXML.js");
// Type definitions for full information objects
/**
* Full profile information object returned by Steam. Notes: `mostPlayedGames` had 4 elements in testing. `groups` contains full information of the first 3 groups, all following groups only have '$' and 'groupID64' populated.
* @typedef fullProfileInfoObject
* @type {{
* steamID64: [string], steamID: [string], onlineState: [string], stateMessage: [string], privacyState: [string], visibilityState: [string], avatarIcon: [string], avatarMedium: [string], avatarFull: [string], vacBanned: [string],
* tradeBanState: [string], isLimitedAccount: [string], customURL: [string], memberSince: [string], steamRating: [string], hoursPlayed2Wk: [string], headline: [string], location: [string],realname: [string], summary: [string],
* mostPlayedGames: [
* { mostPlayedGame: [
* { gameName: [string], gameLink: [string], gameIcon: [string], gameLogo: [string], gameLogoSmall: [string], hoursPlayed: [string], hoursOnRecord: [string], statsName: [string] },
* { gameName: [string], gameLink: [string], gameIcon: [string], gameLogo: [string], gameLogoSmall: [string], hoursPlayed: [string], hoursOnRecord: [string], statsName: [string] },
* { gameName: [string], gameLink: [string], gameIcon: [string], gameLogo: [string], gameLogoSmall: [string], hoursPlayed: [string], hoursOnRecord: [string], statsName: [string] },
* { gameName: [string], gameLink: [string], gameIcon: [string], gameLogo: [string], gameLogoSmall: [string], hoursPlayed: [string], hoursOnRecord: [string], statsName: [string] }
* ] }
* ],
* groups: [
* { group: array<{ "$": { isPrimary: string }, groupID64: [string], groupName: [string], groupURL: [string], headline: [string], summary: [string], avatarIcon: [string], avatarMedium: [string], avatarFull: [string], memberCount: [string], membersInChat: [string], membersInGame: [string], membersOnline: [string] }> }
* ]
* }}
*/
/**
* Full group information object returned by Steam.
* @typedef fullGroupInfoObject
* @type {{
* groupID64: [string],
* groupDetails: [
* { groupName: [string], groupURL: [string], headline: [string], summary: [string], avatarIcon: [string], avatarMedium: [string], avatarFull: [string], memberCount: [string], membersInChat: [string], membersInGame: [string], membersOnline: [string] }
* ],
* memberCount: [string], totalPages: [string], currentPage: [string], startingMember: [string], nextPageLink: [string],
* members: [
* { steamID64: array<string> }
* ]
* }}
*/
/**
* Get the custom profile url of a user as String by their steamID64 or full URL
* @param {String} steamID64 steamID64 or full URL of the user
* @param {function(string, string)} [callback] Optional: Called with `err` (String) and `customURL` (String) parameters on completion
* @return {Promise.<string>} Resolves with customURL on success or rejects with error on failure
*/
module.exports.steamID64ToCustomUrl = (steamID64, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
steamID64 = _parseParam(steamID64);
_parseXML(`https://steamcommunity.com/profiles/${steamID64}`)
.then(res => {
resolve(res.customURL[0]);
callback(null, res.customURL[0]);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Get the steamID64 of a user as String by their custom profile url or full URL
* @param {String} customID Custom ID or full URL of the user as String
* @param {function(string, string)} [callback] Optional: Called with `err` (String) and `steamID64` (String) parameters on completion
* @return {Promise.<string>} Resolves with steamID64 on success or rejects with error on failure
*/
module.exports.customUrlToSteamID64 = (customID, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
customID = _parseParam(customID);
_parseXML(`https://steamcommunity.com/id/${customID}`)
.then(res => {
resolve(res.steamID64[0]);
callback(null, res.steamID64[0]);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Get the full information of a user as Object by their steamID64 or full URL
* @param {String} steamID64 steamID64 or full URL of the user as String
* @param {function(string, fullProfileInfoObject)} [callback] Optional: Called with `err` (String) and `info` (Object) parameters on completion
* @return {Promise.<fullProfileInfoObject>} Resolves with fullInfo object on success or rejects with error string on failure
*/
module.exports.steamID64ToFullInfo = (steamID64, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
steamID64 = _parseParam(steamID64);
_parseXML(`https://steamcommunity.com/profiles/${steamID64}`)
.then(res => {
resolve(res);
callback(null, res);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Get the full information of a user as Object by their custom profile url or full URL
* @param {String} customID Custom ID or full URL of the user as String
* @param {function(string, fullProfileInfoObject)} [callback] Optional: Called with `err` (String) and `info` (Object) parameters on completion
* @return {Promise.<fullProfileInfoObject>} Resolves with fullInfo object on success or rejects with error string on failure
*/
module.exports.customUrlToFullInfo = (customID, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
customID = _parseParam(customID);
_parseXML(`https://steamcommunity.com/id/${customID}`)
.then(res => {
resolve(res);
callback(null, res);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Get the group64ID of a group as String by groupURL or full URL
* @param {String} groupURL Custom Name of the group or full URL as String
* @param {function(string, string)} [callback] Optional: Called with `err` (String) and `groupID64` (String) parameters on completion
* @returns {Promise.<string>} Resolves with groupID64 on success or rejects with error on failure
*/
module.exports.groupUrlToGroupID64 = (groupURL, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
groupURL = _parseParam(groupURL);
_parseXML(`https://steamcommunity.com/groups/${groupURL}/memberslistxml`)
.then(res => {
resolve(res.groupID64[0]);
callback(null, res.groupID64[0]);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Get the full information of a group as Object by groupURL or full URL
* @param {String} groupURL Custom Name of the group or full URL as String
* @param {function(string, fullGroupInfoObject)} [callback] Optional: Called with `err` (String) and `info` (Object) parameters on completion
* @returns {Promise.<fullGroupInfoObject>} Resolves with fullInfo object on success or rejects with error string on failure
*/
module.exports.groupUrlToFullInfo = (groupURL, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
groupURL = _parseParam(groupURL);
_parseXML(`https://steamcommunity.com/groups/${groupURL}/memberslistxml`)
.then(res => {
resolve(res);
callback(null, res);
})
.catch(err => {
reject(err);
callback(err, null);
});
});
};
/**
* Checks if the provided ID or full URL points to a valid sharedfile
* @param {String} sharedfileID Sharedfile ID or full sharedfile URL
* @param {function(string, boolean)} [callback] Optional: Called with `err` (String) and `isValid` (Boolean) parameters on completion
* @returns {Promise.<boolean>} Resolves with `isValid` boolean on success or rejects with error string on failure
*/
module.exports.isValidSharedfileID = (sharedfileID, callback) => {
return new Promise((resolve, reject) => {
// Hack to support Promises & Callbacks: Resolve errors instead of rejecting them when callback is defined to prevent UnhandledPromiseRejection crash
reject = (!callback || typeof callback !== "function") ? reject : resolve;
callback = callback || (() => {});
// Precede sharedfileID with URL if only the ID was provided
if (!sharedfileID.includes("steamcommunity.com")) sharedfileID = "https://steamcommunity.com/sharedfiles/filedetails/?id=" + sharedfileID;
try {
let output = "";
require("https").get(sharedfileID, (res) => {
res.on("data", (chunk) => output += chunk); // Append each chunk to output
res.on("end", () => {
// Check if output contains an error msg
let isInvalid = output.includes("There was a problem accessing the item") || output.includes("That item does not exist");
resolve(!isInvalid);
callback(null, !isInvalid);
});
});
} catch (err) {
reject(err);
callback(err, null);
}
});
};