downloader.js
2.18 KB
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
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var request = require('request');
// First, look for the download link.
/*jshint maxlen:false */
var dir, filePath;
var isWin = (process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows') ? true : false;
var defaultBin = path.join(__dirname, '..', 'bin');
var defaultPath = path.join(defaultBin, 'details');
var url = 'https://yt-dl.org/downloads/latest/youtube-dl';
function download(url, callback) {
'use strict';
var status = null;
// download the correct version of the binary based on the platform
url = exec(url);
request.get(url, { followRedirect: false }, function (err, res) {
if (res.statusCode !== 302) {
return callback(new Error('Did not get redirect for the latest version link. Status: ' + res.statusCode));
}
var url = res.headers.location;
var downloadFile = request.get(url);
var newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[1];
downloadFile.on('response', function response(res) {
if (res.statusCode !== 200) {
status = new Error('Response Error: ' + res.statusCode);
return;
}
downloadFile.pipe(fs.createWriteStream(filePath, { mode: 493 }));
});
downloadFile.on('error', function error(err) { callback(err); });
downloadFile.on('end', function end() { callback(status, newVersion); });
});
}
function exec(path) {
'use strict';
return (isWin) ? path + '.exe' : path;
}
function createBase(binDir) {
'use strict';
dir = (binDir) ? binDir : defaultBin;
mkdirp.sync(dir);
if (binDir) { mkdirp.sync(defaultBin); }
filePath = path.join(dir, exec('youtube-dl'));
}
function downloader(binDir, callback) {
'use strict';
if (typeof binDir === 'function') {
callback = binDir;
binDir = null;
}
createBase(binDir);
download(url, function error(err, newVersion) {
if (err) { return callback(err); }
fs.writeFileSync(defaultPath, JSON.stringify({ version: newVersion, path: ((binDir) ? filePath : binDir), exec: exec('youtube-dl') }), 'utf8');
callback(null, 'Downloaded youtube-dl ' + newVersion);
});
}
module.exports = downloader;