Implement individual song download

This commit is contained in:
Jorge Vargas 2025-03-10 19:35:55 -06:00
parent 108b37dfe3
commit bab6302247
4 changed files with 65 additions and 6 deletions

View file

@ -75,3 +75,31 @@ export async function getTracks(url: string, lastReqRef: React.RefObject<number>
return tracks
}
export async function getDownloadUrl(trackPageUrl: string, lastReqRef: React.RefObject<number>) {
const trackPageHtml = await (await politeFetch(trackPageUrl, lastReqRef)).text()
const $ = cheerio.load(trackPageHtml)
const audioLinks: string[] = []
$('a[href$=".mp3"], a[href$=".flac"]').each((index, link) => {
let href = $(link).attr('href')
if (!href) return
href = decodeURIComponent(href)
if (!href.startsWith('http')) {
href = `https://downloads.khinsider.com${href}`
}
audioLinks.push(href)
})
const flacLinks = audioLinks.filter((link) => link.endsWith('.flac'))
if (flacLinks.length > 0) {
return flacLinks[0]
}
const mp3Links = audioLinks.filter((link) => link.endsWith('.mp3'))
if (mp3Links.length > 0) {
return mp3Links[0]
}
}