mirror of
https://github.com/DoctorMcKay/node-steamcommunity.git
synced 2025-02-20 08:30:51 +08:00
Removed request dependency
This commit is contained in:
parent
c1901d5f55
commit
e9bc778e2a
@ -31,7 +31,7 @@ SteamCommunity.prototype.getSteamSharedFile = function(sharedFileId, callback) {
|
||||
};
|
||||
|
||||
// Get DOM of sharedfile
|
||||
this.httpRequestGet(`https://steamcommunity.com/sharedfiles/filedetails/?id=${sharedFileId}`, (err, res, body) => {
|
||||
this.httpRequestGet(`https://steamcommunity.com/sharedfiles/filedetails/?id=${sharedFileId}`, async (err, res, body) => {
|
||||
try {
|
||||
|
||||
/* --------------------- Preprocess output --------------------- */
|
||||
@ -136,18 +136,10 @@ SteamCommunity.prototype.getSteamSharedFile = function(sharedFileId, callback) {
|
||||
// Find owner profile link, convert to steamID64 using SteamIdResolver lib and create a SteamID object
|
||||
let ownerHref = $(".friendBlockLinkOverlay").attr()["href"];
|
||||
|
||||
Helpers.resolveVanityURL(ownerHref, (err, data) => { // This request takes <1 sec
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
sharedfile.owner = new SteamID(data.steamID);
|
||||
|
||||
// Make callback when ID was resolved as otherwise owner will always be null
|
||||
callback(null, new CSteamSharedFile(this, sharedfile));
|
||||
});
|
||||
let {steamID} = await this._resolveVanityURL(ownerHref);
|
||||
sharedfile.owner = steamID;
|
||||
|
||||
callback(null, new CSteamSharedFile(this, sharedfile));
|
||||
} catch (err) {
|
||||
callback(err, null);
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
const EResult = require('../resources/EResult.js');
|
||||
const request = require('request');
|
||||
const xml2js = require('xml2js');
|
||||
|
||||
/**
|
||||
* Make sure that a provided input is a valid SteamID object.
|
||||
@ -64,41 +62,3 @@ exports.decodeJwt = function(jwt) {
|
||||
|
||||
return JSON.parse(Buffer.from(standardBase64, 'base64').toString('utf8'));
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves a Steam profile URL to get steamID64 and vanityURL
|
||||
* @param {String} url - Full steamcommunity profile URL or only the vanity part.
|
||||
* @param {Object} callback - First argument is null/Error, second is object containing vanityURL (String) and steamID (String)
|
||||
*/
|
||||
exports.resolveVanityURL = function(url, callback) {
|
||||
// Precede url param if only the vanity was provided
|
||||
if (!url.includes("steamcommunity.com")) {
|
||||
url = "https://steamcommunity.com/id/" + url;
|
||||
}
|
||||
|
||||
// Make request to get XML data
|
||||
request(url + "/?xml=1", function(err, response, body) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse XML data returned from Steam into an object
|
||||
new xml2js.Parser().parseString(body, (err, parsed) => {
|
||||
if (err) {
|
||||
callback(new Error("Couldn't parse XML response"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.response && parsed.response.error) {
|
||||
callback(new Error("Couldn't find Steam ID"));
|
||||
return;
|
||||
}
|
||||
|
||||
let steamID64 = parsed.profile.steamID64[0];
|
||||
let vanityURL = parsed.profile.customURL[0];
|
||||
|
||||
callback(null, {"vanityURL": vanityURL, "steamID": steamID64});
|
||||
});
|
||||
});
|
||||
};
|
41
index.js
41
index.js
@ -2,6 +2,7 @@ const {EventEmitter} = require('events');
|
||||
const StdLib = require('@doctormckay/stdlib');
|
||||
const SteamID = require('steamid');
|
||||
const Util = require('util');
|
||||
const xml2js = require('xml2js');
|
||||
|
||||
const Helpers = require('./components/helpers.js');
|
||||
|
||||
@ -360,6 +361,46 @@ SteamCommunity.prototype.getFriendsList = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @return Promise<{vanityURL: string, steamID: SteamID}>
|
||||
* @private
|
||||
*/
|
||||
SteamCommunity.prototype._resolveVanityURL = async function(url) {
|
||||
// Precede url param if only the vanity was provided
|
||||
if (!url.includes('steamcommunity.com')) {
|
||||
url = `https://steamcommunity.com/id/${url}`;
|
||||
}
|
||||
|
||||
// Make request to get XML data
|
||||
let {textBody} = await this._httpRequest({
|
||||
method: 'GET',
|
||||
url,
|
||||
source: 'steamcommunity'
|
||||
});
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
// Parse XML data returned from Steam into an object
|
||||
new xml2js.Parser().parseString(textBody, (err, parsed) => {
|
||||
if (err) {
|
||||
return reject(new Error('Couldn\'t parse XML response'));
|
||||
}
|
||||
|
||||
if (parsed.response && parsed.response.error) {
|
||||
return reject(new Error('Couldn\'t find Steam ID'));
|
||||
}
|
||||
|
||||
let steamID64 = parsed.profile.steamID64[0];
|
||||
let vanityURL = parsed.profile.customURL[0];
|
||||
|
||||
resolve({
|
||||
vanityURL,
|
||||
steamID: new SteamID(steamID64)
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
require('./components/http.js');
|
||||
require('./components/profile.js');
|
||||
require('./components/market.js');
|
||||
|
@ -25,7 +25,6 @@
|
||||
"@doctormckay/stdlib": "^2.6.0",
|
||||
"cheerio": "0.22.0",
|
||||
"image-size": "^0.8.2",
|
||||
"request": "^2.88.0",
|
||||
"steam-session": "^1.2.4",
|
||||
"steam-totp": "^2.1.0",
|
||||
"steamid": "^2.0.0",
|
||||
|
Loading…
Reference in New Issue
Block a user