downloader.js
2.24 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const request = require('request')
const mkdirp = require('mkdirp')
const path = require('path')
const fs = require('fs')
const [, , ...flags] = process.argv
const isWin = flags.includes('--platform=windows') || require('./util').isWin
// First, look for the download link.
let dir, filePath
const defaultBin = path.join(__dirname, '..', 'bin')
const defaultPath = path.join(defaultBin, 'details')
const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
function download (url, callback) {
let status
// download the correct version of the binary based on the platform
url = exec(url)
request.get(url, { followRedirect: false }, function (err, res) {
if (err) return callback(err)
if (res.statusCode !== 302) {
return callback(
new Error(
'Did not get redirect for the latest version link. Status: ' +
res.statusCode
)
)
}
const url = res.headers.location
const downloadFile = request.get(url)
const 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)
})
})
}
const exec = path => (isWin ? path + '.exe' : path)
function createBase (binDir) {
dir = binDir || defaultBin
mkdirp.sync(dir)
if (binDir) mkdirp.sync(defaultBin)
filePath = path.join(dir, exec('youtube-dl'))
}
function downloader (binDir, callback) {
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'
)
return callback(null, 'Downloaded youtube-dl ' + newVersion)
})
}
module.exports = downloader