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

Commit

Permalink
fix reading download state
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Oct 15, 2013
1 parent be74d80 commit 174743a
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fs.exists(file, function(exists) {
});
});

var progressRegex = /(\d+\.\d)% of (\d+\.\d+\w+) at\s+([^\s]+) ETA ((\d|-)+:(\d|-)+)/;
var progressRegex = /(\d+(?:\.\d)?)% of (\d+\.\d+\w+) at\s+([^\s]+) ETA ((\d|-)+:(\d|-)+)/;


/**
Expand Down Expand Up @@ -60,29 +60,31 @@ exports.download = function(urladdr, dest, args) {
youtubedl.stdout.setEncoding('utf8');
youtubedl.stdout.pipe(line);
line.on('data', function(data) {
var pos, result;
var pos;

// Check if video is uploading so script can start
// calling the download progress function.
if (state === 'download' && (result = progressRegex.exec(data))) {

// If this is the first progress display, grab file size.
if (!size) {
emitter.emit(state, {
filename : filename
, size : size = result[2]
if (state === 'download') {
var result;
if (result = progressRegex.exec(data)) {
// If this is the first progress display, grab file size.
if (!size) {
emitter.emit(state, {
filename : filename
, size : size = result[2]
});
}

if (result[3] !== '---b/s') {
speed.push(util.toBytes(result[3].substring(0, result[3].length - 2)));
}
emitter.emit('progress', {
percent : result[1]
, speed : result[3]
, eta : result[4]
});
}

if (result[3] !== '---b/s') {
speed.push(util.toBytes(result[3].substring(0, result[3].length - 2)));
}
emitter.emit('progress', {
percent : result[1]
, speed : result[3]
, eta : result[4]
});

// About to start downloading video.
} else if ((pos = data.indexOf('[download] ')) === 0) {
state = 'download';
Expand Down

0 comments on commit 174743a

Please sign in to comment.