Added parsing of discussion data in a JS VM

This commit is contained in:
Alexander Corn 2016-01-21 17:38:46 -05:00
parent 7efc1f2fd0
commit 6144ee1909
3 changed files with 62 additions and 0 deletions

View File

61
components/discussions.js Normal file
View File

@ -0,0 +1,61 @@
var SteamCommunity = require('../index.js');
var Cheerio = require('cheerio');
var VM = require('vm');
SteamCommunity.prototype.getDiscussion = function(url, callback) {
var self = this;
this.request.get({
"uri": url,
"followRedirect": false
}, function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
if(self._checkCommunityError(body, callback)) {
return;
}
if(response.statusCode >= 300 && response.statusCode <= 399) {
callback(new Error("Topic Not Found"));
return
}
var $ = Cheerio.load(body);
var scripts = $('#group_tab_content_discussions').find('script');
if(scripts.length < 2) {
callback(new Error("Malformed response"));
return;
}
var context = VM.createContext({
"ForumTopic": {},
"CommentThread": {},
"$J": function(input) {
if(typeof input === 'function') {
input();
}
},
"InitializeForumTopic": function(board, boardUrl, topicID, topic) {
context.ForumTopic.board = board;
context.ForumTopic.topicID = topicID;
context.ForumTopic.topic = topic;
},
"InitializeCommentThread": function(type, name, commentData, url, quoteBoxHeight) {
context.CommentThread.type = type;
context.CommentThread.name = name;
context.CommentThread.commentData = commentData;
context.CommentThread.url = url;
context.CommentThread.quoteBoxHeight = quoteBoxHeight;
}
});
console.log(context);
VM.runInContext($(scripts[0]).html(), context);
VM.runInContext($(scripts[1]).html(), context);
console.log(context);
});
};

View File

@ -396,6 +396,7 @@ require('./components/inventoryhistory.js');
require('./components/webapi.js');
require('./components/twofactor.js');
require('./components/confirmations.js');
require('./components/discussions.js');
require('./classes/CMarketItem.js');
require('./classes/CMarketSearchResult.js');
require('./classes/CSteamGroup.js');