-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopdump.js
54 lines (44 loc) · 1.4 KB
/
topdump.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
// this is a one-off script to populate the database with top HN articles from hntoplinks.com
const config = require("./util/config");
const Stories = require("./models/stories");
const Remote = require("./services/hackernews");
const mongoose = require("mongoose");
const main = async () => {
try {
console.log("Connecting to mongodb...");
await mongoose.connect(config.DB_URI_CLOUD, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
console.log("Connected!");
} catch (e) {
console.log("Couldn't connect: ", e);
}
try {
console.log("Starting web scraping...");
// LADATAAN UUSIMMAT TARINAT
try {
let ids = [];
ids.push(...await Remote.getTopStories("daily"));
ids.push(...await Remote.getTopStories("weekly"));
ids.push(...await Remote.getTopStories("monthly"));
ids.push(...await Remote.getTopStories("yearly"));
ids.push(...await Remote.getTopStories("alltime"));
const missingStories = await Remote.checkStoryExists(ids);
console.log("fetching: ", missingStories);
await Remote.addStories(missingStories);
} catch (e) {
console.log("err: ", e);
}
// CLOSE CONNECTION
} finally {
try {
await mongoose.connection.close();
console.log("closed connection");
} catch (e) {
console.log("Couldn't close connection: ", e);
}
}
};
main();