Skip to content

Instantly share code, notes, and snippets.

@deptno
Last active March 9, 2016 01:31
Show Gist options
  • Save deptno/1ac5f8e1678d643e5d4f to your computer and use it in GitHub Desktop.
Save deptno/1ac5f8e1678d643e5d4f to your computer and use it in GitHub Desktop.
translator
#!/usr/bin/env node
var https = require('https');
var client_id = process.env.NAVER_CLIENT_ID;
var client_secret = process.env.NAVER_SECRET;
var host = 'openapi.naver.com';
var port = 443;
var uri = '/v1/language/translate';
var text = process.argv.slice(2).join(" ");
if (!client_id || !client_secret) {
console.error('export NAVER_CLIENT_ID=YOUR_CLINET_ID');
console.error('export NAVER_CLIENT_ID=YOUR_CLIENT_SECRET');
process.exit();
}
var data = require('querystring').stringify({
source: 'en',
target: 'ko',
text: text
});
var options = {
host: host,
port: port,
path: uri,
method: 'POST',
headers: {
'X-Naver-Client-Id':client_id,
'X-Naver-Client-Secret': client_secret,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + res.headers);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log(JSON.parse(chunk).message.result.translatedText);
});
});
req.write(data);
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment