Skip to content

Instantly share code, notes, and snippets.

@deptno
Last active August 29, 2015 14:22
Show Gist options
  • Save deptno/ed464c2ce3d2c5a7f490 to your computer and use it in GitHub Desktop.
Save deptno/ed464c2ce3d2c5a7f490 to your computer and use it in GitHub Desktop.
[MDS] ts restamper
#!/usr/bin/env node
// @source ts folder, target folder, start today, delete based utc
var pathSrc = 'E:/MDS_1.6/transport_streams';
var pathDst = 'U:/DTG'
var startToDay = false;
var deleteBasedUtc = false;
var spawn = require('child_process').spawn;
var execSync = require('child_process').execSync;
var path = require('path');
var fs = require('fs');
var restamper = 'restamper.py',
pathSrc = path.normalize(pathSrc);
pathDst = path.normalize(pathDst);
console.log(path.join(pathSrc, restamper))
if (fs.existsSync(path.join(pathSrc, restamper))) {
restamper = path.join(pathSrc, restamper);
}
function getArrayTs(tsPath) {
return fs.readdirSync(tsPath).filter(function(file) {
var file = path.join(tsPath, file);
return fs.lstatSync(file).isFile() && path.extname(file) === '.ts';
}).map(function(file) {
return path.join(tsPath, file);
});
}
function getYYYYMMDD(isTomorrow) {
var date = isTomorrow ? new Date(Date.now() + ((24*60*60*1000)*1)) : new Date();
var howMany = 5;
var ret = [];
for (var i = 0; i < howMany; i++) {
var month = date.getMonth() + 1;
var day = date.getDate();
if (month.toString().length === 1) {
month = '0' + month;
}
if (day.toString().length === 1) {
day = '0' + day;
}
ret.push([ date.getFullYear(), month, day ].join(''));
date = new Date(date.valueOf() + (24*60*60*1000));
}
return ret;
}
function mkdir(dst) {
try {
fs.mkdirSync(dst);
} catch (ex) {
if (ex.code === 'EEXIST') {
console.info(dst + ' already exist');
return;
} else if (ex.code === 'EACCES') {
console.error(ex);
process.exit(1);
}
dst.split(path.sep).forEach(function(dir) {
if (!/^[A-z]:/.test(dir)) {
mkdir(dir);
}
});
}
}
var dates = startToDay ? getYYYYMMDD() : getYYYYMMDD(true);
var childs = 0;
var run = function() {
var targetday = dates.shift();
if (!targetday) {
process.exit();
}
mkdir(path.join(pathDst, 'system_' + targetday));
getArrayTs(pathSrc).forEach(function(file) {
console.log('>filename: ' + file);
var dirname = path.dirname(file);
var filename = path.basename(file);
var param = [
restamper,
'-i',
file,
'-o',
path.join(pathDst, 'system_' + targetday, filename),
'-d',
targetday
];
var ps = spawn('python', param);
console.log(param.join(' '));
ps.stdout.on('data', function(data) { console.log('' + data); });
ps.stderr.on('data', function(data) { console.log('error > ' + data); });
ps.on('close', function(code) { if (code !== 0) { console.log('process exited with code ' + code); }
if (--childs <= 0) { run(); };
});
childs++;
});
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment