update 30

This commit is contained in:
2021-11-30 13:07:22 +01:00
parent 8f204f7cbd
commit 29d23144e7
11 changed files with 124 additions and 62 deletions

View File

@@ -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,
}