-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
139 lines (92 loc) · 2.61 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var fs = require('fs');
var commander = require('commander');
var async = require('async');
var srtParser = require('subtitles-parser');
commander.version('0.0.1')
.option('-c --comment [comment Msg]', 'Add comment')
.option('-f --folder [folderId]', 'Get links from GDrive folder')
.option('-o --output [filename]', 'Save output into the file')
.parse(process.argv);
var comment = commander.comment;
var filenames = commander.args;
var folderId = commander.folder;
var outFile = commander.output;
var htmlFactory = require('./htmlFactory');
var gDrive = require('./gDrive');
if (!folderId || !filenames) {
commander.help();
}
async.waterfall([
// Auth googleapis
function (cb) {
var credFile = './cred.json';
gDrive.auth(credFile, cb);
},
// Parse file
function (cb) {
async.map(filenames, function (filename, next) {
var res = filename.match(/([^\/\\]+)\.\w*/);
if (!res) {
return setImmediate(function () {
next('Invalid filename');
});
}
var basename = res[1];
var content = fs.readFileSync(filename);
var srt = srtParser.fromSrt(content.toString());
if (srt.length == 0) {
return setImmediate(function () {
next('Invalid Srt File');
});
}
var lastSrt = srt.slice(-1)[0];
var duration = lastSrt.endTime.replace(/,.*/, '');
var length = lastSrt.id;
var html = htmlFactory.generateHTML(comment, srtParser.toSrt(srt));
return setImmediate(function () {
next(null, {
title: basename,
html: html,
duration: duration,
length: length
});
});
}, cb);
},
function (subtitles, cb) {
async.map(subtitles, function (subtitle, next) {
var html = subtitle.html;
var title = subtitle.title;
gDrive.upload(title, html, folderId, function (err, link) {
if (err) {
return next(err);
}
subtitle.link = link;
next(null, subtitle);
});
}, function (err, results) {
if (err) {
cb(err);
return;
}
cb(null, results);
});
},
function (results, cb) {
// Prepare csv
var csv = results.map(function (result) {
return [result.title, result.duration, result.length, result.link].join(',');
}).join('\n');
if (outFile) {
return fs.writeFile(outFile, csv, cb);
}
console.log(csv);
cb(null);
}
], function (err, results) {
if (err) {
console.log('Error:'+JSON.stringify(err));
} else {
console.log('Done');
}
});