generic-webradio-player/player.js

254 lines
8.4 KiB
JavaScript
Raw Permalink Normal View History

2021-11-15 10:52:00 +01:00
let leRadio = [
{ "name": "Radio Spore", "stream": "https://stream.radiospore.oziosi.org:8003/spore.ogg" },
{ "name": "Radio Wombat", "stream": "https://s.streampunk.cc/wombat.ogg"},
{ "name": "RadioGramma", "stream": "https://stream.radiospore.oziosi.org:8003/radiogramma.ogg"},
{ "name": "RadioForte", "stream": "http://stream.s.streampunk.cc/player/RadioForte.ogg"},
{ "name": "RadioEustachio", "stream": "https://stream.radiospore.oziosi.org:8003/radioeustachio.ogg"}]
let playlist = []
let radio = document.querySelector("#radio");
leRadio = leRadio.map((r) => {
r.stream += "?" + Math.floor((Math.random() * 10000) + 1)
return r
});
radio.src = leRadio[0].stream
const playButton = document.querySelector("#play")
const nextButton = document.querySelector("#next")
const prevButton = document.querySelector("#prev")
const contents = document.querySelector("#contents")
const contentsButton = document.querySelector("#contents-button")
const volumeOn = document.querySelector("#volume-on")
const volumeOff = document.querySelector("#volume-off")
const showContentsEl = document.querySelector("#programmi-content")
const showListEl = document.querySelector("#programmi-lista")
let currentStatus
async function playPauseRadio() {
try {
if(radio.paused) {
await radio.play();
}
else {
await radio.pause();
}
}
catch(err) {
console.log(err);
}
}
function onVolumeChange(e) {
if(e.target.value == 0) {
volumeOn.classList.add("hidden");
volumeOff.classList.remove("hidden")
}
else {
volumeOff.classList.add("hidden");
volumeOn.classList.remove("hidden")
}
radio.volume = e.target.value;
}
function setupTabs() {
for(let el of document.querySelectorAll("#menu > input")){
console.log(el)
el.addEventListener('change', (e) => {
for(let ce of document.querySelectorAll("#menu > input")) {
if(ce.checked) {
document.querySelector("#"+ce.id.split('-').slice(1).join('-')).classList.remove("hidden")
}
else {
document.querySelector("#"+ce.id.split('-').slice(1).join('-')).classList.add("hidden")
}
}
})
}
}
function setupPlayer() {
playButton.addEventListener("click", playPauseRadio, false);
volumeOff.addEventListener("click", (e) => {
volume.disabled = false;
e.target.classList.add("hidden");
volumeOn.classList.remove("hidden");
radio.volume = volume.value;
});
volumeOn.addEventListener("click", (e) => {
volume.disabled = true;
e.target.classList.add("hidden");
volumeOff.classList.remove("hidden");
radio.volume = 0;
});
volume.addEventListener('change', onVolumeChange);
contentsButton.addEventListener("click", (e) => {
contents.hidden = !contents.hidden;
e.target.classList.toggle("arrow-up");
})
let showBackToLive = () => {
document.querySelector(".is-live").classList.add("hidden");
document.querySelector(".back-to-live").classList.remove("hidden");
}
prevButton.addEventListener("click",(e) => {
radio.currentTime -= 15;
showBackToLive()
}, false)
nextButton.addEventListener("click",(e) => {
radio.currentTime += 15;
showBackToLive()
}, false)
radio.addEventListener('pause', (e) => {
showBackToLive()
})
document.querySelector(".back-to-live").addEventListener('click', (e) => {
document.querySelector(".is-live").classList.remove("hidden")
e.target.classList.add("hidden")
radio.currentTime = radio.duration
radio.play()
})
}
function downloadPlaylist() {
let m3u = "#EXTM3U\n"
m3u = playlist.reduce((data, track) => {
data += `#EXTINF:${track.itunes.duration},${track.itunes.name} \n`
data += `${track.guid}\n`
return data
}, m3u)
console.log(m3u)
const blob = new Blob([m3u], { 'type': 'audio/x-mpegurl'});
const m3uFile = window.URL.createObjectURL(blob);
var a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "playlist.m3u";
a.click();
window.URL.revokeObjectURL(url);
}
function setupRadios() {
for(let r of leRadio) {
let el = document.createElement("span")
el.innerHTML = r.name;
el.addEventListener('click', (e) => {
radio.src = r.stream;
document.querySelector("#radio-name").textContent = r.name
radio.play()
})
document.querySelector("#other-radios").appendChild(el)
}
}
function setupShowButtons() {
let scrollFwd = (el) => {
let scrollAmount = 0;
let timer = setInterval( () => {
el.scrollLeft += 10;
scrollAmount += 10;
if(scrollAmount >= 100){
window.clearInterval(timer);
}
}, 25);
}
let scrollBack = (el) => {
let scrollAmount = 0;
let timer = setInterval( () => {
el.scrollLeft -= 10;
scrollAmount -= 10;
if(scrollAmount <= -100){
window.clearInterval(timer);
}
}, 25);
}
document.getElementById("prev-show").addEventListener("click", (e) => {
scrollBack(showListEl)
})
document.getElementById("next-show").addEventListener("click", (e) => {
scrollFwd(showListEl)
})
document.getElementById("next-show-card").addEventListener("click", (e) => {
scrollFwd(showContentsEl.querySelector("div.content-box:not(.hidden)"))
})
document.getElementById("prev-show-card").addEventListener("click", (e) => {
scrollBack(showContentsEl.querySelector("div.content-box:not(.hidden)"))
})
}
async function setupProgrammi() {
const res = await fetch("https://www.arkiwi.org/path64/cmFkaW9zcG9yZQ/shows");
const shows = Array.from(await new window.DOMParser().parseFromString(await res.text(), "text/xml").getElementsByTagName("show"));
programmi = shows.map(show => Array.from(show.childNodes).reduce((data, node) => {
if(node.localName) {
data[node.localName] = node.textContent // + ( node.localName == 'feed' ? '?limit=6' : '')
}
return data;
}, {}))
for(let p of programmi) {
let el = document.createElement("span")
el.innerHTML = p.name;
el.addEventListener('click', (e) => {
const id = p.name.replaceAll(/[\W_]+/g," ").replaceAll(' ','-');
for(let box of document.querySelectorAll("#programmi-content > .content-box")) {
2021-11-15 10:52:00 +01:00
if(box.id != id) {
box.classList.add("hidden");
}
}
document.getElementById(id).classList.toggle("hidden");
document.querySelectorAll("#programmi > .show-content > .arrow").forEach((el) => {
el.classList.remove("hidden")
})
2021-11-15 10:52:00 +01:00
})
showListEl.appendChild(el)
fetch(p.feed)
.then(res => res.text())
.then(res => new window.DOMParser().parseFromString(res, "text/xml"))
.then(xml => {
let data = Array.from(xml.getElementsByTagName("item")).
map(item => Array.from(item.childNodes)
.reduce((data, node) => {
if(node.localName) {
if(node.prefix){
if(!data[node.prefix]) {
data[node.prefix] = {}
}
data[node.prefix][node.localName] = node.textContent
}
else {
data[node.localName] = node.textContent
// if(localName == "pubDate") {
// data[node.localName] = new Date(data[node.localName])
// }
}}
return data;
}, {}))
let programma = document.createElement("div")
programma.setAttribute("id", p.name.replaceAll(/[\W_]+/g," ").replaceAll(' ', '-'));
programma.classList.add("content-box");
programma.classList.add("hidden");
document.getElementById("programmi-content").append(programma);
for(let s of data) {
let template = document.querySelector("#show-card-template");
let puntata = template.content.cloneNode(true);
let keywords = ''
if(s['itunes'] && s.itunes.keywords) {
s['itunes']['keywords'].split(',').map((k) => `<span>${k}</span>`).join('')
}
puntata.querySelector(".title").textContent = s.title;
puntata.querySelector(".description").textContent = s.description;
//puntata.innerHTML = `<span><b>${data[a].title}</b></span> <div class="small-tags">${keywordso}</div> <br/> <br/><div>${data[a].description}</div>`;
puntata.querySelector(".play-pause").addEventListener('click', (e) => {
radio.src = s.guid;
playPauseRadio();
document.querySelector("#radio-name").textContent = p.name;
document.querySelector("#program-name").textContent = s.title;
})
puntata.querySelector(".add-to-playlist").addEventListener('click', (e) => {
playlist.push(s)
})
programma.appendChild(puntata)
}
})
}
}
setupPlayer();
setupRadios();
setupTabs();
setupShowButtons();
setupProgrammi();