@nasheedstation/soundcloud-downloader
Version:
Download Soundcloud audio with Node.js, this is a forked version, the original script was written by @zackradisic
35 lines (29 loc) • 1.13 kB
text/typescript
/** @internal @packageDocumentation */
import { AxiosInstance } from 'axios'
import m3u8stream from 'm3u8stream'
import { handleRequestErrs, appendURL } from './util'
const fromURL = async (url: string, clientID: string, axiosInstance: AxiosInstance): Promise<NodeJS.ReadableStream | m3u8stream.Stream> => {
try {
const link = appendURL(url, 'client_id', clientID)
const res = await axiosInstance.get(link, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
Accept: '*/*',
'Accept-Encoding': 'gzip, deflate, br'
},
withCredentials: true
})
if (!res.data.url) throw new Error(`Invalid response from Soundcloud. Check if the URL provided is correct: ${link}`)
if (url.includes('/progressive')) {
const r = await axiosInstance.get(res.data.url, {
withCredentials: true,
responseType: 'stream'
})
return r.data
}
return m3u8stream(res.data.url)
} catch (err) {
throw handleRequestErrs(err)
}
}
export default fromURL