Skip to content

Instantly share code, notes, and snippets.

@cyanolupus
Last active February 28, 2022 02:36
Show Gist options
  • Save cyanolupus/a21a3adaf37b869f5766aa22e233748b to your computer and use it in GitHub Desktop.
Save cyanolupus/a21a3adaf37b869f5766aa22e233748b to your computer and use it in GitHub Desktop.
epgstationでいい感じにrcloneのプログレスバーを表示するやつ
const spawn = require('child_process').spawn;
const execFile = require('child_process').execFile;
const rclone = '/usr/bin/rclone';
const input = process.env.INPUT;
const channelname = process.env.CHANNELNAME;
const channelid = parseInt(process.env.CHANNELID);
const args = [''];
const configpath = '/rclone/rclone.conf';
const targetdir = 'remote:/your/dir/'+channelname+'/';
execFile(rclone, ['mkdir','--config', configpath, targetdir,'--retries-sleep','13m','--retries','7']);
(async () => {
const child = spawn(rclone, ['move', '--config', configpath,
input,targetdir,
'--use-json-log', '--stats', '1s', '--log-level', 'INFO',
'--retries-sleep','13m','--retries','7']);
/**
* エンコード進捗表示用に標準出力に進捗情報を吐き出す
* 出力する JSON
* {"type":"progress","percent": 0.8, "log": "view log" }
*/
child.stderr.on('data', data => {
let strbyline = String(data).split('\n');
for (let i = 0; i < strbyline.length; i++) {
let json = JSON.parse(strbyline[i] || "null");
if (json == null) continue;
const progress = {};
progress['cur'] = parseFloat(json.stats.bytes);
progress['total'] = parseFloat(json.stats.totalBytes);
progress['speed'] = parseFloat(json.stats.speed);
progress['eta'] = parseInt(json.stats.eta);
if ( progress.total == 0 ) continue;
// 進捗率 1.0 で 100%
const percent = progress.cur / progress.total;
let log = 'speed= ';
if (progress.speed >= 1024*1024*1024) {
log += Math.floor(progress.speed/(1024*1024*1024));
log += ' GB';
} else if (progress.speed >= 1024*1024) {
log += Math.floor(progress.speed/(1024*1024));
log += ' MB';
} else if (progress.speed >= 1024) {
log += Math.floor(progress.speed/1024);
log += ' KB';
} else {
log += Math.floor(progress.speed);
log += ' B';
}
log += '/s eta= ';
if (progress.eta >= 3600) {
log += Math.floor(progress.eta/60/60);
log += 'h';
log += Math.floor(progress.eta/60)%60;
log += 'm';
log += Math.floor(progress.eta)%60;
log += 's';
} else if (progress.eta >= 60) {
log += Math.floor(progress.eta/60)%60;
log += 'm';
log += Math.floor(progress.eta)%60;
log += 's';
} else {
log += Math.floor(progress.eta)%60;
log += 's';
}
console.log(JSON.stringify({ type: 'progress', percent: percent, log: log }));
}
});
child.on('error', err => {
console.error(err);
throw new Error(err);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment