-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspells.js
79 lines (75 loc) · 2.18 KB
/
spells.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
import _ from "lodash";
import axios from "axios";
import { default as mongodb } from "mongodb";
import { spellSources } 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 responseSpells = await axios.get(
`https://5e.tools/data/spells/${filename}`
);
const responseSpellsFluff = await axios.get(
`https://5e.tools/data/spells/fluff-${filename}`
);
try {
const spells = responseSpells.data.spell;
const fluff = responseSpellsFluff.data.spellFluff;
console.log(
`counts ${filename} spells ${spells.length} fluff ${fluff.length}`
);
const newSpells = await Promise.all(
spells.map((item) => {
let itemFluff = {};
if (item.hasFluff || item.hasFluffImages) {
itemFluff = _.find(fluff, { name: item.name });
}
return {
id: `${item.name.replace(/\W/g, "")}-${item.source}-${
item.page
}`.toLowerCase(),
...item,
images: itemFluff.images || [],
entriesFluff: itemFluff.entries || [],
};
})
);
return newSpells;
} catch (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/spells/index.json"
);
await Promise.allSettled(
spellSources.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("spells").updateOne(query, update, options);
})
);
}
})
);
} catch (e) {
// Deal with the fact the chain failed
console.log(e);
} finally {
await client.close();
}
})();