Skip to content

Instantly share code, notes, and snippets.

@josephrocca
Last active March 26, 2023 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrocca/0ada4cb92d5b419f48a5c88d4dfa07e7 to your computer and use it in GitHub Desktop.
Save josephrocca/0ada4cb92d5b419f48a5c88d4dfa07e7 to your computer and use it in GitHub Desktop.
Reddit Comment Stream (Node.js)
module.exports = function() {
let request = require("request-promise");
let stopFlag;
async function start() {
if(!this.oncomment) {
console.error("You must attach an oncomment handler (onerror handler is optional).");
return;
}
stopFlag = false;
while(1) {
if(stopFlag) break;
try {
await sleep(1);
let response = await request("https://www.reddit.com/r/all/comments/.json?limit=100");
if(stopFlag) break;
let newComments = extractNewComments(JSON.parse(response).data.children);
for(let c of newComments) {
this.oncomment(c);
}
} catch(e) {
if(this.onerror) this.onerror(e);
}
}
}
function stop() {
stopFlag = true;
}
return {
start,
stop
}
}
async function sleep(delay) {
return new Promise(resolve => setTimeout(resolve, delay*1000));
}
let oldCommentIds = [];
function extractNewComments(comments) {
let newComments = [];
for(let c of comments) {
if(!oldCommentIds.includes(c.data.id)) {
newComments.push(c.data);
oldCommentIds.push(c.data.id);
}
}
// don't let oldCommentIds grow too big
if(oldCommentIds.length > 5000) {
oldCommentIds = oldCommentIds.slice(2500); // cut off first half of array
}
return newComments;
}
// Use the above module as follows:
let RedditCommentStream = require('./reddit-comment-stream.js');
let stream = new RedditCommentStream();
stream.oncomment = function(comment) {
console.log(comment);
}
stream.onerror = function(error) {
console.log(error);
}
stream.start();
@josephrocca
Copy link
Author

josephrocca commented Apr 3, 2017

Note that this script isn't guaranteed to capture every single comment. If you've got a slow connection, or there's a sudden surge of activity on reddit, it may miss some comments. It's just a quick hacky script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment