-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.js
65 lines (61 loc) · 1.71 KB
/
items.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
65
import _ from "lodash";
import axios from "axios";
import { default as mongodb } from "mongodb";
import dotenv from "dotenv";
dotenv.config();
const uri = process.env.MONGODB;
const client = new mongodb.MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const main = async () => {
const responseItems = await axios.get("https://5e.tools/data/items.json");
const responseItemsFluff = await axios.get(
"https://5e.tools/data/fluff-items.json"
);
try {
const items = responseItems.data.item;
const fluff = responseItemsFluff.data.itemFluff;
console.log(`count items ${items.length} fluff ${fluff.length}`);
const newItems = await Promise.all(
items.map((item) => {
let itemFluff = {};
if (item.hasFluffImages) {
itemFluff = _.find(fluff, { name: item.name });
}
return {
id: `${item.name.replace(/\W/g, "")}-${item.source}-${
item.page
}`.toLowerCase(),
base: false,
...item,
images: itemFluff?.images || [],
entriesFluff: itemFluff?.entries || [],
};
})
);
return newItems;
} catch (error) {
throw new Error(error);
}
};
(async () => {
try {
await client.connect();
const dbo = client.db(process.env.DBNAME);
var text = await main();
await Promise.all(
text.map(async (item) => {
const query = { id: item.id };
const update = { $set: item };
const options = { upsert: true };
await dbo.collection("items").updateOne(query, update, options);
})
);
} catch (e) {
// Deal with the fact the chain failed
console.log(e);
} finally {
await client.close();
}
})();