-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.js
89 lines (76 loc) · 2.24 KB
/
convert.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
import fs from "fs";
import { DIR_CATS } from "./common";
import chalk from "chalk";
const DIR_CONVERTED = __dirname + "/scraped-data-converted";
import XLSX from "xlsx";
function parseCatUrl(str = "") {
const splited = str.split("/");
return splited[splited.length - 2];
}
function initConvertedDir() {
try {
if (!fs.existsSync(DIR_CONVERTED)) {
fs.mkdirSync(DIR_CONVERTED);
console.log(chalk.blue("ℹ"), "initConvertedDir: dir is created");
} else {
console.log(
chalk.blue("ℹ"),
"initConvertedDir: don't need to create a dir"
);
}
} catch (err) {
throw err;
}
}
export function toXLSX() {
const wb = XLSX.utils.book_new();
wb.Props = {
Title: "converted data",
Subject: "Test",
Author: "Edvola",
CreatedDate: new Date(),
};
const fileNames = fs.readdirSync(DIR_CATS);
console.log(chalk.yellow("⚠"), "convertation to xlsx started...");
initConvertedDir();
let subCatsOffset = [];
let topicsOffset = [];
for (let i = 0; i < 1; i++) {
subCatsOffset.push("");
}
for (let i = 0; i < 2; i++) {
topicsOffset.push("");
}
fileNames.forEach((fileName) => {
const file = JSON.parse(fs.readFileSync(`${DIR_CATS}/${fileName}`));
const lang = fileName.split(".")[0];
wb.SheetNames.push(lang);
const catsContent = [];
console.log(chalk.blue("ℹ"), `convertation to xlsx ${lang}`);
if (file) {
file.forEach((cat) => {
const subCats = [];
cat.subCatList.forEach((item) => {
const subcatName = parseCatUrl(item.href);
subCats.push([
...subCatsOffset,
`${subcatName} - ${item.text}`,
"тема:",
]);
item.topics.forEach((topic) => {
const topicName = parseCatUrl(topic.href);
subCats.push([...topicsOffset, `${topicName} - ${topic.text}`]);
});
});
catsContent.push(...subCats);
});
wb.Sheets[lang] = XLSX.utils.aoa_to_sheet([...catsContent]);
}
XLSX.writeFile(wb, `${DIR_CONVERTED}/converted.xlsx`, {
bookType: "xlsx",
type: "binary",
});
});
console.log(chalk.green("✔"), "convertation to xlsx finished!");
}
process.env.hasOwnProperty("main") && toXLSX();