Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Fix path parameters and add progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed Aug 5, 2018
1 parent a122eae commit febdf7a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 71 deletions.
48 changes: 38 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
#! /usr/bin/env node

const signale = require('signale');
const { join } = require('path');
const { join, parse } = require('path');
const lineByLine = require('linebyline');
const ytdl = require('ytdl-core');
const { existsSync, createWriteStream } = require('fs');
const inquirer = require('inquirer');
inquirer.registerPrompt('fuzzypath', require('inquirer-fuzzy-path'));
const fs = require('fs');
const ProgressBar = require('progress');

signale.start('music-downloader started');

// Path to put default search area for file selection
let userHomePath;
if (process.env.HOME) {
userHomePath = process.env.HOME;
} else if (process.env.HOMEPATH) {
userHomePath = process.env.HOMEPATH;
} else {
userHomePath = __dirname;
}

inquirer
.prompt([{
type: 'fuzzypath',
name: 'urlList',
'type': 'input',
'name': 'urlList',
// Only allow non-directories and text files
pathFilter: (isDirectory, nodePath) => !isDirectory && nodePath.endsWith('.txt'),
message: 'Select a text file to load videos from (1 video per line):'
'validate': input => fs.lstatSync(input).isFile() && parse(input).ext === '.txt',
'message': 'Select a text file to load videos from (1 video per line):',
'default': join(userHomePath, 'videos.txt')
},
{
type: 'fuzzypath',
name: 'resultDirectory',
'type': 'input',
'name': 'resultDirectory',
// Only allow directories
pathFilter: isDirectory => isDirectory,
message: 'Select a directory to store downloaded audio to:'
'validate': input => fs.lstatSync(input).isDirectory(),
'message': 'Select a directory to store downloaded audio to:',
'default': join(userHomePath, 'Music')
}])
.catch(err => signale.error(err))
.then(params => {
Expand Down Expand Up @@ -72,6 +85,8 @@ inquirer
filter: 'audioonly'
});

let bar;

// Set up event handlers before calling the pipe function
stream
.on('error', err => {
Expand All @@ -84,6 +99,19 @@ inquirer
message: 'Downloading...'
});
})
// This event triggers whenever a chunk of data is received
.on('response', serverResponse => {
bar = new ProgressBar(`[${displayName}] [:bar] :etas remaining`, {
complete: '=',
incomplete: ' ',
width: 20,
total: parseInt(serverResponse.headers['content-length'], 10),
clear: true
});
})
.on('progress', chunkLength => {
bar.tick(chunkLength);
})
// This event is triggered when the stream has finished being flushed to the file system
.on('finish', () => {
videoLogger.success({
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
},
"dependencies": {
"inquirer": "6.0.0",
"inquirer-fuzzy-path": "1.0.0",
"linebyline": "1.3.0",
"progress": "^2.0.0",
"signale": "1.2.1",
"ytdl-core": "0.23.0"
},
Expand All @@ -27,5 +27,5 @@
"lint": "eslint .",
"start": "node index.js"
},
"version": "2.1.3"
"version": "3.0.0"
}
61 changes: 2 additions & 59 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ ajv@^6.0.1, ajv@^6.5.0:
json-schema-traverse "^0.4.1"
uri-js "^4.2.1"

ansi-escapes@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-2.0.0.tgz#5bae52be424878dd9783e8910e3fc2922e83c81b"

ansi-escapes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30"
Expand Down Expand Up @@ -321,7 +317,7 @@ esutils@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"

external-editor@^2.0.4, external-editor@^2.1.0:
external-editor@^2.1.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-2.2.0.tgz#045511cfd8d133f3846673d1047c154e214ad3d5"
dependencies:
Expand Down Expand Up @@ -393,10 +389,6 @@ functional-red-black-tree@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"

fuzzy@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/fuzzy/-/fuzzy-0.1.3.tgz#4c76ec2ff0ac1a36a9dccf9a00df8623078d4ed8"

glob@^7.0.3, glob@^7.0.5, glob@^7.1.2:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
Expand Down Expand Up @@ -476,45 +468,6 @@ inherits@2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

inquirer-autocomplete-prompt@^0.12.2:
version "0.12.2"
resolved "https://registry.yarnpkg.com/inquirer-autocomplete-prompt/-/inquirer-autocomplete-prompt-0.12.2.tgz#693bbd9da586f98b126bd83305bb8d8714dc4155"
dependencies:
ansi-escapes "^3.0.0"
chalk "^2.0.0"
figures "^2.0.0"
inquirer "3.2.0"
run-async "^2.3.0"

inquirer-fuzzy-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/inquirer-fuzzy-path/-/inquirer-fuzzy-path-1.0.0.tgz#b6cffa5022fa667b84f1b0a74d2d6c8ee2549309"
dependencies:
ansi-styles "^3.2.1"
fuzzy "^0.1.3"
inquirer "^5.2.0"
inquirer-autocomplete-prompt "^0.12.2"
strip-ansi "^4.0.0"

inquirer@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-3.2.0.tgz#45b44c2160c729d7578c54060b3eed94487bb42b"
dependencies:
ansi-escapes "^2.0.0"
chalk "^2.0.0"
cli-cursor "^2.1.0"
cli-width "^2.0.0"
external-editor "^2.0.4"
figures "^2.0.0"
lodash "^4.3.0"
mute-stream "0.0.7"
run-async "^2.2.0"
rx-lite "^4.0.8"
rx-lite-aggregates "^4.0.8"
string-width "^2.1.0"
strip-ansi "^4.0.0"
through "^2.3.6"

inquirer@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-6.0.0.tgz#e8c20303ddc15bbfc2c12a6213710ccd9e1413d8"
Expand Down Expand Up @@ -854,22 +807,12 @@ rimraf@^2.2.8:
dependencies:
glob "^7.0.5"

run-async@^2.2.0, run-async@^2.3.0:
run-async@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"
dependencies:
is-promise "^2.1.0"

rx-lite-aggregates@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite-aggregates/-/rx-lite-aggregates-4.0.8.tgz#753b87a89a11c95467c4ac1626c4efc4e05c67be"
dependencies:
rx-lite "*"

rx-lite@*, rx-lite@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"

rxjs@^5.5.2:
version "5.5.11"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.11.tgz#f733027ca43e3bec6b994473be4ab98ad43ced87"
Expand Down

0 comments on commit febdf7a

Please sign in to comment.