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

Commit

Permalink
Merge pull request #319 from Corollarium/overwriteDownload
Browse files Browse the repository at this point in the history
Download fixes
  • Loading branch information
Kikobeats authored Aug 18, 2020
2 parents 9793194 + d6133fc commit 63c0ab6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ downloader('path/to-binary', function error(err, done) {
})
```
This script parses a couple of flags from `argv`:
* `--platform=windows` forces downloading the Windows version of youtube-dl.
* `--overwrite` overwrites the existing youtube-dl executable if it exists.
### Update (promise version)
If you are using promises there's now a promise version, if you don't pass a function as second argument:
``` js
const downloader = require('youtube-dl/lib/downloader')

downloader('path/to-binary')
.then((message) => {
console.log(message);
}).catch((err) => {
console.log("err", err);
exit(1);
});

```
### Environment Variables
Youtube-dl looks for certain environment variables to aid its operations. If Youtube-dl doesn't find them in the environment during the installation step, a lowercased variant of these variables will be used from the [npm config](https://docs.npmjs.com/cli/config) or [yarn config](https://yarnpkg.com/lang/en/docs/cli/config/).
Expand Down
39 changes: 32 additions & 7 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const request = require('request')
const mkdirp = require('mkdirp')
const path = require('path')
const fs = require('fs')
const util = require('util')

const [, , ...flags] = process.argv

const isWin = flags.includes('--platform=windows') || require('./util').isWin
const isOverwrite = flags.includes('--overwrite')

// First, look for the download link.
let dir, filePath
Expand Down Expand Up @@ -44,25 +46,30 @@ function download (url, callback) {
status = new Error('Response Error: ' + res.statusCode)
return
}
downloadFile.pipe(fs.createWriteStream(filePath, { mode: 493 }))

const outputStream = fs.createWriteStream(filePath, { mode: 493 });
outputStream.on(
'close',
function end() {
callback(status, newVersion);
});
downloadFile.pipe(outputStream);
})

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)
if (!fs.existsSync(dir)) {
mkdirp.sync(dir)
if (binDir) mkdirp.sync(defaultBin)
}
filePath = path.join(dir, exec('youtube-dl'))
}

Expand All @@ -71,9 +78,27 @@ function downloader (binDir, callback) {
callback = binDir
binDir = null
}
else if (!callback) {
return util.promisify(downloader)(binDir)
}

createBase(binDir)

// handle overwritin
if (fs.existsSync(filePath)) {
if (!isOverwrite) {
return callback("File exists");
}
else {
try {
fs.unlinkSync(filePath);
}
catch (e) {
callback(e);
}
}
}

download(url, function error (err, newVersion) {
if (err) return callback(err)
fs.writeFileSync(
Expand Down
9 changes: 5 additions & 4 deletions scripts/download.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var downloader = require('../lib/downloader')

downloader(function error (err, done) {
if (err) return console.log(err.stack)
console.log(done)
})
downloader().then((message) => {
console.log(message);
}).catch((err) => {
console.error(err)
});

0 comments on commit 63c0ab6

Please sign in to comment.