Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARASAAC scripts to get symbols and data #1321

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}