2020-05-24 23:10:30 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
/* Import local files */
|
2020-05-24 23:10:30 +02:00
|
|
|
const {fetchKanjiFromTxt, fetchKanjiFromJisho} = require('./src/dataFetching.js');
|
|
|
|
const {getKanjiTexData} = require('./src/texConversion.js');
|
|
|
|
const {kanjiTable} = require('./src/kanjiTables.js');
|
|
|
|
|
|
|
|
/* Encapsulate main process in async function */
|
|
|
|
async function main(jlptLevel) {
|
|
|
|
|
2020-05-25 22:16:04 +02:00
|
|
|
const jlptLevelCaps = jlptLevel.toUpperCase();
|
|
|
|
|
2020-05-24 23:10:30 +02:00
|
|
|
const kanjiArray = await fetchKanjiFromTxt(`./data/txt/${jlptLevel}.txt`);
|
2020-05-25 22:16:04 +02:00
|
|
|
console.log(`${jlptLevelCaps}: Fetched txt`);
|
2020-05-24 23:10:30 +02:00
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
const jishoResults = await fetchKanjiFromJisho(kanjiArray);
|
2020-05-25 22:16:04 +02:00
|
|
|
console.log(`${jlptLevelCaps}: Fetched Jisho data`);
|
2020-05-24 23:10:30 +02:00
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
const sortedKanjiArray = jishoResults.map(result => result.query);
|
|
|
|
const texData = getKanjiTexData(jishoResults);
|
2020-05-25 22:16:04 +02:00
|
|
|
console.log(`${jlptLevelCaps}: Processed pages`);
|
2020-05-24 23:10:30 +02:00
|
|
|
|
2020-05-25 22:16:04 +02:00
|
|
|
const resultTable = kanjiTable(sortedKanjiArray);
|
|
|
|
console.log(`${jlptLevelCaps}: Processed table`);
|
2020-05-24 23:10:30 +02:00
|
|
|
|
|
|
|
let resultPage = '';
|
|
|
|
for (kanji of texData) {
|
|
|
|
resultPage+=`${kanji.kanjiPageHeader}
|
|
|
|
${kanji.kanjiMeaning ? kanji.kanjiMeaning : ''}
|
|
|
|
${kanji.kunyomi ? kanji.kunyomi : ''}
|
|
|
|
${kanji.onyomi ? kanji.onyomi : ''}
|
|
|
|
${kanji.kanjiRow}
|
|
|
|
\\newpage\n`;
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:16:04 +02:00
|
|
|
fs.writeFile(`./data/tables/${jlptLevel}.tex`, resultTable, (err) => {if (err) console.error(err)});
|
|
|
|
fs.writeFile(`./data/pages/${jlptLevel}.tex`, resultPage, (err) => {if (err) console.error(err)});
|
2020-05-24 23:10:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
/* Handle args */
|
|
|
|
async function argWrapper() {
|
2020-05-24 23:10:30 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (!/n\d/.test(process.argv[2])) throw 'Input not valid';
|
|
|
|
await main(process.argv[2]);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:03:19 +02:00
|
|
|
argWrapper();
|