steam-profile
Version:
Steam Profile
203 lines (161 loc) • 5.8 kB
JavaScript
const request = require('postman-request');
const uint64 = require('cuint').UINT64;
const cheerio = require('cheerio');
const convert = require('xml-js');
/**
* @Description (Steam Profile)
* @site jezuu.es
* @author chulibyy
* @date 2-12-2019
*/
module.exports = class SteamProfile {
/**
* @constructor
*/
constructor() {
this.name = 'SteamProfile';
this.version = '1.0.3';
}
/**
* @get
* @param {string} profile [SteamID64, CustomID, UrlProfile]
* @return {object}
*/
get(profile) {
if (!profile)
return undefined;
if (!Array.isArray(profile))
profile = profile.replace(/ /g, '').split(',');
let out = [];
return new Promise(async(resolve, reject) => {
await this._asyncForEach(profile, async (element) => {
let bodyXml = await this._requestProfile(element, true);
let bodyhtml = await this._requestProfile(element, false);
let r = await JSON.parse(convert.xml2json(bodyXml, {compact: true}));
if (r && r.profile) {
out.push({
steamID64: r.profile.steamID64 ? r.profile.steamID64._text : undefined,
steamID3: r.profile.steamID64 ? this._getSteamID3(r.profile.steamID64._text) : undefined,
steamID2: r.profile.steamID64 ? this._getSteamID2(r.profile.steamID64._text) : undefined,
steamID: r.profile.steamID ? r.profile.steamID._cdata : undefined,
onlineState: r.profile.onlineState ? r.profile.onlineState._text : undefined,
stateMessage: r.profile.stateMessage ? r.profile.stateMessage._cdata : undefined,
privacyState: r.profile.privacyState ? r.profile.privacyState._text : undefined,
visibilityState: r.profile.visibilityState ? r.profile.visibilityState._text : undefined,
avatarIcon: r.profile.avatarIcon ? r.profile.avatarIcon._cdata : undefined,
avatarMedium: r.profile.avatarMedium ? r.profile.avatarMedium._cdata : undefined,
avatarFull: r.profile.avatarFull ? r.profile.avatarFull._cdata : undefined,
vacBanned: r.profile.vacBanned ? r.profile.vacBanned._text : undefined,
gameBanned: this._getGameBans(bodyhtml),
tradeBanState: r.profile.tradeBanState ? r.profile.tradeBanState._text : undefined,
isLimitedAccount: r.profile.isLimitedAccount ? r.profile.isLimitedAccount._text : undefined,
customURL: r.profile.customURL ? r.profile.customURL._cdata : undefined,
memberSince: r.profile.memberSince ? r.profile.memberSince._text : undefined,
badgeYears: r.profile.memberSince ? this._getBadgeYears(r.profile.memberSince._text) : undefined,
hoursPlayed2Wk: r.profile.hoursPlayed2Wk ? r.profile.hoursPlayed2Wk._text : undefined,
location: r.profile.location ? r.profile.location._cdata : undefined,
realname: r.profile.realname ? r.profile.realname._cdata : undefined,
});
}
});
resolve(out);
});
}
/**
* @_requestProfile
* @param {string} profile [SteamID64, CustomID, UrlProfile]
* @param {bool} xml
* @return {string}
*/
_requestProfile(profile, xml) {
if(!profile)
return undefined;
return new Promise((resolve, reject) => {
request(this._parseUrlProfileXml(profile, xml), (err, res, body) => {
if (err) {
reject(err);
return;
}
if(res.statusCode === 200)
resolve(body);
else
resolve();
});
});
}
/**
* @_parseUrlProfileXml
* @param {string} profile [SteamID64, CustomID, UrlProfile]
* @param {bool} xml
* @return {string}
*/
_parseUrlProfileXml(profile, xml) {
if(!profile)
return undefined;
let strXml = xml ? '?xml=1' : '';
if(/^[-]?\d+$/.test(profile))
return 'https://steamcommunity.com/profiles/' + profile + strXml;
else
if(profile.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g))
return profile + strXml;
else
return 'https://steamcommunity.com/id/' + profile + strXml;
}
/**
* @_getSteamID2
* @param {string} accountid64 [SteamID64]
* @return {string}
*/
_getSteamID2(accountid64) {
if(!accountid64)
return undefined;
let accountid = parseInt(this._getSteamID3(accountid64), 10);
return 'STEAM_' + 0 + ':' + (accountid & 1) + ':' + Math.floor(accountid / 2);
}
/**
* @_getSteamID3
* @param {string} accountid64 [SteamID64]
* @return {string}
*/
_getSteamID3(accountid64) {
if(!accountid64)
return undefined;
return ((new uint64(accountid64, 10).toNumber() & 0xFFFFFFFF) >>> 0).toString();
}
/**
* @_getGameBans
* @param {string} body
* @return {string}
*/
_getGameBans(body) {
const $ = cheerio.load(body, {xmlMode: true});
let profile_ban = $('.profile_ban_status .profile_ban');
let gameBanned = profile_ban.text().replace(/(\r\n|\n|\r|\t)/gm, '').match(/([0-9])[ ]game ban on record?/);
return gameBanned = gameBanned ? gameBanned[1] : '0';
}
/**
* @_getBadgeYears
* @param {string} memberSince [Date]
* @return {string}
*/
_getBadgeYears(memberSince) {
if(!memberSince)
return undefined;
let currentYear = new Date().getFullYear();
let urlImg = 'https://steamcommunity-a.akamaihd.net/public/images/badges/02_years/steamyears@_54.png';
if (memberSince.search(', ') === -1)
return undefined;
return urlImg.replace('@', (currentYear - new Date(memberSince).getFullYear()));
}
/**
* @_asyncForEach async
* @param {array} array []
* @param {function} callback
* @return {callback}
*/
async _asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
};