Skip to content

Instantly share code, notes, and snippets.

@Pho3nixHun
Created October 3, 2020 11:46
Show Gist options
  • Save Pho3nixHun/b278466a9a269aea49c366f442b4db90 to your computer and use it in GitHub Desktop.
Save Pho3nixHun/b278466a9a269aea49c366f442b4db90 to your computer and use it in GitHub Desktop.
const require = async (url) => {
const scriptAlreadyRequired = document.querySelector(`script[data-origin="${url}"`);
if (scriptAlreadyRequired) {
return scriptAlreadyRequired;
}
const script = document.createElement('script');
const code = await fetch(url);
script.type = 'text/javascript';
script.setAttribute('data-origin', url);
script.innerText = await code.text();
document.head.appendChild(script);
return script;
}
const main = async (filename) => {
await require("https://cdn.jsdelivr.net/npm/web-streams-polyfill@2.0.2/dist/ponyfill.min.js");
await require("https://cdn.jsdelivr.net/npm/streamsaver@2.0.3/StreamSaver.min.js");
const audio = await fetch("https://www.radio1.hu/wimplayer?access_token=4%01%09%07%22%19%13XWTYTJS%5CV%5BS%5D", {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,hu-HU;q=0.8,hu;q=0.7",
"range": "bytes=131072-",
"sec-fetch-dest": "video",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-origin"
},
"referrer": "https://www.radio1.hu/tracklista/tommyboy-es-a-housematic/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
});
const fileStream = streamSaver.createWriteStream(filename)
const writer = fileStream.getWriter()
// stream the response
const reader = audio.body.getReader()
const pump = async () => {
const { value, done } = await reader.read();
writer.write(value);
return writer.ready.then(pump)
}
// Start the reader
pump().then(() =>
console.log('Closed the stream, Done writing')
)
}
main('stream.mp3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment