1
0
Fork 0

make it ESM ready

This commit is contained in:
lesion 2023-03-18 21:05:04 +01:00
parent baa181354f
commit 207f06a791

View file

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