44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
const {LogType} = require('loguix')
|
|
const clog = new LogType("Soundcloud-Stream")
|
|
const {Soundcloud} = require('soundcloud.ts')
|
|
const { isAllowedUrl } = require('../SongCheck')
|
|
|
|
const soundcloud = new Soundcloud();
|
|
|
|
// Le flux SoundCloud est lu depuis le début : le seek n'est pas supporté (le player le refuse)
|
|
const seekable = false
|
|
|
|
/**
|
|
* @param {Object} song
|
|
* @param {number} seekSec - ignoré (seek non supporté)
|
|
* @param {{signal?: AbortSignal}} options
|
|
* @returns {Promise<import('stream').Readable|null>} le flux, ou null en cas d'échec / d'annulation
|
|
*/
|
|
async function getStream(song, seekSec = 0, { signal } = {}) {
|
|
if(signal?.aborted) return null
|
|
if(!song || !isAllowedUrl("soundcloud", song.url)) {
|
|
clog.error("URL SoundCloud refusée : " + JSON.stringify(song?.url))
|
|
return null
|
|
}
|
|
try {
|
|
const stream = await soundcloud.util.streamTrack(song.url)
|
|
if(!stream) {
|
|
clog.error("Aucun flux renvoyé par SoundCloud pour : " + song.title)
|
|
return null
|
|
}
|
|
// Chargement remplacé entre-temps (skip, leave...) : on libère le flux
|
|
if(signal?.aborted) {
|
|
stream.destroy?.()
|
|
return null
|
|
}
|
|
signal?.addEventListener('abort', () => stream.destroy?.(), { once: true })
|
|
return stream
|
|
} catch(e) {
|
|
clog.error("Erreur lors de la récupération du stream : " + song.title)
|
|
clog.error(e)
|
|
return null
|
|
}
|
|
}
|
|
|
|
module.exports = {getStream, seekable}
|