diff --git a/karma.config.js b/karma.config.js index 31246d2..53da838 100644 --- a/karma.config.js +++ b/karma.config.js @@ -1,34 +1,34 @@ module.exports = function (config) { - config.set({ - frameworks: ['mocha'], - // plugins: ['karma-webpack', 'karma-mocha', 'karma-chai-as-promised'], - webpack: { - // karma watches the test entry points - // Do NOT specify the entry option - // webpack watches dependencies - - // webpack configuration - }, - preprocessors: { - 'test/**/*.js': ['webpack'], - 'radiomanifest.js': ['webpack'] - }, - files: [ - 'radiomanifest.js', - 'test/**/*.js' - ], - reporters: ['progress'], - port: 9876, // karma web server port - colors: true, - logLevel: config.LOG_INFO, - browsers: ['ChromeHeadless', 'FirefoxHeadless'], - autoWatch: false, - concurrency: Infinity, - customLaunchers: { - FirefoxHeadless: { - base: 'Firefox', - flags: ['-headless'], - }, - }, - }) + config.set({ + frameworks: ['mocha'], + // plugins: ['karma-webpack', 'karma-mocha', 'karma-chai-as-promised'], + webpack: { + // karma watches the test entry points + // Do NOT specify the entry option + // webpack watches dependencies + + // webpack configuration + }, + preprocessors: { + 'test/**/*.js': ['webpack'], + 'radiomanifest.js': ['webpack'] + }, + files: [ + 'radiomanifest.js', + 'test/**/*.js' + ], + reporters: ['progress'], + port: 9876, // karma web server port + colors: true, + logLevel: config.LOG_INFO, + browsers: ['ChromeHeadless', 'FirefoxHeadless'], + autoWatch: false, + concurrency: Infinity, + customLaunchers: { + FirefoxHeadless: { + base: 'Firefox', + flags: ['-headless'] + } + } + }) } diff --git a/radiomanifest.js b/radiomanifest.js index a13ac23..2135998 100644 --- a/radiomanifest.js +++ b/radiomanifest.js @@ -26,137 +26,134 @@ class RadioManifest { } } - -function getStreaminfoUrl(siteurl) { - return siteurl + '/streaminfo.json'; // XXX: improve this logic +function getStreaminfoUrl (siteurl) { + return siteurl + '/streaminfo.json' // XXX: improve this logic } -function getManifestUrl(siteurl) { - return siteurl + '/radiomanifest.xml'; // XXX: improve this logic +function getManifestUrl (siteurl) { + return siteurl + '/radiomanifest.xml' // XXX: improve this logic } -function parseRadioManifest(xml) { - var res = xml.evaluate('/radio-manifest/streaming/source', xml) - var sources = [] - while(true) { - var src = res.iterateNext() - if(src === null) break; - if(!src.hasAttribute("priority")) { - src.setAttribute("priority", "0") - } else if(parseInt(src.getAttribute("priority"), 10) < 0) { - continue; +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 } sources.push(src) } - sources.sort(function cmp(a,b) { - return parseInt(a.getAttribute("priority", 10)) < parseInt(b.getAttribute("priority", 10)); + sources.sort(function cmp (a, b) { + return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10)) }) - + res = xml.evaluate('/radio-manifest/schedule', xml) - var scheduleEl = res.iterateNext() - var schedule = null - if(scheduleEl !== null) { - schedule = scheduleEl.getAttribute("url") + const scheduleEl = res.iterateNext() + let schedule = null + if (scheduleEl !== null) { + schedule = scheduleEl.getAttribute('url') } - + res = xml.evaluate('/radio-manifest/shows', xml) - var showsEl = res.iterateNext() - var shows = null - if(showsEl !== null) { - shows = showsEl.getAttribute("src") + const showsEl = res.iterateNext() + let shows = null + if (showsEl !== null) { + shows = showsEl.getAttribute('src') } - - var manifest = new Radio(sources, schedule, shows) + + const manifest = new Radio(sources, schedule, shows) return manifest } -function Radio(sources, schedule, shows) { +function Radio (sources, schedule, shows) { this.streaming = new RadioStreaming(sources) this.schedule = schedule this.shows = shows - this.name = "" + this.name = '' } -Radio.prototype.getStreaming = function() { +Radio.prototype.getStreaming = function () { return this.streaming } -Radio.prototype.setName = function(name) { +Radio.prototype.setName = function (name) { this.name = name } -function RadioStreaming(sources) { +function RadioStreaming (sources) { this.sources = sources } -RadioStreaming.prototype.getOptions = function() { - return this.sources.map(function(x) { +RadioStreaming.prototype.getOptions = function () { + return this.sources.map(function (x) { return x.getAttribute('name') }) } RadioStreaming.prototype.getSource = function (name) { - if(name === undefined) { - + if (name === undefined) { + } - var s = this.sources.find(function(x) { + const s = this.sources.find(function (x) { return x.getAttribute('name') === name }) - if(s === undefined) return s + if (s === undefined) return s return s.getAttribute('src') } -async function get(siteurl, options) { - let resp = await fetch(getManifestUrl(siteurl)); +async function get (siteurl, options) { + let resp = await fetch(getManifestUrl(siteurl)) let text = await resp.text() - - var parser = new DOMParser(); - var dom = parser.parseFromString(text, 'text/xml') - var manifest = parseRadioManifest(dom) - + + const parser = new DOMParser() + const dom = parser.parseFromString(text, 'text/xml') + const manifest = parseRadioManifest(dom) + resp = null try { - resp = await fetch(getStreaminfoUrl(siteurl)); + resp = await fetch(getStreaminfoUrl(siteurl)) text = await resp.text() - - var data = JSON.parse(text) - var name = data["icy-name"] - if(name !== undefined) { + + const data = JSON.parse(text) + const name = data['icy-name'] + if (name !== undefined) { manifest.setName(name) } } catch (e) { - if(e instanceof TypeError && e.message.startsWith('NetworkError')) { + if (e instanceof TypeError && e.message.startsWith('NetworkError')) { // the fetch has failed true - } else if(e instanceof SyntaxError && e.message.startsWith('JSON.parse')) { + } else if (e instanceof SyntaxError && e.message.startsWith('JSON.parse')) { true - } - else { + } else { console.error('Error', e) throw e } } - + // XXX: in base alle options fai fetch anche di altra roba return manifest } -function parseM3U(body) { - body.split("\n").filter((e) => { - if(e.startsWith("#")) { +function parseM3U (body) { + body.split('\n').filter((e) => { + if (e.startsWith('#')) { return false } else { - try { new URL(e); return true } - catch {return false} + try { new URL(e); return true } catch { return false } } }) } module.exports = { - get: get, - objs: { - Radio: Radio, - RadioStreaming: RadioStreaming - }, - parsers: { - M3U: parseM3U, - radioManifest: parseRadioManifest, - } + get: get, + objs: { + Radio: Radio, + RadioStreaming: RadioStreaming + }, + parsers: { + M3U: parseM3U, + radioManifest: parseRadioManifest + } } diff --git a/test/radiomanifest.test.js b/test/radiomanifest.test.js index a42eaf9..6d8f59c 100644 --- a/test/radiomanifest.test.js +++ b/test/radiomanifest.test.js @@ -5,9 +5,7 @@ chai.use(require('chai-as-promised')) const expect = chai.expect describe('radiomanifest.js', () => { - describe('Get a radiomanifest', () => { - it('should return a Promise', () => { const p = radiomanifest.get('http://omstring') expect(p instanceof Promise).to.be.eql(true) @@ -17,7 +15,6 @@ describe('radiomanifest.js', () => { const p = radiomanifest.get('http://invalidurl') expect(p).to.eventually.be.rejected }) - }) describe('streaming', () => { @@ -25,7 +22,4 @@ describe('radiomanifest.js', () => { }) }) - - - }) diff --git a/ui.js b/ui.js index cabb0c7..d6680eb 100644 --- a/ui.js +++ b/ui.js @@ -1,10 +1,11 @@ -async function fai() { - var radio = await get("https://www.ondarossa.info/") - // var radio = await get("https://boyska.degenerazione.xyz/radiomanifest/examples/empty/") - console.log('radio?', radio) - var s = radio.getStreaming() - console.log(s.getOptions()) - console.log(s.getSource(s.getOptions()[0])) +// const radiomanifest = require('radiomanifest.js') +async function fai () { + const radio = await radiomanifest.get('https://www.ondarossa.info/') + // var radio = await get("https://boyska.degenerazione.xyz/radiomanifest/examples/empty/") + console.log('radio?', radio) + const s = radio.getStreaming() + console.log(s.getOptions()) + console.log(s.getSource(s.getOptions()[0])) } fai()