Finished API calls

This commit is contained in:
dabura667 2015-07-21 03:27:15 +09:00
commit bc0bddd5cb
26 changed files with 3526 additions and 2002 deletions

View file

@ -2,77 +2,54 @@
'use strict';
var fs = require('fs')
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');
var path = require('path');
var https = require('https');
var AdmZip = require('adm-zip');
var bhttp = require("bhttp");
var crowdin_identifier = 'copay'
var crowdin_file_name1 = 'template.pot'
var local_file_name1 = './po/template.pot'
var crowdin_file_name1 = 'files[template.pot]'
var local_file_name1 = path.join(__dirname, 'po/template.pot')
var local_file1 = fs.createReadStream(local_file_name1)
var crowdin_file_name2 = 'template.pot'
var local_file_name2 = './docs/appstore_en'
var crowdin_file_name2 = 'files[appstore/appstore_en.txt]'
var local_file_name2 = path.join(__dirname, 'docs/appstore_en.txt')
var local_file2 = fs.createReadStream(local_file_name2)
var crowdin_file_name3 = 'files[appstore/updateinfo_en.txt]'
var local_file_name3 = path.join(__dirname, 'docs/updateinfo_en.txt')
var local_file3 = fs.createReadStream(local_file_name3)
// obtain the crowdin api key
var crowdin_api_key = '';
fs.readFile('./crowdin_api_key.txt', 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
crowdin_api_key = data;
});
var crowdin_api_key = fs.readFileSync(path.join(__dirname, 'crowdin_api_key.txt')).slice(3) //slicing utf-8 BOM
//console.log('api key: ' + crowdin_api_key);
if (crowdin_api_key != '') {
var host_url = 'http://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key;
var post_data = querystring.stringify({
'files[' + crowdin_file_name1 + ']' : '@' + local_file_name1,
'files[' + crowdin_file_name2 + ']' : '@' + local_file_name2
});
var post_options = {
host: host_url,
port: '80',
path: '',
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Content-Length': post_data.length
}
var payload = {
'files[template.pot]': local_file1,
'files[appstore/appstore_en.txt]': local_file2,
'files[appstore/updateinfo_en.txt]': local_file3
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(post_data);
// This post updates the english files on crowdin.
// https://crowdin.com/page/api/update-file
post_req.end();
bhttp.post('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key, payload, {}, function(err, response) {
console.log("Response from hosting service:", response.body.toString());
})
// This call will tell the server to generate a new zip file for you based on most recent translations.
http.get('http://api.crowdin.com/api/project/' + crowdin_identifier + '/export?key=' + crowdin_api_key, function(res) {
console.log("Got response: " + res.statusCode);
https.get('https://api.crowdin.com/api/project/' + crowdin_identifier + '/export?key=' + crowdin_api_key, function(res) {
console.log("Export Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
console.log("Export Got error: " + e.message);
});
};
// Download most recent translations for all languages.
http.get('http://crowdin.com/download/project/' + crowdin_identifier + '.zip', function(res) {
https.get('https://crowdin.com/download/project/' + crowdin_identifier + '.zip', function(res) {
var data = [], dataLen = 0;
res.on('data', function(chunk) {
@ -80,20 +57,13 @@ http.get('http://crowdin.com/download/project/' + crowdin_identifier + '.zip', f
dataLen += chunk.length;
}).on('end', function() {
var buf = new Buffer(dataLen);
for (var i=0, len = data.length, pos = 0; i < len; i++) {
data[i].copy(buf, pos);
pos += data[i].length;
}
var zip = new AdmZip(buf);
var zipEntries = zip.getEntries();
console.log(zipEntries.length)
for (var i = 0; i < zipEntries.length; i++) {
// parse each file from the zip, ex. http://crowdin.com/download/project/electrum.zip
// the filename for each file is the same, but they are separated into locale folder names.
// those foldernames might be able to be used to automatically rename each file to 'locale.po' like current.
console.log(zip.readAsText(zipEntries[i]));
for (var i=0, len = data.length, pos = 0; i < len; i++) {
data[i].copy(buf, pos);
pos += data[i].length;
};
var zip = new AdmZip(buf);
zip.extractAllTo("./", true);
console.log("Done extracting ZIP file.");
});
});