diff --git a/classes/CSteamSharedFile.js b/classes/CSteamSharedFile.js index 277dbb9..9e1b65c 100644 --- a/classes/CSteamSharedFile.js +++ b/classes/CSteamSharedFile.js @@ -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); } diff --git a/components/helpers.js b/components/helpers.js index e4eb5aa..5a97559 100644 --- a/components/helpers.js +++ b/components/helpers.js @@ -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}); - }); - }); -}; \ No newline at end of file diff --git a/index.js b/index.js index d8fe9fe..dc52723 100644 --- a/index.js +++ b/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'); diff --git a/package.json b/package.json index 3c83092..9bc3dca 100644 --- a/package.json +++ b/package.json @@ -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",