Skip to content

Instantly share code, notes, and snippets.

@anthonytxie
Last active April 10, 2018 13:43
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 anthonytxie/b2b55d66b6f12993c9da82c238a382f3 to your computer and use it in GitHub Desktop.
Save anthonytxie/b2b55d66b6f12993c9da82c238a382f3 to your computer and use it in GitHub Desktop.
const fetchComments = async (startDate, limit) => {
console.log(`beginning comment fetch from ${startDate} to ${endDate}`);
while (startDate < endDate) {
// gets the first limit comments after startDate
const response = await axios.get(
`https://apiv2.pushshift.io/reddit/search/comment/?q=hodl&size=${limit}&after=${startDate
.toString()
.substr(0, 10)}`
);
const comments = response.data.data;
// prep comments for db
const commentDocuments = comments.map(comment => {
return {
commentDate: moment.unix(comment.created_utc).format("MM/DD/YYYY"),
subreddit: comment.subreddit,
body: comment.body
};
});
console.log(
`insert (${new Date(_.first(comments).created_utc * 1000)} -> ${new Date(
_.last(comments).created_utc * 1000
)})`
);
await Comment.insertMany(commentDocuments).catch(e => console.log(e));
startDate = _.last(comments).created_utc * 1000;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment