Skip to content

Instantly share code, notes, and snippets.

@palikhov
Created February 20, 2019 21:24
Show Gist options
  • Save palikhov/eacd74beade4c4c020d622e8ff6d911b to your computer and use it in GitHub Desktop.
Save palikhov/eacd74beade4c4c020d622e8ff6d911b to your computer and use it in GitHub Desktop.
Console script to download from jukebox fanburst rracks
(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