From 2d76943fc0e9f48446981d1a141d496ab6efd48d Mon Sep 17 00:00:00 2001 From: Martin Bedouret Date: Fri, 16 Dec 2022 19:54:18 -0300 Subject: [PATCH 1/2] initial version --- scripts/arasaac-create-files.js | 80 +++++++++++++++++++++++++++++ scripts/arasaac-download-symbols.js | 60 ++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 scripts/arasaac-create-files.js create mode 100644 scripts/arasaac-download-symbols.js diff --git a/scripts/arasaac-create-files.js b/scripts/arasaac-create-files.js new file mode 100644 index 000000000..45bada92f --- /dev/null +++ b/scripts/arasaac-create-files.js @@ -0,0 +1,80 @@ +const { writeFile } = require('fs'); +const https = require('https'); + +const ARASAAC_BASE_PATH_API = 'https://api.arasaac.org/api/'; + +const locales = [ + 'ar', + 'bg', + 'de', + 'el', + 'en', + 'es', + 'fr', + 'he', + 'hr', + 'hu', + 'it', + 'ko', + 'mk', + 'nl', + 'pl', + 'pt', + 'ro', + 'ru', + 'sk', + 'sq', + 'sv', + 'sr', + 'uk', + 'zh' +]; + +console.log('Fetching symbol data from ARASAAC API .....'); + +locales.forEach(async locale => { + const pictosPath = `pictograms/all/${locale}`; + let jsonData = []; + try { + https.get(ARASAAC_BASE_PATH_API + pictosPath, res => { + let data = []; + + res.on('data', chunk => { + data.push(chunk); + }); + + res.on('end', () => { + console.log('. . . SUCCESS Fetching symbol data for locale ' + locale); + jsonData = JSON.parse(Buffer.concat(data).toString()); + const result = []; + jsonData.forEach(element => { + keywords = []; + element['keywords'].forEach(kw => { + keywords.push(kw['keyword']); + }); + const picto = { + id: element['_id'], + kw: keywords + }; + result.push(picto); + }); + + writeFile( + './src/api/arasaac/' + locale + '.json', + JSON.stringify(result), + error => { + if (error) { + console.log('An error has occurred ', error); + return; + } + console.log('Data written successfully to disk'); + } + ); + }); + }); + } catch (err) { + console.log('ERROR Failed to fetch symbol data for locale ' + locale); + console.log(err.message); + return; + } +}); diff --git a/scripts/arasaac-download-symbols.js b/scripts/arasaac-download-symbols.js new file mode 100644 index 000000000..ee6bf1df1 --- /dev/null +++ b/scripts/arasaac-download-symbols.js @@ -0,0 +1,60 @@ +const fs = require('fs'); +const Path = require('path'); +const Axios = require('axios'); + +const ARASAAC_BASE_PATH_API = 'https://api.arasaac.org/api/'; +const jsonData = require('../src/api/arasaac/es.json'); +const pictosPath = `pictograms/`; + +console.log('Fetching symbol data from ARASAAC API .....'); + +async function downloadFile(fileUrl, outputLocationPath) { + const writer = fs.createWriteStream(outputLocationPath); + + return Axios({ + method: 'get', + url: fileUrl, + responseType: 'stream' + }).then(response => { + //ensure that the user can call `then()` only when the file has + //been downloaded entirely. + + return new Promise((resolve, reject) => { + response.data.pipe(writer); + let error = null; + writer.on('error', err => { + error = err; + writer.close(); + reject(err); + }); + writer.on('close', () => { + if (!error) { + resolve(true); + } + //no need to call the reject here, as it will have been called in the + //'error' stream; + }); + }); + }); +} + +for (let i = 1; i < Math.trunc(jsonData.length / 100); i++) { + (function(ind) { + setTimeout(function() { + for (let j = (i - 1) * 100; j < i * 100; j++) { + const picto = jsonData[j]; + console.log(j); + try { + const apipath = ARASAAC_BASE_PATH_API + pictosPath + picto['id']; + console.log(apipath); + const path = './src/api/arasaac/symbols/' + picto['id'] + '.png'; + downloadFile(apipath, path).then(console.log('ok!')); + } catch (err) { + console.log('ERROR Failed to fetch symbol data'); + console.log(err.message); + return; + } + } + }, 1000 + 7000 * ind); + })(i); +} From b20769a9046ee3ebe185227e9f0dd4dac8743c27 Mon Sep 17 00:00:00 2001 From: Martin Bedouret Date: Tue, 20 Dec 2022 16:08:47 -0300 Subject: [PATCH 2/2] Fix issues --- scripts/arasaac-create-files.js | 2 +- scripts/arasaac-download-symbols.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/arasaac-create-files.js b/scripts/arasaac-create-files.js index 45bada92f..cc50a1dc4 100644 --- a/scripts/arasaac-create-files.js +++ b/scripts/arasaac-create-files.js @@ -48,7 +48,7 @@ locales.forEach(async locale => { jsonData = JSON.parse(Buffer.concat(data).toString()); const result = []; jsonData.forEach(element => { - keywords = []; + var keywords = []; element['keywords'].forEach(kw => { keywords.push(kw['keyword']); }); diff --git a/scripts/arasaac-download-symbols.js b/scripts/arasaac-download-symbols.js index ee6bf1df1..7b22738d8 100644 --- a/scripts/arasaac-download-symbols.js +++ b/scripts/arasaac-download-symbols.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const Path = require('path'); const Axios = require('axios'); const ARASAAC_BASE_PATH_API = 'https://api.arasaac.org/api/';