const superagent = require('superagent');
const crypto = require('crypto');
const csvParse = require('csv-parse');
const fs = require('fs');
const path = require('path');
const serverConfig = {
protocol: 'https:',
hostname: 'translate.rozetta-api.io',
port: 443
};
const authConfig = {
accessKey: 'ACCESS_KEY',
secretKey: 'SECRET_KEY',
nonce: Date.now().toString()
};
const fsPromises = fs.promises;
const DATA_FOLDER = path.join(__dirname);
const normalizeText = (text) => {
const normalized = text.trim();
if (normalized.startsWith('"') && normalized.endsWith('"')) {
return normalized.replace(/^"/, '')
.replace(/"$/, '');
}
return normalized;
};
const extractLines = async (content) => new Promise((resolve, reject) => {
csvParse(
content,
{
delimiter: ',',
relax: true,
trim: true,
},
(error, lines) => {
if (error !== undefined) {
reject(error);
return;
}
resolve(lines);
},
);
});
const linesToEntries = (fromLang, toLang, records) => {
const entries = records.filter((record) => record.length >= 2)
.map((record) => ({
fromLang,
fromText: normalizeText(record[0]),
toLang,
toText: record[1],
}));
return entries;
};
const sendRequest = (serverConfig, authConfig, payload) => {
const url = '/api/v1/dictionary/bulk';
const signature = authUtils.generateSignature(
url,
authConfig.secretKey,
authConfig.nonce
);
const headers = {
accessKey: authConfig.accessKey,
signature,
nonce: authConfig.nonce,
};
return superagent.post(`${serverConfig.protocol}//${serverConfig.hostname}${url}`)
.set(headers)
.send(payload)
.then((res) => {
return res.body;
}).catch((err) => {
return err.message;
});
};
const main = async () => {
try {
const csvFiles = await fsPromises.readdir(DATA_FOLDER);
const allEntries = [];
for (const file of csvFiles) {
if (!file.endsWith('.csv')) {
continue;
}
const filePath = path.join(DATA_FOLDER, file);
/*
csv file like:
square,広場
rabbit,ラビット
*/
const content = await fsPromises.readFile(
filePath,
{
encoding: 'utf8',
},
);
const lines = await extractLines(content);
// set all lines lang from en to ja
const entries = linesToEntries(
'en',
'ja',
lines,
);
if (entries.length === 0) {
continue;
}
allEntries.push(...entries);
}
const response = await sendRequest(
serverConfig,
authConfig,
allEntries
);
console.log('Server response:');
console.log(response);
} catch (error) {
console.error(error);
}
};
main();