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

Commit

Permalink
move bin path to datadir
Browse files Browse the repository at this point in the history
update tests for subtitles youtube dropped str for vtt
  • Loading branch information
przemyslawpluta committed Mar 8, 2016
1 parent f131dca commit 7ef58ef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# youtube-dl [![Build Status](https://secure.travis-ci.org/fent/node-youtube-dl.png)](http://travis-ci.org/fent/node-youtube-dl)
# youtube-dl

[![Build Status](https://secure.travis-ci.org/fent/node-youtube-dl.png)](http://travis-ci.org/fent/node-youtube-dl) [![npm version](https://badge.fury.io/js/youtube-dl.svg)](https://badge.fury.io/js/youtube-dl) [![Dependency Status](https://gemnasium.com/fent/node-youtube-dl.png)](https://gemnasium.com/fent/node-youtube-dl)
Download videos from youtube in node.js using [youtube-dl](http://rg3.github.com/youtube-dl/).

I also made a [pure Javascript youtube downloading module](https://github.com/fent/node-ytdl). The reason I'm maintaining this one is because it supports a lot more video sites besides youtube.
Expand Down
20 changes: 11 additions & 9 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var request = require('request');
var sortObj = require('sort-object');
var pkg = require('../package');
var pathEx = require('path-extra');

// First, look for the download link.
/*jshint maxlen:false */
var dir, filePath, isWin;
var dataPath = pathEx.datadir(pkg.name);
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';

Expand Down Expand Up @@ -36,16 +37,13 @@ function createBase(binDir) {
isWin = (process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows') ? true : false;
dir = (binDir) ? binDir : path.join(__dirname, '..', 'bin');
mkdirp.sync(dir, {mode: 484});
mkdirp.sync(dataPath, {mode: 484});
filePath = path.join(dir, 'youtube-dl' + ((isWin) ? '.exe' : ''));
}

function addToPkg() {
function savePath() {
'use strict';
var list = Object.keys(pkg);
list.splice(list.indexOf('main') + 1, 0, 'bin');
pkg.bin = filePath;
pkg = sortObj(pkg, list);
fs.writeFileSync(path.join(__dirname, '..', 'package.json'), JSON.stringify(pkg, null, 2), 'utf8');
fs.writeFileSync(path.join(dataPath, 'bin'), filePath, 'utf8');
}

function downloader(binDir, callback) {
Expand All @@ -58,6 +56,10 @@ function downloader(binDir, callback) {

createBase(binDir);

var ytdlBinary = fs.readFileSync(pathEx.datadir(pkg.name) + '/bin').toString();

var exists = fs.existsSync(ytdlBinary);

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

if (err || res.statusCode !== 200) { return callback(err || new Error('Response Error: ' + res.statusCode)); }
Expand All @@ -71,15 +73,15 @@ function downloader(binDir, callback) {
var verpath = path.join(dir, 'version');
var oldVersion = fs.existsSync(verpath) && fs.readFileSync(verpath, 'utf8');

if (newVersion === oldVersion) { return callback(null, 'Already up to date ' + newVersion); }
if (newVersion === oldVersion && exists) { return callback(null, 'Already up to date ' + newVersion); }

var link = m[0];
if (isWin) { link += '.exe'; }

download(link, function error(err) {
if (err) { return callback(err); }
fs.writeFileSync(verpath, newVersion);
addToPkg();
savePath();
callback(null, 'Downloaded youtube-dl ' + newVersion);
});

Expand Down
10 changes: 8 additions & 2 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ var http = require('http');
var streamify = require('streamify');
var request = require('request');
var util = require('./util');
var ytdlBinary = require('../package').bin;
var pathEx = require('path-extra');
var pkg = require('../package');

var ytdlBinary = fs.readFileSync(pathEx.datadir(pkg.name) + '/bin').toString();

// Check that youtube-dl file exists.
var exists = fs.existsSync(ytdlBinary);
if (!exists) {
console.log('ERROR: youtube-dl file in ' + path.dirname(ytdlBinary) + ' does not exist.');
console.log('ERROR: unable to locate youtube-dl in ' + path.dirname(ytdlBinary));
process.exit(1);
}

Expand Down Expand Up @@ -161,6 +164,7 @@ function call(urls, args1, args2, options, callback) {
}

var data = stdout.trim().split(/\r?\n/);

callback(null, data);
});

Expand Down Expand Up @@ -239,6 +243,7 @@ ytdl.getInfo = function getInfo(url, args, options, callback) {
defaultArgs.push('-f');
defaultArgs.push('best');
}

call(url, defaultArgs, args, options, function done(err, data) {
if (err) { return callback(err); }

Expand Down Expand Up @@ -312,6 +317,7 @@ ytdl.getSubs = function getSubs(url, options, callback) {
if (!options.warrning) {
args.push('--no-warnings');
}

call(url, args, [], { cwd: options.cwd }, function(err, data) {
if (err) { return callback(err); }

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
},
"dependencies": {
"mkdirp": "^0.5.1",
"path-extra": "^3.0.0",
"request": "^2.69.0",
"sort-object": "^3.0.1",
"streamify": "~0.2.3"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var assert = require('assert');
var video1 = 'http://www.youtube.com/watch?v=90AiXO1pAiA';
var video2 = 'https://www.youtube.com/watch?v=179MiZSibco';
var video3 = 'https://www.youtube.com/watch?v=AW8OOp2undg';
var subtitleFile = '1 1 1-179MiZSibco.en.srt';
var subtitleFile = '1 1 1-179MiZSibco.en.vtt';

vows.describe('download').addBatch({
'a video with format specified': {
Expand Down Expand Up @@ -124,6 +124,7 @@ vows.describe('download').addBatch({
'subtitles were downloaded': function(err, files) {
'use strict';
if (err) { throw err; }
console.dir(files);
assert.equal(files[0], subtitleFile);
assert.isTrue(fs.existsSync(path.join(__dirname, subtitleFile)));
fs.unlinkSync(path.join(__dirname, subtitleFile));
Expand Down
13 changes: 7 additions & 6 deletions test/getInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ vows.describe('getInfo').addBatch({
'topic': function() {
'use strict';
var video = 'http://www.youtube.com/watch?v=90AiXO1pAiA';
ytdl.getInfo(video, ['-f', '18/22/37/38'], this.callback);
ytdl.getInfo(video, ['-f', '18/43/36'], this.callback);
},

'info returned': function(err, info) {
'use strict';

assert.isNull(err);
assert.isObject(info);
assert.equal(info.id, '90AiXO1pAiA');
Expand All @@ -25,9 +26,9 @@ vows.describe('getInfo').addBatch({
'This is also the original I find it hilarious that there ' +
'are copycat videos!');
assert.equal(info._filename, 'lol-90AiXO1pAiA.mp4');
assert.equal(info.format, '18 - 640x360 (medium)');
assert.equal(info.format, '18 - 480x360 (medium)');
assert.equal(info.duration, '11');
assert.equal(info.width, 640);
assert.equal(info.width, 480);
assert.equal(info.height, 360);
assert.isArray(info.formats);
}
Expand Down Expand Up @@ -121,7 +122,7 @@ vows.describe('getInfo').addBatch({
assert.equal(info[0].format, 'http-360p - 480x272');
assert.equal(info[0].duration, '3:55');
assert.equal(info[1].id, '90AiXO1pAiA');
assert.equal(info[1].format_id, '18');
assert.equal(info[1].format_id, '43');
assert.equal(info[1].title, 'lol');
assert.isString(info[1].url);
assert.isString(info[1].thumbnail);
Expand All @@ -130,8 +131,8 @@ vows.describe('getInfo').addBatch({
'Filmed in 2003 before Youtube was invented. ' +
'This is also the original I find it hilarious that there ' +
'are copycat videos!');
assert.equal(info[1]._filename, 'lol-90AiXO1pAiA.mp4');
assert.equal(info[1].format, '18 - 640x360 (medium)');
assert.equal(info[1]._filename, 'lol-90AiXO1pAiA.webm');
assert.equal(info[1].format, '43 - 640x360 (medium)');
assert.equal(info[1].duration, '11');
assert.equal(info[1].width, 640);
assert.equal(info[1].height, 360);
Expand Down

0 comments on commit 7ef58ef

Please sign in to comment.