-
Notifications
You must be signed in to change notification settings - Fork 0
/
monsters.js
100 lines (97 loc) · 3.02 KB
/
monsters.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import _ from "lodash";
import axios from "axios";
import { default as mongodb } from "mongodb";
import { bestiarySources } from "./supportedSources.js";
import dotenv from "dotenv";
dotenv.config();
const uri = process.env.MONGODB;
const client = new mongodb.MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const main = async (filename) => {
const responseBestiary = await axios.get(
`https://5e.tools/data/bestiary/${filename}`
);
const responseBestiaryFluff = await axios.get(
`https://5e.tools/data/bestiary/fluff-${filename}`
);
const responseLegendaryGroup = await axios.get(
`https://5e.tools/data/bestiary/legendarygroups.json`
);
try {
const monster = responseBestiary.data.monster;
const fluff = responseBestiaryFluff.data.monsterFluff;
const legendaryGroup = responseLegendaryGroup.data.legendaryGroup;
console.log(
`counts ${filename} monster ${monster.length} fluff ${fluff.length} legendaryGroup ${legendaryGroup.length}`
);
const newMonster = await Promise.all(
monster.map((item) => {
let lairActions = [];
let regionalEffects = [];
let mythicEncounter = [];
let itemFluff = {};
if (item.hasFluff || item.hasFluffImages) {
itemFluff = _.find(fluff, { name: item.name });
}
if (!_.isEmpty(item.legendaryGroup)) {
const findGroup = _.find(legendaryGroup, {
name: item.name,
source: item.source,
});
lairActions = findGroup?.lairActions || [];
regionalEffects = findGroup?.regionalEffects || [];
mythicEncounter = findGroup?.mythicEncounter || [];
}
item["saves"] = item.save || {};
return {
id: `${item.name.replace(/\W/g, "")}-${item.source}-${
item.page
}`.toLowerCase(),
..._.omit(item, ["save"]),
images: itemFluff.images || [],
entriesFluff: itemFluff.entries || [],
lairActions,
regionalEffects,
mythicEncounter,
};
})
);
return newMonster;
} catch (error) {
console.log(error);
throw new Error(Error);
}
};
(async () => {
try {
await client.connect();
const dbo = client.db(process.env.DBNAME);
const responseIndex = await axios.get(
"https://5e.tools/data/bestiary/index.json"
);
await Promise.allSettled(
bestiarySources.map(async (source) => {
const sourceJson = responseIndex.data[source] || null;
if (sourceJson !== null) {
const result = await main(sourceJson);
await Promise.all(
result.map(async (item) => {
const query = { id: item.id };
const update = { $set: item };
const options = { upsert: true };
await dbo
.collection("monsters")
.updateOne(query, update, options);
})
);
}
})
);
} catch (e) {
console.log(e);
} finally {
await client.close();
}
})();