forked from boyska/radiomanifest.js
make it ESM ready
This commit is contained in:
parent
baa181354f
commit
207f06a791
1 changed files with 288 additions and 292 deletions
|
@ -1,25 +1,23 @@
|
||||||
const fetch = require('isomorphic-unfetch')
|
import fetch from 'isomorphic-unfetch'
|
||||||
const shows = require('./shows.js')
|
import shows from './shows.js'
|
||||||
const calendar = require('./calendar.js')
|
import calendar from './calendar.js'
|
||||||
|
|
||||||
function getStreaminfoUrl(siteurl) {
|
function getStreaminfoUrl(siteurl) {
|
||||||
return siteurl + '/streaminfo.json' // XXX: improve this logic
|
return siteurl + "/streaminfo.json" // XXX: improve this logic
|
||||||
}
|
}
|
||||||
function getManifestUrl(siteurl) {
|
function getManifestUrl(siteurl) {
|
||||||
return siteurl + '/radiomanifest.xml' // XXX: improve this logic
|
return siteurl + "/radiomanifest.xml" // XXX: improve this logic
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAttribute(el, attr, default_value) {
|
function getAttribute(el, attr, default_value) {
|
||||||
if(el.hasAttribute(attr))
|
if (el.hasAttribute(attr)) return el.getAttribute(attr)
|
||||||
return el.getAttribute(attr);
|
return default_value
|
||||||
return default_value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents everything we know about a radio. This includes, but is not limited to, radiomanifest.xml.
|
* Represents everything we know about a radio. This includes, but is not limited to, radiomanifest.xml.
|
||||||
*/
|
*/
|
||||||
class Radio {
|
class Radio {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array} [sources] optional
|
* @param {Array} [sources] optional
|
||||||
* @param {string} [scheduleURL]
|
* @param {string} [scheduleURL]
|
||||||
|
@ -31,8 +29,8 @@ class Radio {
|
||||||
this.scheduleURL = scheduleURL
|
this.scheduleURL = scheduleURL
|
||||||
this.showsURL = showsURL
|
this.showsURL = showsURL
|
||||||
this.feed = feed
|
this.feed = feed
|
||||||
this.name = ''
|
this.name = ""
|
||||||
this.description = ''
|
this.description = ""
|
||||||
this.logo = null
|
this.logo = null
|
||||||
this.shows = []
|
this.shows = []
|
||||||
this.schedule = null
|
this.schedule = null
|
||||||
|
@ -128,38 +126,44 @@ class Radio {
|
||||||
*/
|
*/
|
||||||
static fromDOM(xml) {
|
static fromDOM(xml) {
|
||||||
const doc = xml.cloneNode(true)
|
const doc = xml.cloneNode(true)
|
||||||
let res = doc.evaluate('/radio-manifest/streaming/source', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
|
let res = doc.evaluate(
|
||||||
|
"/radio-manifest/streaming/source",
|
||||||
|
doc,
|
||||||
|
null,
|
||||||
|
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
|
||||||
|
null
|
||||||
|
)
|
||||||
const sources = []
|
const sources = []
|
||||||
for (let i = 0; i < res.snapshotLength; i++) {
|
for (let i = 0; i < res.snapshotLength; i++) {
|
||||||
const src = res.snapshotItem(i)
|
const src = res.snapshotItem(i)
|
||||||
|
|
||||||
if (!src.hasAttribute('priority')) {
|
if (!src.hasAttribute("priority")) {
|
||||||
src.setAttribute('priority', '0')
|
src.setAttribute("priority", "0")
|
||||||
} else if (parseInt(src.getAttribute('priority'), 10) < 0) {
|
} else if (parseInt(src.getAttribute("priority"), 10) < 0) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sources.push(src)
|
sources.push(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
res = doc.evaluate('/radio-manifest/schedule', doc)
|
res = doc.evaluate("/radio-manifest/schedule", doc)
|
||||||
const scheduleEl = res.iterateNext()
|
const scheduleEl = res.iterateNext()
|
||||||
let scheduleURL = null
|
let scheduleURL = null
|
||||||
if (scheduleEl !== null) {
|
if (scheduleEl !== null) {
|
||||||
scheduleURL = scheduleEl.getAttribute('src')
|
scheduleURL = scheduleEl.getAttribute("src")
|
||||||
}
|
}
|
||||||
|
|
||||||
res = xml.evaluate('/radio-manifest/shows', xml)
|
res = xml.evaluate("/radio-manifest/shows", xml)
|
||||||
const showsEl = res.iterateNext()
|
const showsEl = res.iterateNext()
|
||||||
let showsURL = null
|
let showsURL = null
|
||||||
if (showsEl !== null) {
|
if (showsEl !== null) {
|
||||||
showsURL = showsEl.getAttribute('src')
|
showsURL = showsEl.getAttribute("src")
|
||||||
}
|
}
|
||||||
|
|
||||||
res = xml.evaluate('/radio-manifest/feed', xml)
|
res = xml.evaluate("/radio-manifest/feed", xml)
|
||||||
const feedEl = res.iterateNext()
|
const feedEl = res.iterateNext()
|
||||||
let feed = null
|
let feed = null
|
||||||
if (feedEl !== null) {
|
if (feedEl !== null) {
|
||||||
feed = feedEl.getAttribute('src')
|
feed = feedEl.getAttribute("src")
|
||||||
}
|
}
|
||||||
|
|
||||||
const manifest = new Radio(sources, scheduleURL, showsURL, feed)
|
const manifest = new Radio(sources, scheduleURL, showsURL, feed)
|
||||||
|
@ -173,9 +177,7 @@ class Radio {
|
||||||
*/
|
*/
|
||||||
class RadioStreaming {
|
class RadioStreaming {
|
||||||
constructor(sources) {
|
constructor(sources) {
|
||||||
this.sources = sources.sort(
|
this.sources = sources.sort((a, b) => this.getPriority(a) < this.getPriority(a))
|
||||||
(a,b) => this.getPriority(a) < this.getPriority(a)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -184,7 +186,7 @@ class RadioStreaming {
|
||||||
*/
|
*/
|
||||||
getOptions() {
|
getOptions() {
|
||||||
return this.sources.map(function (x) {
|
return this.sources.map(function (x) {
|
||||||
return x.getAttribute('name')
|
return x.getAttribute("name")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,16 +194,14 @@ class RadioStreaming {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
getPriority(element) {
|
getPriority(element) {
|
||||||
return parseInt(getAttribute(element, 'priority', '1'))
|
return parseInt(getAttribute(element, "priority", "1"))
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
getTopPrioritySources() {
|
getTopPrioritySources() {
|
||||||
var topPriority = this.getPriority(this.sources[0])
|
var topPriority = this.getPriority(this.sources[0])
|
||||||
return this.sources.filter(
|
return this.sources.filter(src => parseInt(src.getAttribute("priority"), 10) === topPriority)
|
||||||
(src) => parseInt(src.getAttribute('priority'), 10) === topPriority
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @return {string} url of the source. Note that this is much probably a playlist, in M3U format
|
* @return {string} url of the source. Note that this is much probably a playlist, in M3U format
|
||||||
|
@ -211,10 +211,10 @@ class RadioStreaming {
|
||||||
return this.getTopPrioritySources()[0]
|
return this.getTopPrioritySources()[0]
|
||||||
}
|
}
|
||||||
const s = this.sources.find(function (x) {
|
const s = this.sources.find(function (x) {
|
||||||
return x.getAttribute('name') === name
|
return x.getAttribute("name") === name
|
||||||
})
|
})
|
||||||
if (s === undefined) return s
|
if (s === undefined) return s
|
||||||
return s.getAttribute('src')
|
return s.getAttribute("src")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -226,7 +226,7 @@ class RadioStreaming {
|
||||||
var allSources = this.getTopPrioritySources()
|
var allSources = this.getTopPrioritySources()
|
||||||
var allAudios = []
|
var allAudios = []
|
||||||
for (let src of allSources) {
|
for (let src of allSources) {
|
||||||
let url = src.getAttribute('src')
|
let url = src.getAttribute("src")
|
||||||
let resp = await fetch(url)
|
let resp = await fetch(url)
|
||||||
allAudios.unshift(...parseM3U(await resp.text()))
|
allAudios.unshift(...parseM3U(await resp.text()))
|
||||||
}
|
}
|
||||||
|
@ -255,7 +255,7 @@ async function get (siteurl, options) {
|
||||||
let text = await resp.text()
|
let text = await resp.text()
|
||||||
|
|
||||||
const parser = new DOMParser()
|
const parser = new DOMParser()
|
||||||
const dom = parser.parseFromString(text, 'text/xml')
|
const dom = parser.parseFromString(text, "text/xml")
|
||||||
const manifest = Radio.fromDOM(dom)
|
const manifest = Radio.fromDOM(dom)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -266,13 +266,11 @@ async function get (siteurl, options) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
manifest.schedule = await calendar.get(manifest)
|
manifest.schedule = await calendar.get(manifest)
|
||||||
if (manifest.schedule !== undefined)
|
if (manifest.schedule !== undefined) manifest.schedule.radio = manifest
|
||||||
manifest.schedule.radio = manifest
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Error while fetching shows file", e)
|
console.error("Error while fetching shows file", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
resp = null
|
resp = null
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getStreaminfoUrl(siteurl))
|
resp = await fetch(getStreaminfoUrl(siteurl))
|
||||||
|
@ -284,16 +282,16 @@ async function get (siteurl, options) {
|
||||||
text = await resp.text()
|
text = await resp.text()
|
||||||
|
|
||||||
const data = JSON.parse(text)
|
const data = JSON.parse(text)
|
||||||
const name = data['icy-name']
|
const name = data["icy-name"]
|
||||||
if (name !== undefined) {
|
if (name !== undefined) {
|
||||||
manifest.setName(name)
|
manifest.setName(name)
|
||||||
}
|
}
|
||||||
const desc = data['icy-description']
|
const desc = data["icy-description"]
|
||||||
if (desc !== undefined) {
|
if (desc !== undefined) {
|
||||||
manifest.setDescription(desc)
|
manifest.setDescription(desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
const logo = data['icy-logo']
|
const logo = data["icy-logo"]
|
||||||
if (desc !== undefined) {
|
if (desc !== undefined) {
|
||||||
manifest.setLogo(logo)
|
manifest.setLogo(logo)
|
||||||
}
|
}
|
||||||
|
@ -301,7 +299,7 @@ async function get (siteurl, options) {
|
||||||
if (e instanceof SyntaxError) {
|
if (e instanceof SyntaxError) {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
console.error('Error', e)
|
console.error("Error", e)
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,15 +308,14 @@ async function get (siteurl, options) {
|
||||||
return manifest
|
return manifest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function parseM3U(body) {
|
function parseM3U(body) {
|
||||||
return body.split('\n').filter((line) => {
|
return body.split("\n").filter(line => {
|
||||||
if (line.startsWith('#')) {
|
if (line.startsWith("#")) {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
new URL(line); return true
|
new URL(line)
|
||||||
|
return true
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -326,8 +323,7 @@ function parseM3U (body) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
module.exports = {
|
|
||||||
get: get,
|
get: get,
|
||||||
objs: {
|
objs: {
|
||||||
Radio: Radio,
|
Radio: Radio,
|
||||||
|
@ -336,6 +332,6 @@ module.exports = {
|
||||||
parsers: {
|
parsers: {
|
||||||
M3U: parseM3U,
|
M3U: parseM3U,
|
||||||
radioManifest: Radio.fromDOM,
|
radioManifest: Radio.fromDOM,
|
||||||
shows: shows.parse,
|
shows: shows.parse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue