kanji-sheets/jishoScrape/index.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
2020-05-25 23:03:19 +02:00
/* Import local files */
2020-06-10 16:48:24 +02:00
const { fetchJishoResults } = require('./src/dataFetching.js');
const { getKanjiTexData } = require('./src/texConversion.js');
2020-06-10 20:22:35 +02:00
const { chapterTabular } = require('./src/chapterTabular.js');
2020-05-25 22:16:04 +02:00
2020-06-08 21:58:58 +02:00
/* Encapsulate main process in async function */
2020-06-10 16:48:24 +02:00
async function main(grade) {
/* Custom log function */
function log(message) {
const gradeCaps = grade.toUpperCase();
console.log(`${gradeCaps}: ${message}`);
}
2020-05-27 17:28:01 +02:00
2020-06-10 16:48:24 +02:00
const jishoResults = await fetchJishoResults(grade, log);
2020-06-08 21:58:58 +02:00
const kanjiArray = jishoResults.map(result => result.query);
2020-06-10 16:48:24 +02:00
log('Generating tex pages');
2020-06-11 00:01:44 +02:00
const texData = getKanjiTexData(jishoResults, grade);
2020-06-10 16:48:24 +02:00
log('Generating chapter table page');
2020-06-09 15:19:20 +02:00
const chapterTable = chapterTabular(kanjiArray, 16);
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-06-10 16:48:24 +02:00
fs.writeFile(
`./data/tables/${grade}.tex`,
chapterTable,
(err) => { if (err) console.error(err) }
);
fs.writeFile(
`./data/pages/${grade}.tex`,
resultPage,
(err) => { if (err) console.error(err) }
);
2020-06-08 21:58:58 +02:00
}
2020-05-25 23:03:19 +02:00
/* Handle args */
async function argWrapper() {
try {
2020-06-10 16:48:24 +02:00
if (!/grade\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();