Skip to content

Commit

Permalink
Merge pull request #1321 from cboard-org/arasaac-scripts
Browse files Browse the repository at this point in the history
ARASAAC scripts to get symbols and data
  • Loading branch information
martinbedouret authored Dec 20, 2022
2 parents 4cd45ad + b20769a commit 8fba1bd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
80 changes: 80 additions & 0 deletions scripts/arasaac-create-files.js
Original file line number Diff line number Diff line change
@@ -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 => {
var 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;
}
});
59 changes: 59 additions & 0 deletions scripts/arasaac-download-symbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fs = require('fs');
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);
}

0 comments on commit 8fba1bd

Please sign in to comment.