From 1dbd19c6b2635178ecaa8d377e1c1957a92dc54a Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 25 Nov 2021 22:33:17 +0100 Subject: [PATCH] more object-orientation --- radiomanifest.js | 142 +++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 74 deletions(-) diff --git a/radiomanifest.js b/radiomanifest.js index 2135998..ebdbf38 100644 --- a/radiomanifest.js +++ b/radiomanifest.js @@ -1,85 +1,79 @@ const fetch = require('isomorphic-unfetch') -class RadioManifest { - constructor (baseURL, options) { - this.baseURL = baseURL - this.options = options - - const radiomanifest = fetch(`${baseURL}/radiomanifest.xml`) - return radiomanifest - } - - getShowByName (showName) { - - } - - getStreaming () { - - } - - getSchedule () { - - } - - getShowAtTime () { - - } -} - function getStreaminfoUrl (siteurl) { return siteurl + '/streaminfo.json' // XXX: improve this logic } function getManifestUrl (siteurl) { return siteurl + '/radiomanifest.xml' // XXX: improve this logic } -function parseRadioManifest (xml) { - let res = xml.evaluate('/radio-manifest/streaming/source', xml) - const sources = [] - while (true) { - const src = res.iterateNext() - if (src === null) break - if (!src.hasAttribute('priority')) { - src.setAttribute('priority', '0') - } else if (parseInt(src.getAttribute('priority'), 10) < 0) { - continue + +class Radio { + constructor (sources, schedule, shows, feed) { + this.streaming = new RadioStreaming(sources) + this.schedule = schedule + this.shows = shows + this.feed = feed + this.name = '' + } + + getStreaming () { + return this.streaming + } + + setName (name) { + this.name = name + } + + getShowByName (showName) { + } + + getSchedule () { + } + + getShowAtTime () { + } + + static fromDOM (xml) { + let res = xml.evaluate('/radio-manifest/streaming/source', xml) + const sources = [] + while (true) { + const src = res.iterateNext() + if (src === null) break + if (!src.hasAttribute('priority')) { + src.setAttribute('priority', '0') + } else if (parseInt(src.getAttribute('priority'), 10) < 0) { + continue + } + sources.push(src) } - sources.push(src) + sources.sort(function cmp (a, b) { + return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10)) + }) + + res = xml.evaluate('/radio-manifest/schedule', xml) + const scheduleEl = res.iterateNext() + let schedule = null + if (scheduleEl !== null) { + schedule = scheduleEl.getAttribute('src') + } + + res = xml.evaluate('/radio-manifest/shows', xml) + const showsEl = res.iterateNext() + let shows = null + if (showsEl !== null) { + shows = showsEl.getAttribute('src') + } + + res = xml.evaluate('/radio-manifest/feed', xml) + const feedEl = res.iterateNext() + let feed = null + if (feedEl !== null) { + feed = feedEl.getAttribute('src') + } + + const manifest = new Radio(sources, schedule, shows, feed) + return manifest } - sources.sort(function cmp (a, b) { - return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10)) - }) - - res = xml.evaluate('/radio-manifest/schedule', xml) - const scheduleEl = res.iterateNext() - let schedule = null - if (scheduleEl !== null) { - schedule = scheduleEl.getAttribute('url') - } - - res = xml.evaluate('/radio-manifest/shows', xml) - const showsEl = res.iterateNext() - let shows = null - if (showsEl !== null) { - shows = showsEl.getAttribute('src') - } - - const manifest = new Radio(sources, schedule, shows) - return manifest -} - -function Radio (sources, schedule, shows) { - this.streaming = new RadioStreaming(sources) - this.schedule = schedule - this.shows = shows - this.name = '' -} - -Radio.prototype.getStreaming = function () { - return this.streaming -} - -Radio.prototype.setName = function (name) { - this.name = name } function RadioStreaming (sources) { @@ -108,7 +102,7 @@ async function get (siteurl, options) { const parser = new DOMParser() const dom = parser.parseFromString(text, 'text/xml') - const manifest = parseRadioManifest(dom) + const manifest = Radio.fromDOM(dom) resp = null try { @@ -154,6 +148,6 @@ module.exports = { }, parsers: { M3U: parseM3U, - radioManifest: parseRadioManifest + radioManifest: Radio.fromDOM } }