-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
68 lines (56 loc) · 1.8 KB
/
build.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
const fs = require("fs");
const zip = require("jszip");
const PATH = "./data/";
function streamToString(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
stream.on("error", (err) => reject(err));
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}
function getPart(p, str) {
return (" " + str).split(`<${p}>`)[1].split(`</${p}>`)[0];
}
async function losungen(year) {
const z = new zip();
// Warum XML? Die anderen datein sind komisch encodiert!
const zipResult = await fetch(
`https://www.losungen.de/fileadmin/media-losungen/download/Losung_${year}_XML.zip`,
);
const zipBuffer = await zipResult.arrayBuffer();
await z.loadAsync(zipBuffer);
const csvFile = z.file(
Object.keys(z.files).filter((v) => v.endsWith("xml"))[0],
).nodeStream("nodebuffer");
/**
* @type {string}
*/
const csvContent = await streamToString(csvFile);
const days = csvContent.split("<Losungen>").slice(1);
for (const day of days) {
const datum = getPart("Datum", day).split('T')[0];
const Losungstext = getPart("Losungstext", day);
const Losungsvers = getPart("Losungsvers", day);
const Lehrtext = getPart("Lehrtext", day);
const Lehrtextvers = getPart("Lehrtextvers", day);
const data = {
Losungstext,
Losungsvers,
Lehrtext,
Lehrtextvers
}
fs.writeFileSync(PATH + datum + '.json', JSON.stringify(data))
}
}
if (!fs.existsSync(PATH)) {
fs.mkdirSync(PATH);
}
fs.readdirSync(PATH)
.filter((v) => v[0] !== ".")
.map((v) => PATH + v)
.forEach(fs.unlinkSync);
const currentYear = new Date().getFullYear()
losungen(currentYear).then(() => {
losungen(currentYear + 1).catch(() => {console.warn('NO DATA FOR NEXT YEAR!')})
});