forked from boyska/radiomanifest.js
more object-orientation
This commit is contained in:
parent
e5fa7fc3a2
commit
1dbd19c6b2
1 changed files with 68 additions and 74 deletions
142
radiomanifest.js
142
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue