Improve resolveVanityURL() and move to helpers

This commit is contained in:
3urobeat 2023-05-15 22:27:37 +02:00
parent 897ad16154
commit 4723bd93a6
No known key found for this signature in database
GPG Key ID: 6842D80775A4E952
2 changed files with 42 additions and 17 deletions

View File

@ -1,4 +1,6 @@
const EResult = require('../resources/EResult.js'); const EResult = require('../resources/EResult.js');
const request = require('request');
const xml2js = require('xml2js');
exports.isSteamID = function(input) { exports.isSteamID = function(input) {
var keys = Object.keys(input); var keys = Object.keys(input);
@ -54,3 +56,41 @@ exports.eresultError = function(eresult) {
err.eresult = eresult; err.eresult = eresult;
return err; return err;
}; };
/**
* 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;
let vanityURL = parsed.profile.customURL;
callback(null, {"vanityURL": vanityURL, "steamID": steamID64});
});
});
};

View File

@ -1,5 +1,6 @@
var SteamCommunity = require('../index.js'); var SteamCommunity = require('../index.js');
var CEconItem = require('../classes/CEconItem.js'); var CEconItem = require('../classes/CEconItem.js');
var Helpers = require('./helpers.js');
var SteamID = require('steamid'); var SteamID = require('steamid');
var request = require('request'); var request = require('request');
var Cheerio = require('cheerio'); var Cheerio = require('cheerio');
@ -142,7 +143,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
} }
if (options.resolveVanityURLs) { if (options.resolveVanityURLs) {
Async.map(vanityURLs, resolveVanityURL, function(err, results) { Async.map(vanityURLs, Helpers.resolveVanityURL, function(err, results) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -170,19 +171,3 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
}, "steamcommunity"); }, "steamcommunity");
}; };
function resolveVanityURL(vanityURL, callback) {
request("https://steamcommunity.com/id/" + vanityURL + "/?xml=1", function(err, response, body) {
if (err) {
callback(err);
return;
}
var match = body.match(/<steamID64>(\d+)<\/steamID64>/);
if (!match || !match[1]) {
callback(new Error("Couldn't find Steam ID"));
return;
}
callback(null, {"vanityURL": vanityURL, "steamID": match[1]});
});
}