forked from beatfreaker/subdownloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
114 lines (108 loc) · 2.85 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/bin/env node
'use strict';
var subd = require('./');
var fs = require('fs');
var path = require('path');
var Q = require('q');
var isVideo = require('is-video');
var meow = require('meow');
var ProgressBar = require('progress');
var chalk = require('chalk');
var p = process.cwd();
var filesArray = [];
var cli = meow({
help: [
'Help\n',
' > subdownload\n',
' To download subtitles for all the files present in current folder\n',
' > subdownload --deep\n',
' To download subtitles for all the files present in current folder as well as in subfolder\n',
' > subdownload "File Name.mkv"\n',
' To download subtitles for specific file\n',
' > subdownload "File1.mkv" "File2.avi" .... "Filen"\n',
' To download subtitles for more then one file\n'
]
});
var filterFiles = function (files) {
try {
return files.filter(function (file) {
return fs.statSync(file).isFile() && isVideo(file);
});
} catch (err) {
console.log('Please check if all the file name given exists or not.');
}
};
var getFileList = function () {
var fileList;
var counter = 1;
var defered = Q.defer();
fs.readdir(p, function (err, files) {
if (err) {
throw err;
}
if (cli.input.length === 0) {
fileList = filterFiles(files);
} else {
fileList = filterFiles(cli.input);
}
if (fileList) {
fileList.forEach(function (file) {
filesArray.push(file);
counter++;
if (counter > fileList.length) {
defered.resolve(filesArray);
}
});
}
});
return defered.promise;
};
var getDeepFiles = function (currentDir) {
fs.readdirSync(currentDir).forEach(function (name) {
var filePath = path.join(currentDir, name);
var stat = fs.statSync(filePath);
if (stat.isFile() && isVideo(filePath)) {
filesArray.push(filePath);
} else if (stat.isDirectory()) {
getDeepFiles(filePath);
}
});
};
var startDownload = function (filesArray) {
var bar;
var progressBarMessage;
var green = '\u001b[42m \u001b[0m';
subd.on('processing', function(obj) {
//console.log('Processing :: ' + obj.fileName + ' ' + progressBarMessage);
progressBarMessage = 'Downloading ' + chalk.bgBlue.bold(obj.fileName) + ' [:bar] :percent :etas | :current of :total bytes';
})
.on('response', function(obj, res) {
var len = parseInt(res.headers['content-length'], 10);
bar = new ProgressBar(progressBarMessage, {
complete: green,
incomplete: '-',
width: 10,
total: len
});
})
.on('data', function(obj, chunk) {
bar.tick(chunk.length);
})
.on('end', function(obj, res) {
if(res.statusCode !== '200') {
console.log('Sorry no subitles were found for ====> ' + chalk.bgRed.bold(obj.fileName));
}console.log('\n');
});
subd.subdownload(filesArray);
};
var getPara = function () {
if (cli.flags.deep) {
getDeepFiles(p);
startDownload(filesArray);
} else {
getFileList().then(function (data) {
startDownload(data);
});
}
};
getPara();