-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://192.168.1.247/reddit_popular');
const Post = require('./models/post');
const getPosts = require('./getPosts');
nextAll();
setTimeout(nextPopular, 1000);
function storePosts (data) {
// posts is a listing object from reddit
// posts are in listing.data.children
// post props are in listing.data.children[i].data
return new Promise((fulfill, reject) => {
let posts = data.posts;
let promises = [];
for (let i=0; i<posts.length; i++) {
((post) => {
promises.push(new Promise((fulfill, reject) => {
post.multi = data.multi;
let query = {id:post.id, multi:post.multi};
Post.findOneAndUpdate(query, post, {upsert:true}, function(err, doc){
if (err) {
reject(err);
return;
}
fulfill(doc);
});
}));
})(posts[i]);
}
Promise.all(promises).then((posts) => {
fulfill({
after : data.after,
posts : posts
});
}).catch((err) => {
reject(err);
});
});
}
function nextAll (after) {
console.log(`Getting /r/all after ${after}`);
getPosts('all', after).then(storePosts)
.then((data) => {
setTimeout(() => {
nextAll(data.after);
}, 2000 * Math.random());
})
.catch((err) => {console.error(err);});
}
function nextPopular (after) {
console.log(`Getting /r/popular after ${after}`);
getPosts('popular', after).then(storePosts)
.then((data) => {
setTimeout(() => {
nextPopular(data.after);
}, 2000 * Math.random());
})
.catch((err) => {console.error(err);});}