Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove exeSync #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"put.io-v2": "git://github.com/343max/put.io.js-v2.git",
"argv": "*",
"underscore": "*",
"execSync": "*",
"node-pushover": "*"
}
}
}
82 changes: 48 additions & 34 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ var PutIO = require('put.io-v2');
var argv = require( 'argv' );
var _ = require('underscore');
var fs = require('fs');
var execSync = require('execSync');
var Pushover = require('node-pushover');
var TVShowMatcher = require('./tvshowdir');
var config = require('./config');
var spawn = require('child_process').spawn;


var push = null;
if (config.pushpin.enabled) {
Expand Down Expand Up @@ -48,6 +49,48 @@ if (tvShowDir) {
matcher = function() {};
}

function downloadFiles(files) {
var file_nodes = files;
var file = file_nodes.pop();
if (file) {
var fileDir = localPath;
var tvshow = matcher(file.name);

if (tvshow) fileDir = tvshow.path;

var finalPath = fileDir + '/' + file.name;

fs.stat(finalPath, function gotFileStat(err, stat) {
if (stat && stat.size == file.size) {
// this file was allready downloaded - so we might delete it
console.log('deleting ' + file.name + ' from put.io');
api.files.delete(file.id);
if (file_nodes > 0) {
downloadFiles(file_nodes);
}
};
var shellCommand = config.ariaPath + ' --file-allocation=none --continue=true -d "' + fileDir + '" "' + api.files.download(file.id) + '"';
var localFilePath = localPath + '/' + file.name;
console.log('downloading ' + localFilePath + '...');
console.log(shellCommand);
var obj = spawn('bash', ['-c', shellCommand], { stdio: 'inherit' });

obj.on('exit',function(code,signal) {
var afterStat = fs.statSync(finalPath);
deleteShowIfCompleted(api, file, afterStat);
downloadFiles(file_nodes);
if (file.size > 20 * 1024 * 1024) {
if (tvshow) {
push.send('put.io sync', 'downloaded an episode of ' + tvshow.name);
} else {
push.send('put.io sync', 'Downloaded ' + file.name);
}
}
});
});
}
}

function deleteShowIfCompleted(api, fileNode, stat) {
if (stat && stat.size == fileNode.size) {
// this file was allready downloaded - so we might delete it
Expand All @@ -68,43 +111,15 @@ function listDir(directoryId, localPath, isChildDir) {
}
} else {
fs.mkdir(localPath, 0766, function dirCreated() {
var files = [];
_.each(data.files, function eachFile(fileNode) {
var localFilePath = localPath + '/' + fileNode.name;

if (fileNode.content_type == 'application/x-directory') {
listDir(fileNode.id, localFilePath, true);
listDir(fileNode.id, localPath, true);
} else {
var fileDir = localPath;
var tvshow = matcher(fileNode.name);

if (tvshow) fileDir = tvshow.path;

var finalPath = fileDir + '/' + fileNode.name;

fs.stat(finalPath, function gotFileStat(err, stat) {
if (deleteShowIfCompleted(api, fileNode, stat)) {
return;
}

var shellCommand = config.ariaPath + ' -d "' + fileDir + '" "' + api.files.download(fileNode.id) + '"';

console.log('downloading ' + localFilePath + '...');
console.log(shellCommand);
var result = execSync.stdout(shellCommand);

var afterStat =fs.statSync(finalPath);
deleteShowIfCompleted(api, fileNode, afterStat);

if (fileNode.size > 20 * 1024 * 1024) {
if (tvshow) {
push.send('put.io sync', 'downloaded an episode of ' + tvshow.name);
} else {
push.send('put.io sync', 'Downloaded ' + fileNode.name);
}
}
});
files.push(fileNode);
}
});
downloadFiles(files);
});
}
});
Expand All @@ -124,4 +139,3 @@ if (fs.existsSync(lockFile)) {
listDir(directoryId, localPath, false);

}