const superagent = require('superagent');
const crypto = require('crypto');
const fs = require('fs');
const AdmZip = require('adm-zip');
const authUtils = require('./utils/auth-utils');
const serverConfig = {
protocol: 'https:',
hostname: 'translate.rozetta-api.io',
port: 443
};
const authConfig = {
accessKey: 'YOUR_ACCESS_KEY',
secretKey: 'YOUR_SECRET_KEY',
nonce: new Date().getTime().toString()
};
const translateItemIds = 'your translateItemIds';
const sendRequest = (serverConfig, authConfig, translateItemIds) => {
const itemIds = translateItemIds.split(',');
const url = `/api/v1/downloads?ids=${JSON.stringify(itemIds)}`;
const signature = authUtils.generateSignature(
url,
authConfig.secretKey,
authConfig.nonce,
);
superagent.get(`${serverConfig.protocol}//${serverConfig.hostname}${url}`)
.set({
accessKey: authConfig.accessKey,
signature,
nonce: authConfig.nonce,
}).end(function (req, resp) {
if (resp.status === 200) {
fs.createWriteStream('./output.zip').write(resp.body, (error) => {
if (error) {
console.error(error);
} else {
const zip = new AdmZip('./output.zip');
zip.extractAllTo('./', true);
}
});
}
});
};
const main = async () => {
if (!translateItemIds) {
console.log("Error. please input translationItemId.");
return;
};
try {
await sendRequest(
serverConfig,
authConfig,
translateItemIds
);
} catch (error) {
console.error(error);
}
};
main();