diff --git a/classes/CSteamSharedfile.js b/classes/CSteamSharedfile.js index de9db50..8ed43ad 100644 --- a/classes/CSteamSharedfile.js +++ b/classes/CSteamSharedfile.js @@ -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,126 +12,148 @@ 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, - type: null, - appID: null, - owner: null, - fileSize: null, - postDate: null, - resolution: null, - uniqueVisitorsCount: null, - favoritesCount: null, - upvoteCount: null - } + + // Construct object holding all the data we can scrape + let sharedfile = { + id: sid, + type: null, + appID: null, + owner: null, + fileSize: null, + postDate: null, + resolution: null, + uniqueVisitorsCount: null, + favoritesCount: null, + upvoteCount: null + }; - // Get DOM of sharedfile - this.httpRequestGet(`https://steamcommunity.com/sharedfiles/filedetails/?id=${sid}`, (err, res, body) => { - try { + // Get DOM of sharedfile + this.httpRequestGet(`https://steamcommunity.com/sharedfiles/filedetails/?id=${sid}`, (err, res, body) => { + try { - /* --------------------- Preprocess output --------------------- */ + /* --------------------- Preprocess output --------------------- */ - // Load output into cheerio to make parsing easier - let $ = Cheerio.load(body); + // Load output into cheerio to make parsing easier + let $ = Cheerio.load(body); - // Dynamically map detailsStatsContainerLeft to detailsStatsContainerRight in an object to make readout easier. It holds size, post date and resolution. - let detailsStatsObj = {}; - let detailsLeft = $(".detailsStatsContainerLeft").children(); - let detailsRight = $(".detailsStatsContainerRight").children(); + // Dynamically map detailsStatsContainerLeft to detailsStatsContainerRight in an object to make readout easier. It holds size, post date and resolution. + let detailsStatsObj = {}; + let detailsLeft = $(".detailsStatsContainerLeft").children(); + 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 + 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 + } - detailsStatsObj[detailsLeft[e].children[0].data.trim()] = detailsRight[e].children[0].data; - }); + detailsStatsObj[detailsLeft[e].children[0].data.trim()] = detailsRight[e].children[0].data; + }); - // Dynamically map stats_table descriptions to values. This holds Unique Visitors and Current Favorites - let statsTableObj = {}; - let statsTable = $(".stats_table").children(); + // Dynamically map stats_table descriptions to values. This holds Unique Visitors and Current Favorites + 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 - }); + // 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 + }); - /* --------------------- Find and map values --------------------- */ + /* --------------------- Find and map values --------------------- */ - // Find appID in share button onclick event - sharedfile.appID = Number($("#ShareItemBtn").attr()["onclick"].replace(`ShowSharePublishedFilePopup( '${sid}', '`, "").replace("' );", "")) + // Find appID in share button onclick event + sharedfile.appID = Number($("#ShareItemBtn").attr()["onclick"].replace(`ShowSharePublishedFilePopup( '${sid}', '`, "").replace("' );", "")); - // Find fileSize if not guide - sharedfile.fileSize = detailsStatsObj["File Size"] || null; // TODO: Convert to bytes? It seems like to always be MB but no guarantee + // Find fileSize if not guide + sharedfile.fileSize = detailsStatsObj["File Size"] || null; // TODO: Convert to bytes? It seems like to always be MB but no guarantee - // Find postDate and convert to timestamp - let posted = detailsStatsObj["Posted"].trim(); + // Find postDate and convert to timestamp + let posted = detailsStatsObj["Posted"].trim(); - sharedfile.postDate = Date.parse(Helpers.decodeSteamTime(posted)); // Pass String into helper and parse the returned String to get a Unix timestamp + sharedfile.postDate = Date.parse(Helpers.decodeSteamTime(posted)); // Pass String into helper and parse the returned String to get a Unix timestamp - // Find resolution if artwork or screenshot - sharedfile.resolution = detailsStatsObj["Size"] || null; + // Find resolution if artwork or screenshot + sharedfile.resolution = detailsStatsObj["Size"] || null; - // Find uniqueVisitorsCount. We can't use ' || null' here as Number("0") casts to false - if (statsTableObj["Unique Visitors"]) sharedfile.uniqueVisitorsCount = Number(statsTableObj["Unique Visitors"]); + // Find uniqueVisitorsCount. We can't use ' || null' here as Number("0") casts to false + 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"]); + // Find favoritesCount. We can't use ' || null' here as Number("0") casts to false + 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); + // 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); + } - // 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; + // 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; + } - // Find owner profile link, convert to steamID64 using SteamIdResolver lib and create a SteamID object - let ownerHref = $(".friendBlockLinkOverlay").attr()["href"]; + // 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); + SteamIdResolver.customUrlToSteamID64(ownerHref, (err, steamID64) => { // This request takes <1 sec + 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)); - }); + // Make callback when ID was resolved as otherwise owner will always be null + callback(null, new CSteamSharedfile(this, sharedfile)); + }); - } catch (err) { - callback(err, null); - } - }, "steamcommunity"); + } catch (err) { + callback(err, null); + } + }, "steamcommunity"); }; function CSteamSharedfile(community, data) { - this._community = community; + this._community = community; - // Clone all the data we recieved - Object.assign(this, data); // TODO: This is cleaner but might break IntelliSense + // Clone all the data we recieved + 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; - this.appID = data.appID; - this.owner = data.owner; - this.fileSize = data.fileSize; - this.postDate = data.postDate; - this.resolution = data.resolution; - this.uniqueVisitorsCount = data.uniqueVisitorsCount; - this.favoritesCount = data.favoritesCount; - this.upvoteCount = data.upvoteCount; */ + /* this.id = data.id; + this.type = data.type; + this.appID = data.appID; + this.owner = data.owner; + this.fileSize = data.fileSize; + this.postDate = data.postDate; + this.resolution = data.resolution; + this.uniqueVisitorsCount = data.uniqueVisitorsCount; + this.favoritesCount = data.favoritesCount; + this.upvoteCount = data.upvoteCount; */ } /** @@ -140,7 +162,7 @@ function CSteamSharedfile(community, data) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.deleteComment = function(cid, callback) { - this._community.deleteSharedfileComment(this.userID, this.id, cid, callback); + this._community.deleteSharedfileComment(this.userID, this.id, cid, callback); }; /** @@ -148,7 +170,7 @@ CSteamSharedfile.prototype.deleteComment = function(cid, callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.favorite = function(callback) { - this._community.favoriteSharedfile(this.id, this.appID, callback); + this._community.favoriteSharedfile(this.id, this.appID, callback); }; /** @@ -157,7 +179,7 @@ CSteamSharedfile.prototype.favorite = function(callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.comment = function(message, callback) { - this._community.postSharedfileComment(this.owner, this.id, message, callback); + this._community.postSharedfileComment(this.owner, this.id, message, callback); }; /** @@ -165,7 +187,7 @@ CSteamSharedfile.prototype.comment = function(message, callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.subscribe = function(callback) { - this._community.subscribeSharedfileComments(this.owner, this.id, callback); + this._community.subscribeSharedfileComments(this.owner, this.id, callback); }; /** @@ -173,7 +195,7 @@ CSteamSharedfile.prototype.subscribe = function(callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.unfavorite = function(callback) { - this._community.unfavoriteSharedfile(this.id, this.appID, callback); + this._community.unfavoriteSharedfile(this.id, this.appID, callback); }; /** @@ -181,7 +203,7 @@ CSteamSharedfile.prototype.unfavorite = function(callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.unsubscribe = function(callback) { - this._community.unsubscribeSharedfileComments(this.owner, this.id, callback); + this._community.unsubscribeSharedfileComments(this.owner, this.id, callback); }; /** @@ -189,7 +211,7 @@ CSteamSharedfile.prototype.unsubscribe = function(callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.voteDown = function(callback) { - this._community.voteDownSharedfile(this.id, callback); + this._community.voteDownSharedfile(this.id, callback); }; /** @@ -197,5 +219,5 @@ CSteamSharedfile.prototype.voteDown = function(callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ CSteamSharedfile.prototype.voteUp = function(callback) { - this._community.voteUpSharedfile(this.id, callback); + this._community.voteUpSharedfile(this.id, callback); }; \ No newline at end of file diff --git a/components/sharedfiles.js b/components/sharedfiles.js index be9fe3a..3ef690a 100644 --- a/components/sharedfiles.js +++ b/components/sharedfiles.js @@ -10,24 +10,24 @@ var SteamID = require('steamid'); * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.deleteSharedfileComment = function(userID, sid, cid, callback) { - if (typeof userID === "string") { - userID = new SteamID(userID); - } + if (typeof userID === "string") { + userID = new SteamID(userID); + } - this.httpRequestPost({ - "uri": `https://steamcommunity.com/comment/PublishedFile_Public/delete/${userID.toString()}/${sid}/`, - "form": { - "gidcomment": cid, - "count": 10, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": `https://steamcommunity.com/comment/PublishedFile_Public/delete/${userID.toString()}/${sid}/`, + "form": { + "gidcomment": cid, + "count": 10, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -37,20 +37,20 @@ SteamCommunity.prototype.deleteSharedfileComment = function(userID, sid, cid, ca * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.favoriteSharedfile = function(sid, appid, callback) { - this.httpRequestPost({ - "uri": "https://steamcommunity.com/sharedfiles/favorite", - "form": { - "id": sid, - "appid": appid, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": "https://steamcommunity.com/sharedfiles/favorite", + "form": { + "id": sid, + "appid": appid, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -61,24 +61,24 @@ SteamCommunity.prototype.favoriteSharedfile = function(sid, appid, callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.postSharedfileComment = function(userID, sid, message, callback) { - if (typeof userID === "string") { - userID = new SteamID(userID); - } + if (typeof userID === "string") { + userID = new SteamID(userID); + } - this.httpRequestPost({ - "uri": `https://steamcommunity.com/comment/PublishedFile_Public/post/${userID.toString()}/${sid}/`, - "form": { - "comment": message, - "count": 10, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": `https://steamcommunity.com/comment/PublishedFile_Public/post/${userID.toString()}/${sid}/`, + "form": { + "comment": message, + "count": 10, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -88,23 +88,23 @@ SteamCommunity.prototype.postSharedfileComment = function(userID, sid, message, * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.subscribeSharedfileComments = function(userID, sid, callback) { - if (typeof userID === "string") { - userID = new SteamID(userID); - } + if (typeof userID === "string") { + userID = new SteamID(userID); + } - this.httpRequestPost({ - "uri": `https://steamcommunity.com/comment/PublishedFile_Public/subscribe/${userID.toString()}/${sid}/`, - "form": { - "count": 10, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { // eslint-disable-line - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": `https://steamcommunity.com/comment/PublishedFile_Public/subscribe/${userID.toString()}/${sid}/`, + "form": { + "count": 10, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { // eslint-disable-line + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -114,20 +114,20 @@ SteamCommunity.prototype.subscribeSharedfileComments = function(userID, sid, cal * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.unfavoriteSharedfile = function(sid, appid, callback) { - this.httpRequestPost({ - "uri": "https://steamcommunity.com/sharedfiles/unfavorite", - "form": { - "id": sid, - "appid": appid, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": "https://steamcommunity.com/sharedfiles/unfavorite", + "form": { + "id": sid, + "appid": appid, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -137,23 +137,23 @@ SteamCommunity.prototype.unfavoriteSharedfile = function(sid, appid, callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.unsubscribeSharedfileComments = function(userID, sid, callback) { - if (typeof userID === "string") { - userID = new SteamID(userID); - } + if (typeof userID === "string") { + userID = new SteamID(userID); + } - this.httpRequestPost({ - "uri": `https://steamcommunity.com/comment/PublishedFile_Public/unsubscribe/${userID.toString()}/${sid}/`, - "form": { - "count": 10, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { // eslint-disable-line - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": `https://steamcommunity.com/comment/PublishedFile_Public/unsubscribe/${userID.toString()}/${sid}/`, + "form": { + "count": 10, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { // eslint-disable-line + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -162,19 +162,19 @@ SteamCommunity.prototype.unsubscribeSharedfileComments = function(userID, sid, c * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.voteDownSharedfile = function(sid, callback) { - this.httpRequestPost({ - "uri": "https://steamcommunity.com/sharedfiles/votedown", - "form": { - "id": sid, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": "https://steamcommunity.com/sharedfiles/votedown", + "form": { + "id": sid, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; /** @@ -183,17 +183,17 @@ SteamCommunity.prototype.voteDownSharedfile = function(sid, callback) { * @param {function} callback - Takes only an Error object/null as the first argument */ SteamCommunity.prototype.voteUpSharedfile = function(sid, callback) { - this.httpRequestPost({ - "uri": "https://steamcommunity.com/sharedfiles/voteup", - "form": { - "id": sid, - "sessionid": this.getSessionID() - } - }, function(err, response, body) { - if (!callback) { - return; - } + this.httpRequestPost({ + "uri": "https://steamcommunity.com/sharedfiles/voteup", + "form": { + "id": sid, + "sessionid": this.getSessionID() + } + }, function(err, response, body) { + if (!callback) { + return; + } - callback(null || err); - }, "steamcommunity"); + callback(null || err); + }, "steamcommunity"); }; \ No newline at end of file diff --git a/resources/ESharedfileType.js b/resources/ESharedfileType.js index b8732ce..fc528d5 100644 --- a/resources/ESharedfileType.js +++ b/resources/ESharedfileType.js @@ -2,12 +2,12 @@ * @enum ESharedfileType */ module.exports = { - "Screenshot": 0, - "Artwork": 1, - "Guide": 2, + "Screenshot": 0, + "Artwork": 1, + "Guide": 2, - // Value-to-name mapping for convenience - "0": "Screenshot", - "1": "Artwork", - "2": "Guide" + // Value-to-name mapping for convenience + "0": "Screenshot", + "1": "Artwork", + "2": "Guide" }; \ No newline at end of file