Add data buffering

This commit is contained in:
Oystein Kristoffer Tveit 2020-05-27 17:28:01 +02:00
parent 86e2c54a9f
commit 8701d50e64
4 changed files with 39 additions and 8 deletions

5
.gitignore vendored
View File

@ -1,10 +1,15 @@
#LaTeX output files
main.out main.out
main.log main.log
main.aux main.aux
main.synctex.gz main.synctex.gz
main.toc main.toc
#Data to be reprocessed
data/testing/ data/testing/
data/pages/ data/pages/
data/tables/ data/tables/
data/jisho
#Libraries
jishoScrape/node_modules jishoScrape/node_modules

View File

@ -24,6 +24,7 @@ folders:
mkdir data/pages mkdir data/pages
mkdir data/tables mkdir data/tables
mkdir data/testing mkdir data/testing
mkdir data/jisho
cd jishoScrape cd jishoScrape
npm install npm install
cd .. cd ..

View File

@ -1,7 +1,7 @@
const fs = require('fs'); const fs = require('fs');
/* Import local files */ /* Import local files */
const {fetchKanjiFromTxt, fetchKanjiFromJisho} = require('./src/dataFetching.js'); const {fetchKanjiFromTxt, fetchJishoBufferData, fetchKanjiFromJisho} = require('./src/dataFetching.js');
const {getKanjiTexData} = require('./src/texConversion.js'); const {getKanjiTexData} = require('./src/texConversion.js');
const {kanjiTable} = require('./src/kanjiTables.js'); const {kanjiTable} = require('./src/kanjiTables.js');
@ -10,11 +10,24 @@ async function main(jlptLevel) {
const jlptLevelCaps = jlptLevel.toUpperCase(); const jlptLevelCaps = jlptLevel.toUpperCase();
const kanjiArray = await fetchKanjiFromTxt(`./data/txt/${jlptLevel}.txt`); /* Fetch data from buffer if available.
console.log(`${jlptLevelCaps}: Fetched txt`); * Else fetch data from txt and jisho requests,
* and make buffer files
*/
if(fs.existsSync(`./data/jisho/${jlptLevel}.json`)) {
var jishoResults = await fetchJishoBufferData(`./data/jisho/${jlptLevel}.json`);
console.log(`${jlptLevelCaps}: Fetched Jisho data from buffer`);
const jishoResults = await fetchKanjiFromJisho(kanjiArray); } else {
console.log(`${jlptLevelCaps}: Fetched Jisho data`); const kanjiArray = await fetchKanjiFromTxt(`./data/txt/${jlptLevel}.txt`);
console.log(`${jlptLevelCaps}: Fetched txt`);
var jishoResults = await fetchKanjiFromJisho(kanjiArray);
console.log(`${jlptLevelCaps}: Fetched Jisho data`);
fs.writeFile(`./data/jisho/${jlptLevel}.json`, JSON.stringify(jishoResults, null, " "), (err) => {if (err) console.error(err)});
console.log(`${jlptLevelCaps}: Written Jisho data to buffer`);
}
const sortedKanjiArray = jishoResults.map(result => result.query); const sortedKanjiArray = jishoResults.map(result => result.query);
const texData = getKanjiTexData(jishoResults); const texData = getKanjiTexData(jishoResults);

View File

@ -6,7 +6,7 @@ const jisho = new jishoApi();
/** /**
* Reads a txt file and splits the characters into an array * Reads a txt file and splits the characters into an array
* @param {string} file Path to file * @param {string} file Path to file
* @returns {string[]} Kanji * @returns {string[]} A list of Kanji
*/ */
async function fetchKanjiFromTxt(file) { async function fetchKanjiFromTxt(file) {
const read = util.promisify(fs.readFile); const read = util.promisify(fs.readFile);
@ -14,10 +14,21 @@ async function fetchKanjiFromTxt(file) {
return data.split(''); return data.split('');
} }
/**
* Reads a json file and returns the data as an object
* @param {string} file Path to file
* @returns {object} Jisho results
*/
async function fetchJishoBufferData(file) {
const read = util.promisify(fs.readFile);
const data = await read(file, 'utf8');
return JSON.parse(data);
}
/** /**
* Makes a delayed kanji search request in order not to overload the server. * Makes a delayed kanji search request in order not to overload the server.
* @param {string} kanji The character to search for * @param {string} kanji A character to search for
* @param {number} delay Number of milliseconds delay to the request * @param {number} delay A number of milliseconds delay to the request
* @return {promise} A promise that's going to run a request after the specified delay * @return {promise} A promise that's going to run a request after the specified delay
*/ */
async function delayedJishoCall(kanji, delay) { async function delayedJishoCall(kanji, delay) {
@ -43,4 +54,5 @@ async function fetchKanjiFromJisho(kanjiArray) {
} }
exports.fetchKanjiFromTxt = fetchKanjiFromTxt; exports.fetchKanjiFromTxt = fetchKanjiFromTxt;
exports.fetchJishoBufferData = fetchJishoBufferData;
exports.fetchKanjiFromJisho = fetchKanjiFromJisho; exports.fetchKanjiFromJisho = fetchKanjiFromJisho;