Merge pull request #159 from Jarzon/groupcomments

Steam group comments
This commit is contained in:
Alex Corn 2017-12-26 16:28:16 -05:00 committed by GitHub
commit 1efac6bc70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 5 deletions

View File

@ -114,6 +114,14 @@ CSteamGroup.prototype.getHistory = function(page, callback) {
this._community.getGroupHistory(this.steamID, page, callback);
};
CSteamGroup.prototype.getAllComments = function(from, count, callback) {
this._community.getAllGroupComments(this.steamID, from, count, callback);
};
CSteamGroup.prototype.deleteComment = function(cid, callback) {
this._community.deleteGroupComment(this.steamID, cid, callback);
/**
* Get requests to join this restricted group.
* @param {function} callback - First argument is null/Error, second is array of SteamID objects

View File

@ -2,6 +2,7 @@ var SteamCommunity = require('../index.js');
var SteamID = require('steamid');
var xml2js = require('xml2js');
var Cheerio = require('cheerio');
var Helpers = require('./helpers.js');
var EResult = SteamCommunity.EResult;
SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link, addresses, addressIdx) {
@ -519,6 +520,70 @@ SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) {
}, "steamcommunity");
};
SteamCommunity.prototype.getAllGroupComments = function(gid, from, count, callback) {
var options = {
uri: "http://steamcommunity.com/comment/Clan/render/" + gid.getSteamID64() + "/-1/",
form: {
start: from,
count: count
}
};
var self = this;
this.httpRequestPost(options, function(err, response, body) {
if (err) {
callback(err);
return;
}
var comments = [];
var $ = Cheerio.load(JSON.parse(body).comments_html);
$(".commentthread_comment_content").each(function () {
var comment = {};
var cachedSelector;
var $selector = $(this).find(".commentthread_author_link");
comment.authorName = $($selector).find("bdi").text();
comment.authorId = $($selector).attr("href").replace(/http:\/\/steamcommunity.com\/(id|profiles)\//, "");
comment.date = Helpers.decodeSteamTime($(this).find(".commentthread_comment_timestamp").text().trim());
$selector = $(this).find(".commentthread_comment_text");
comment.commentId = $($selector).attr("id").replace("comment_content_", "");
comment.text = $($selector).html().trim();
comments.push(comment);
});
callback(null, comments);
}, "steamcommunity");
};
SteamCommunity.prototype.deleteGroupComment = function(gid, cid, callback) {
if(typeof cid !== 'string') {
cid = cid.toString();
}
var options = {
uri: "http://steamcommunity.com/comment/Clan/delete/" + gid.getSteamID64() + "/-1/",
form: {
sessionid: this.getSessionID(),
gidcomment: cid
}
};
var self = this;
this.httpRequestPost(options, function(err, response, body) {
if(!callback) {
return;
}
callback(err || null);
}, "steamcommunity");
};
/**
* Get requests to join a restricted group.
* @param {SteamID|string} gid - The SteamID of the group you want to manage

View File

@ -13,11 +13,26 @@ exports.isSteamID = function(input) {
};
exports.decodeSteamTime = function(time) {
var date = new Date();
if (time.includes("@")) {
var parts = time.split('@');
if (!parts[0].match(/,/)) {
if (!parts[0].includes(",")) {
// no year, assume current year
parts[0] += ", " + (new Date()).getFullYear();
parts[0] += ", " + date.getFullYear();
}
return new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it
date = new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it
} else {
// Relative date
var amount = time.replace(/(\d) (minutes|hour|hours) ago/, "$1");
if(time.includes("minutes")) {
date.setMinutes(date.getMinutes() - amount);
} else if(time.match(/hour|hours/)) {
date.setHours(date.getHours() - amount);
}
}
return date;
};