From 935fc70e992ea8b616c721ee40635f01587484f0 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 26 Dec 2021 20:00:17 +0100 Subject: [PATCH] pickURLs added --- radiomanifest.js | 69 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/radiomanifest.js b/radiomanifest.js index 09351da..01e4533 100644 --- a/radiomanifest.js +++ b/radiomanifest.js @@ -7,6 +7,12 @@ function getManifestUrl (siteurl) { return siteurl + '/radiomanifest.xml' // XXX: improve this logic } +function getAttribute(el, attr, default_value) { + if(el.hasAttribute(attr)) + return el.getAttribute(attr); + return default_value; +} + class Radio { constructor (sources, schedule, shows, feed) { this.streaming = new RadioStreaming(sources) @@ -47,9 +53,6 @@ class Radio { } sources.push(src) } - sources.sort(function cmp (a, b) { - return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10)) - }) res = doc.evaluate('/radio-manifest/schedule', doc) const scheduleEl = res.iterateNext() @@ -77,24 +80,50 @@ class Radio { } } -function RadioStreaming (sources) { - this.sources = sources -} - -RadioStreaming.prototype.getOptions = function () { - return this.sources.map(function (x) { - return x.getAttribute('name') - }) -} -RadioStreaming.prototype.getSource = function (name) { - if (name === undefined) { - +class RadioStreaming { + constructor (sources) { + this.sources = sources.sort( + (a,b) => this.getPriority(a) < this.getPriority(a) + ) + } + + getOptions() { + return this.sources.map(function (x) { + return x.getAttribute('name') + }) + } + // this is private + getPriority(element) { + return parseInt(getAttribute(element, 'priority', '1')) + } + // this is private + getTopPrioritySources() { + var topPriority = this.getPriority(this.sources[0]) + return this.sources.filter( + (src) => parseInt(src.getAttribute('priority'), 10) === topPriority + ) + } + getSource(name) { + if (name === undefined) { + return this.getTopPrioritySources()[0] + } + const s = this.sources.find(function (x) { + return x.getAttribute('name') === name + }) + if (s === undefined) return s + return s.getAttribute('src') + } + + async pickURLs() { + var allSources = this.getTopPrioritySources() + var allAudios = [] + for(let src of allSources) { + let url = src.getAttribute('src') + let resp = await fetch(url) + allAudios.unshift(... parseM3U(await resp.text())) + } + return allAudios } - const s = this.sources.find(function (x) { - return x.getAttribute('name') === name - }) - if (s === undefined) return s - return s.getAttribute('src') } async function get (siteurl, options) {