-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.51 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
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri =
"mongodb+srv://sam:[email protected]/?retryWrites=true&w=majority";
const { scrapWether } = require("./scrap");
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
async function run() {
const scrapDate = new Date().toISOString().substring(0, 10);
try {
await client.connect();
const db = client.db("test");
const places = await db.collection("places").find({}).toArray();
for (const place of places) {
const wether = await scrapWether(place.windguruId);
const minDate = wether[0].date;
const maxDate = wether[wether.length - 1].date;
await db.collection("wether").deleteMany({
"place._id": place._id,
date: {
$gte: minDate,
$lte: maxDate,
},
});
// await db.collection('wether').updateMany({
// 'place._id': place._id,
// date: {
// $gte: minDate,
// $lte: maxDate
// }
// }, { $set: { old: true }});
const docs = wether.map((w) => ({
...w,
place,
scrapDate,
old: false,
}));
await db.collection("wether").insertMany(docs);
}
console.log("FIN");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
// scrapWether(198474).then(console.log);