-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-subs
executable file
·90 lines (76 loc) · 2.03 KB
/
update-subs
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
#! /usr/bin/env node
//
// Find all video files recursively and download an srt for them
//
// search for video files below this point
var root = "/volume1/video"
var fs = require('fs')
var domain = require('domain')
var path = require('path')
var subdb = require('/volume1/@appstore/Node.js/usr/lib/node_modules/subdb-cli/lib/subdb-cli.js')
//var recursive = require('/root/readdirr.js');
//
// thanks to https://gist.github.com/simov/4132717
//
var dir = null, file = null;
function readdirr (dpath, cb) {
dir = [], file = [];
dir.push(dpath);
function loop (i) {
if (i == dir.length) return cb(null, dir, file);
fs.readdir(dir[i], function (err, files) {
if (err) return cb(err);
getstat(dir[i], files, function (err) {
if (err) return cb(err);
loop(++i);
});
});
}
loop(0);
}
// my version skips symbolic links
function getstat (dpath, files, cb) {
function loop (i) {
if (i == files.length) return cb();
var fpath = path.join(dpath, files[i]);
fs.lstat(fpath, function (err, stats) {
if (err) return cb(err);
if (!stats.isSymbolicLink()) {
if (stats.isDirectory()) {
dir.push(fpath);
} else {
file.push(fpath);
}
}
loop(++i);
});
}
loop(0);
}
// callback to readdirr, which will fire at every directory
var cb = function (err, dir, files) {
for (i in files) {
var file=files[i]
var ext
if ((extpos = file.search(/\.(m4v|avi|mkv|mp4)$/)) > -1) {
var body = file.substr(0, extpos);
console.log("considering " + files[i])
var subfile = body + ".srt"
if (fs.existsSync(subfile)) {
console.log("skipping subfile");
}
else {
console.log("fetching " + file)
d = domain.create();
d.on('error', function(err) {
console.error(err);
});
subdb.download(file, "en")
}
}
}
if (err) {
console.log(err);
}
};
readdirr(root, cb);