Created
December 6, 2019 20:07
-
-
Save chsnt/0d669b0a9509086dce829f8d0ae7257c to your computer and use it in GitHub Desktop.
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
//require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS }) | |
const translateG = require('./translate'); | |
const getFiles = require('./getFiles'); | |
const fs = require('fs-extra'); | |
const cliProgress = require('cli-progress'); | |
const targetLang = 'ru' | |
// входная папка | |
const dir = '../data/boards/sciences/' | |
const arrFieldsForTranslate = [ | |
'selftext', | |
// 'body', | |
'body_html', | |
'title', | |
'subreddit_loc', | |
] | |
const filePath = function (p, lang) { | |
if (lang) return `../data/boards-${lang}/${p.subreddit.display_name}/${p.id}.json` | |
return `../data/boards/${p.subreddit.display_name}/${p.id}.json` | |
} | |
const pbSubr = new cliProgress.SingleBar({ | |
format: '{dir}: [{bar}] {percentage}% | {value}/{total} ', | |
hideCursor: true | |
}, cliProgress.Presets.shades_classic); | |
//let promArr = [] | |
const startApp = () => { | |
getFiles(dir, function (err, files) { | |
pbSubr.start(files.length, 0); | |
files.forEach( file => { | |
fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\')) | |
.then(exists => { | |
if(!exists) { | |
translatorWithErrorHandle (file); | |
} else { | |
pbSubr.increment() | |
pbSubr.update(undefined, {dir: file.split('\\')[3]}); | |
} | |
}) | |
}); | |
}); | |
} | |
const translatorWithErrorHandle = (file) => { | |
translator (file) | |
.catch(err => { | |
if (err.code == 'ETIMEDOUT' || err.code == 'ECONNRESET') { | |
console.log(err.code) | |
translatorWithErrorHandle(file) | |
} else { | |
console.error(err.message) | |
} | |
}) | |
} | |
const translator = (fileAddress) => { | |
return new Promise ((resolve, reject) =>{ | |
fs.readJson(fileAddress) | |
.then( async (post) => { | |
let double = JSON.parse(JSON.stringify(post)) | |
await asyncForIn( post, async field => { | |
if( arrFieldsForTranslate.indexOf(field) >-1 && post[field] != "") { | |
try { | |
double[field] = await translateG (post[field], targetLang) | |
if (field === 'body_html') { | |
double['body_html'] = double['body_html'].replace(/<\/\s/g, '</').replace(/<Ч\s\//g, '</') | |
} | |
} catch(err) { | |
reject(err) | |
} | |
} else if( field === 'replies' && post.replies ) { | |
try { | |
if (post.replies.length) double.replies = await comTranslate (post.replies) | |
} catch(err) { | |
reject(err) | |
} | |
} | |
}) | |
fs.outputFile( filePath(double, targetLang), JSON.stringify( double )) | |
.then( r => {pbSubr.increment() ; resolve('done')}) | |
.catch(err => console.log(`ERROR! (${err}) -> ${double.subreddit.display_name}/${double.id}`)) | |
}) | |
.catch(err => { | |
reject(err) | |
}) | |
}) | |
} | |
const comTranslate = async (arr) => { | |
let newArr = [] | |
let obj = {} | |
await asyncForEach ( arr, async (com) => { | |
await asyncForIn( com, async field => { | |
obj = com | |
if( arrFieldsForTranslate.indexOf(field) >-1 ) { | |
obj[field]= await translateG (com[field], targetLang) | |
if (field === 'body_html') { | |
obj['body_html'] = obj['body_html'].replace(/<\/\s/g, '</').replace(/<Ч\s\//g, '</') | |
} | |
} else if( field === 'replies' && com.replies ) { | |
if (com.replies.length) obj.replies = await comTranslate (com.replies) | |
} | |
}) | |
newArr.push(obj) | |
}); | |
return newArr | |
} | |
async function asyncForEach(array, callback) { | |
for (let index = 0; index < array.length; index++) { | |
await callback(array[index], index, array); | |
} | |
} | |
async function asyncForIn(object, callback) { | |
//for (let index = 0; index < object.length; index++) { | |
for (field in object) { | |
await callback( field, object); | |
} | |
} | |
startApp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment