Formatting

This commit is contained in:
3urobeat
2023-05-14 18:52:16 +02:00
parent 81d8dc2a5c
commit 897ad16154
3 changed files with 236 additions and 214 deletions

View File

@@ -1,9 +1,9 @@
const Cheerio = require("cheerio");
const SteamID = require("steamid");
const Cheerio = require('cheerio');
const SteamID = require('steamid');
const Helpers = require('../components/helpers.js');
const SteamCommunity = require("../index.js");
const SteamIdResolver = require("steamid-resolver");
const ESharedfileType = require("../resources/ESharedfileType.js");
const SteamCommunity = require('../index.js');
const SteamIdResolver = require('steamid-resolver');
const ESharedfileType = require('../resources/ESharedfileType.js');
/**
@@ -12,6 +12,7 @@ const ESharedfileType = require("../resources/ESharedfileType.js");
* @param {function} callback - First argument is null/Error, second is object containing all available information
*/
SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
// Construct object holding all the data we can scrape
let sharedfile = {
id: sid,
@@ -24,7 +25,7 @@ SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
uniqueVisitorsCount: null,
favoritesCount: null,
upvoteCount: null
}
};
// Get DOM of sharedfile
@@ -42,7 +43,9 @@ SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
let detailsRight = $(".detailsStatsContainerRight").children();
Object.keys(detailsLeft).forEach((e) => { // Dynamically get all details. Don't hardcore so that this also works for guides.
if (isNaN(e)) return; // Ignore invalid entries
if (isNaN(e)) {
return; // Ignore invalid entries
}
detailsStatsObj[detailsLeft[e].children[0].data.trim()] = detailsRight[e].children[0].data;
});
@@ -51,8 +54,10 @@ SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
let statsTableObj = {};
let statsTable = $(".stats_table").children();
Object.keys(statsTable).forEach((e, i) => {
if (isNaN(e)) return; // Ignore invalid entries
Object.keys(statsTable).forEach((e) => {
if (isNaN(e)) {
return; // Ignore invalid entries
}
// Value description is at index 3, value data at index 1
statsTableObj[statsTable[e].children[3].children[0].data] = statsTable[e].children[1].children[0].data.replace(/,/g, ""); // Remove commas from 1k+ values
@@ -62,7 +67,7 @@ SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
/* --------------------- Find and map values --------------------- */
// Find appID in share button onclick event
sharedfile.appID = Number($("#ShareItemBtn").attr()["onclick"].replace(`ShowSharePublishedFilePopup( '${sid}', '`, "").replace("' );", ""))
sharedfile.appID = Number($("#ShareItemBtn").attr()["onclick"].replace(`ShowSharePublishedFilePopup( '${sid}', '`, "").replace("' );", ""));
// Find fileSize if not guide
@@ -80,31 +85,48 @@ SteamCommunity.prototype.getSteamSharedfile = function(sid, callback) {
// Find uniqueVisitorsCount. We can't use ' || null' here as Number("0") casts to false
if (statsTableObj["Unique Visitors"]) sharedfile.uniqueVisitorsCount = Number(statsTableObj["Unique Visitors"]);
if (statsTableObj["Unique Visitors"]) {
sharedfile.uniqueVisitorsCount = Number(statsTableObj["Unique Visitors"]);
}
// Find favoritesCount. We can't use ' || null' here as Number("0") casts to false
if (statsTableObj["Current Favorites"]) sharedfile.favoritesCount = Number(statsTableObj["Current Favorites"]);
if (statsTableObj["Current Favorites"]) {
sharedfile.favoritesCount = Number(statsTableObj["Current Favorites"]);
}
// Find upvoteCount. We can't use ' || null' here as Number("0") casts to false
let upvoteCount = $("#VotesUpCountContainer > #VotesUpCount").text();
if (upvoteCount) sharedfile.upvoteCount = Number(upvoteCount);
if (upvoteCount) {
sharedfile.upvoteCount = Number(upvoteCount);
}
// Determine type by looking at the second breadcrumb. Find the first separator as it has a unique name and go to the next element which holds our value of interest
let breadcrumb = $(".breadcrumbs > .breadcrumb_separator").next().get(0).children[0].data || "";
if (breadcrumb.includes("Screenshot")) sharedfile.type = ESharedfileType.Screenshot;
if (breadcrumb.includes("Artwork")) sharedfile.type = ESharedfileType.Artwork;
if (breadcrumb.includes("Guide")) sharedfile.type = ESharedfileType.Guide;
if (breadcrumb.includes("Screenshot")) {
sharedfile.type = ESharedfileType.Screenshot;
}
if (breadcrumb.includes("Artwork")) {
sharedfile.type = ESharedfileType.Artwork;
}
if (breadcrumb.includes("Guide")) {
sharedfile.type = ESharedfileType.Guide;
}
// Find owner profile link, convert to steamID64 using SteamIdResolver lib and create a SteamID object
let ownerHref = $(".friendBlockLinkOverlay").attr()["href"];
SteamIdResolver.customUrlToSteamID64(ownerHref, (err, steamID64) => { // This request takes <1 sec
if (!err) sharedfile.owner = new SteamID(steamID64);
if (!err) {
sharedfile.owner = new SteamID(steamID64);
}
// Make callback when ID was resolved as otherwise owner will always be null
callback(null, new CSteamSharedfile(this, sharedfile));
@@ -120,7 +142,7 @@ function CSteamSharedfile(community, data) {
this._community = community;
// Clone all the data we recieved
Object.assign(this, data); // TODO: This is cleaner but might break IntelliSense
Object.assign(this, data); // TODO: This is cleaner but might break IntelliSense. I'm leaving the block below to be reactivated if necessary
/* this.id = data.id;
this.type = data.type;