update 30
This commit is contained in:
@@ -14,22 +14,27 @@ catch (e) {
|
||||
const isWindows = process.platform === 'win32'
|
||||
|
||||
const download = (socket, url) => {
|
||||
if (url === '' || url === null) {
|
||||
socket.emit('progress', { status: 'Done!' })
|
||||
return
|
||||
}
|
||||
|
||||
getDownloadInfo(socket, url)
|
||||
|
||||
const ytldp = spawn(`./lib/yt-dlp${isWindows ? '.exe' : ''}`,
|
||||
['-o', `${settings.download_path || 'downloads/'}%(title)s.%(ext)s`, url]
|
||||
)
|
||||
|
||||
from(ytldp.stdout) // stout as observable
|
||||
from(ytldp.stdout) // stdout as observable
|
||||
.pipe(throttle(() => interval(500))) // discard events closer than 500ms
|
||||
.subscribe({
|
||||
next: (stdout) => {
|
||||
let _stdout = String(stdout)
|
||||
socket.emit('progress', formatter(_stdout)) // finally, emit
|
||||
//let _stdout = String(stdout)
|
||||
socket.emit('progress', formatter(String(stdout))) // finally, emit
|
||||
//logger('download', `Fetching ${stdout}`)
|
||||
console.log(formatter(_stdout))
|
||||
},
|
||||
complete: () => {
|
||||
socket.emit('progress', { status: 'Done!' })
|
||||
logger('download', 'Done!')
|
||||
}
|
||||
})
|
||||
|
||||
@@ -39,15 +44,29 @@ const download = (socket, url) => {
|
||||
})
|
||||
}
|
||||
|
||||
const getDownloadInfo = (socket, url) => {
|
||||
let stdoutChunks = [];
|
||||
const ytdlpInfo = spawn(`./lib/yt-dlp${isWindows ? '.exe' : ''}`, ['-s', '-j', url]);
|
||||
|
||||
ytdlpInfo.stdout.on('data', (data) => {
|
||||
stdoutChunks.push(data)
|
||||
})
|
||||
|
||||
ytdlpInfo.on('exit', () => {
|
||||
const buffer = Buffer.concat(stdoutChunks)
|
||||
const json = JSON.parse(buffer.toString())
|
||||
socket.emit('info', json)
|
||||
})
|
||||
}
|
||||
|
||||
const abortDownload = (socket) => {
|
||||
const res = process.platform === 'win32' ?
|
||||
spawn('taskkill', ['/IM', 'yt-dlp.exe', '/F', '/T']) :
|
||||
spawn('killall', ['yt-dlp'])
|
||||
res.stdout.on('data', data => {
|
||||
res.on('exit', () => {
|
||||
socket.emit('progress', 'Aborted!')
|
||||
logger('download', `Aborting ${data.toString()}`)
|
||||
logger('download', 'Aborting downloads')
|
||||
})
|
||||
logger('download', 'Aborted')
|
||||
}
|
||||
|
||||
const formatter = (stdout) => {
|
||||
@@ -71,10 +90,9 @@ const formatter = (stdout) => {
|
||||
default:
|
||||
return { progress: '0' }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
download: download,
|
||||
abortDownload: abortDownload
|
||||
}
|
||||
abortDownload: abortDownload,
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
const Readable = require('stream').Readable;
|
||||
|
||||
class Subscription extends Readable{
|
||||
constructor(options) {
|
||||
super();
|
||||
if (!(this instanceof Subscription))
|
||||
return new Subscription(options);
|
||||
|
||||
options = options || {};
|
||||
Readable.call(this, options);
|
||||
|
||||
this.value = 0;
|
||||
}
|
||||
_read() {
|
||||
while(this.value <= 100){
|
||||
this.push(String(this.value++));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exports.subscribe = function(event, options){
|
||||
return new Subscription(options);
|
||||
}
|
||||
Reference in New Issue
Block a user