Skip to content

Instantly share code, notes, and snippets.

@hortinstein
Created February 3, 2012 09:09
Show Gist options
  • Save hortinstein/1729182 to your computer and use it in GitHub Desktop.
Save hortinstein/1729182 to your computer and use it in GitHub Desktop.
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;
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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment