Created
February 11, 2019 08:13
-
-
Save palikhov/3bc00421d2a14172db7f7c0bbdfe3bfe to your computer and use it in GitHub Desktop.
Fanburst MP3 Download: Downloads all Fanburst tracks in your Jukebox as a single .ZIP (hopefully). Requires B20 to be running.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const pAjaxLoad = url => { | |
const oReq = new XMLHttpRequest(); | |
return new Promise((resolve, reject) => { | |
oReq.open("GET", url, true); | |
oReq.responseType = "arraybuffer"; | |
oReq.onload = () => resolve({buff: oReq.response}); | |
oReq.onerror = (e) => reject(new Error(`Error during request: ${e}`)); | |
oReq.send(); | |
}); | |
}; | |
const toSave = []; | |
const getWrappedPromise = async track => { | |
try { | |
toSave.push({title: track.title, data: await pAjaxLoad(track.url)}); | |
d20plus.ut.log(`Downloaded "${track.title}" (${track.url})`); | |
} catch (e) { | |
d20plus.ut.error(`Error downloading "${track.title}" (${track.url}): ${e.message}\nRetrying with CORS-Anywhere...`); | |
try { | |
toSave.push({title: track.title, data: await pAjaxLoad(`https://cors-anywhere.herokuapp.com/${track.url}`)}); | |
d20plus.ut.log(`Downloaded "${track.title}" (${track.url}) using CORS-Anywhere`); | |
} catch (e) { | |
d20plus.ut.error(`Error downloading "${track.title}" (${track.url}): ${e.message}.`); | |
} | |
} | |
}; | |
await Promise.all(Jukebox.playlist.filter(it => it.attributes.source === "Fanburst").map(it => ({ | |
title: it.attributes.title, | |
url: `https://api.fanburst.com/tracks/${it.attributes.track_id}/stream?client_id=0fc1df8b-40a4-4391-b0f7-d0257edb6634` | |
})).map(it => getWrappedPromise(it))); | |
const zip = new JSZip(); | |
toSave.forEach((track, i) => zip.file(`${`${i}`.padStart(3, "0")}-${track.title}.mp3`, track.data.buff, {binary: true})); | |
zip.generateAsync({type: "blob"}).then((content) => d20plus.ut.saveAs(content, `fanburst.zip`)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment