Last active
March 26, 2023 15:47
-
-
Save josephrocca/0ada4cb92d5b419f48a5c88d4dfa07e7 to your computer and use it in GitHub Desktop.
Reddit Comment Stream (Node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.