39 lines
972 B
JavaScript
39 lines
972 B
JavaScript
import Soundcloud from 'lucida/streamers/soundcloud/main.js'
|
|
let clientId = process.env.SOUNDCLOUD_CLIENT_ID;
|
|
|
|
let sc = new Soundcloud({
|
|
// oauthToken: clientId
|
|
})
|
|
|
|
async function search(query) {
|
|
let data = sc.search(query);
|
|
return data;
|
|
}
|
|
async function download(url, res) {
|
|
if (!url) {
|
|
return res.status(400).send('Please provide a SoundCloud track URL as ?url=')
|
|
}
|
|
|
|
try {
|
|
const info = await sc.getByUrl(url)
|
|
|
|
if (info.type !== 'track') {
|
|
return res.status(400).send('URL is not a SoundCloud track')
|
|
}
|
|
|
|
const { stream, mimeType, sizeBytes } = await info.getStream()
|
|
|
|
res.setHeader('Content-Type', mimeType)
|
|
if (sizeBytes) {
|
|
res.setHeader('Content-Length', sizeBytes.toString())
|
|
}
|
|
res.setHeader('Cache-Control', 'no-cache')
|
|
|
|
stream.pipe(res)
|
|
} catch (err) {
|
|
console.error('Stream error:', err)
|
|
res.status(500).send(err.message || 'Failed to stream track')
|
|
}
|
|
}
|
|
|
|
export { search, download }; |