-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1321 from cboard-org/arasaac-scripts
ARASAAC scripts to get symbols and data
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |