diff --git a/lib/downloader.js b/lib/downloader.js index c03cdf2..9b00233 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -1,5 +1,7 @@ const { spawn } = require('child_process'); const logger = require('./logger'); +const { from, interval } = require('rxjs'); +const { throttle } = require('rxjs/operators'); let settings; try { @@ -16,8 +18,15 @@ const download = (socket, url) => { ['-o', `${settings.download_path || 'downloads/'}%(title)s.%(ext)s`, url] ) ytldp.stdout.on('data', data => { - socket.emit('progress', data.toString()) - logger('download', `Fetching ${data.toString()}`) + // reactive programming magic + from(Promise.resolve(data)) // stout as promise => Observable + .pipe(throttle(() => interval(500))) // discard events closer than 500ms + .subscribe({ + next: () => { + socket.emit('progress', data.toString()) // finally, emit + logger('download', `Fetching ${data.toString()}`) + } + }) }) ytldp.on('exit', () => { socket.emit('progress', 'Done!') diff --git a/lib/fetch-yt-dlp.js b/lib/fetch-yt-dlp.js deleted file mode 100644 index e893f4b..0000000 --- a/lib/fetch-yt-dlp.js +++ /dev/null @@ -1,16 +0,0 @@ -const https = require('https'); -const path = require('path'); -const fs = require('fs'); - -const isWindows = process.platform === 'win32'; - -const file = fs.createWriteStream(path.join( - __dirname, `yt-dlp${isWindows ? '.exe' : ''}` -)); - -https.get( - isWindows ? - 'https://github.com/yt-dlp/yt-dlp/releases/download/2021.11.10.1/yt-dlp.exe' : - 'https://github.com/yt-dlp/yt-dlp/releases/download/2021.11.10.1/yt-dlp', - res => res.pipe(file) -); \ No newline at end of file diff --git a/package.json b/package.json index d06993b..57edcf4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "react": "^17.0.2", "react-bootstrap": "^2.0.2", "react-dom": "^17.0.2", + "rxjs": "^7.4.0", "socket.io": "^4.3.2", "socket.io-client": "^4.3.2" },