Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
fixed downloader if user passed in a custom bin dir
Browse files Browse the repository at this point in the history
  • Loading branch information
EragonJ committed May 10, 2016
1 parent 25ed267 commit 2954eed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.srt
*.mp3
*.m4a
*.swp
.DS_Store
node_modules
bin
83 changes: 50 additions & 33 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ var request = require('request');

// First, look for the download link.
/*jshint maxlen:false */
var dir, filePath;
var binPath;
var filePath;
var detailsPath;

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 defaultBinPath = path.join(__dirname, '..', 'bin');
var defaultDetailsPath = path.join(defaultBinPath, 'details');
var regexp = /https:\/\/yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/;
var url = 'https://rg3.github.io/youtube-dl/download.html';

function download(link, callback) {

function download(link, to, callback) {
'use strict';

var downloadFile = request.get(link);
var status = null;

Expand All @@ -23,65 +26,79 @@ function download(link, callback) {
status = new Error('Response Error: ' + res.statusCode);
return;
}
downloadFile.pipe(fs.createWriteStream(filePath, {mode: 493}));
downloadFile.pipe(fs.createWriteStream(to, {
mode: 493
}));
});

downloadFile.on('error', function error(err) { callback(err); });

downloadFile.on('end', function end() { callback(status); });
downloadFile.on('error', function error(err) {
callback(err);
});

downloadFile.on('end', function end() {
callback(status);
});
}

function exec(path) {
'use strict';
return (isWin) ? path + '.exe' : path;
}

function createBase(binDir) {
function downloader(userBinPath, callback) {
'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;
if (typeof userBinPath === 'function') {
callback = userBinPath;
userBinPath = null;
}

createBase(binDir);
// prepare needed paths
binPath = (userBinPath) ? userBinPath : defaultBinPath;
filePath = path.join(binPath, exec('youtube-dl'));
detailsPath = path.join(binPath, 'details');

// prepare needed folder
mkdirp.sync(binPath);

request.get(url, function get(err, res, body) {

if (err || res.statusCode !== 200) { return callback(err || new Error('Response Error: ' + res.statusCode)); }
if (err || res.statusCode !== 200) {
return callback(err || new Error('Response Error: ' + res.statusCode));
}

var m = regexp.exec(body);

if (!m) { return callback(new Error('Could not find download link in ' + url)); }
if (!m) {
return callback(new Error('Could not find download link in ' + url));
}

// Check if there is a new version available.
var newVersion = m[1];
var details =
fs.existsSync(detailsPath) && fs.readFileSync(detailsPath, 'utf8');

var details = fs.existsSync(defaultPath) && fs.readFileSync(defaultPath, 'utf8');

if (details) { details = JSON.parse(details); }
if (details) {
details = JSON.parse(details);
}

if (newVersion === details.version && fs.existsSync(details.path)) { return callback(null, 'Already up to date ' + newVersion); }
if (newVersion === details.version) {
return callback(null, 'Already up to date ' + newVersion);
}

var link = exec(m[0]);

download(link, function error(err) {
if (err) { return callback(err); }
fs.writeFileSync(defaultPath, JSON.stringify({version: newVersion, path: ((binDir) ? filePath : binDir), exec: exec('youtube-dl')}), 'utf8');
download(link, filePath, function error(err) {
if (err) {
return callback(err);
}
fs.writeFileSync(detailsPath, JSON.stringify({
version: newVersion,
exec: exec('youtube-dl')
}), 'utf8');
callback(null, 'Downloaded youtube-dl ' + newVersion);
});

});

}

module.exports = downloader;
2 changes: 1 addition & 1 deletion lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var detailsPath = path.join(__dirname, '..', 'bin/details'), ytdlBinary;

if (fs.existsSync(detailsPath)) {
var details = JSON.parse(fs.readFileSync(detailsPath));
ytdlBinary = (details.path) ? details.path : path.resolve(__dirname, '..', 'bin', details.exec);
ytdlBinary = path.resolve(__dirname, '..', 'bin', details.exec);
}

// Check that youtube-dl file exists.
Expand Down

0 comments on commit 2954eed

Please sign in to comment.