public
Created

  • Download Gist
redditParser.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
var http = require('http');
var events = require('events');
var sys = require('util');
/// redditParser
// `
function RedditParser() {
if(false === (this instanceof RedditParser)) {
return new RedditParser();
}
events.EventEmitter.call(this)
}
sys.inherits(RedditParser, events.EventEmitter);
//eventEmitter.on('someOccurence', function(message){
// console.log(message);
//});
//
//
 
//eventEmitter.emit('someOccurence', 'Something happened!');
 
/// grabComments
// @string username: username for the user whose comments are pulled
// @string last: identity of last returned comment
// useful for recursive calls
RedditParser.prototype.grabComments = function grabComments(username, last)
{
//last = (last) ? last : '';//sets last if not provided to the empty string
var self = this;
var options = {
host: 'www.reddit.com',
port: 80,
path: '/user/'+username+'/comments/.json?after='+last
}
console.log("... processing "+username);
http.get(options, function (res) {
// console.log("reddit res: " + res.statusCode);
 
var resJSON = ''; //stores the comment JSON stream given in the res
res.on('data', function (chunk) {
resJSON+=chunk;
})
res.on('end', function () {
 
var obJSON = (JSON.parse(resJSON));
 
if (obJSON.hasOwnProperty("error")){
console.log(obJSON);
console.log('... ', username, ' does not exist');
return;
}
comments = obJSON["data"]["children"];
last = obJSON["data"]["after"];
console.log(last);
if (last){
setTimeout(function(){
// console.log("now");
grabComments(username, last);
},2220) ;
 
} else {
var finishedMessage =( '...finished parsing ' + username);
console.log("before emit");
self.emit('next', finishedMessage);
}
 
 
for (comment in comments)
{
if (!comments.hasOwnProperty(comment)) {
continue;
}
//console.log(JSON.stringify(comments[comment]["data"]["subreddit"], null,'\t'));
}
 
})
}).on('error', function (e) {
console.log("got error: " + e.message);
});
}
module.exports = RedditParser;
server.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var http = require('http');
var RedditParser = require('./redditParser');
///Creates the server//
http.createServer(function (req,res) {
comments_json = []
res.writeHead(200, {'content-type': 'text/plain'});
var spider = new RedditParser();
 
spider.grabComments(req.url);
spider.on('next', function (message) {
console.log(message);
});
res.end("loading" );
}).listen(8124, "127.0.0.1");

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.