Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Last active September 29, 2015 20:08
Show Gist options
  • Save chrahunt/1f05bfc2eaaf4852220b to your computer and use it in GitHub Desktop.
Save chrahunt/1f05bfc2eaaf4852220b to your computer and use it in GitHub Desktop.
// Just run with node mm.js and a file "ids.txt" is generated in the same directory.
var https = require('https');
var fs = require('fs');
var submission_id = "3lhmxs";
// Last comment id on comment list, if any.
var cutoff_comment_id = "cv7cbxs";
var link = "https://www.reddit.com/r/millionairemakers/comments/" + submission_id + ".json?sort=old";
https.get(link, function (res) {
res.setEncoding('utf8');
var body = "";
res.on('data', function (d) {
body += d;
});
res.on('end', function() {
var data = parseContent(body);
writeFile(data);
});
});
function writeFile(ids) {
var text = ids.join("\n");
fs.writeFile("ids.txt", text);
}
function parseContent(content) {
// get content
var data = JSON.parse(content);
var comment_ids = [];
var children = data[1].data.children;
var outer_break = false;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.data.parent_id === "t3_" + submission_id) {
if (child.kind === "t1") {
comment_ids.push(child.data.id);
} else if (child.kind === "more") {
var inner_children = child.data.children;
for (var j = 0; j < inner_children.length; j++) {
var inner_child = inner_children[j];
comment_ids.push(inner_child);
if (inner_child == cutoff_comment_id) {
// if we want to break outer loop.
//outer_break = true;
break;
}
}
// just in case the cutoff id isn't in a "more", who knows.
// todo: fix repo?
if (outer_break) {
break;
}
}
}
}
return comment_ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment