radiomanifest.js/radiomanifest.js

160 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-11-18 15:35:32 +01:00
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 () {
}
}
2021-11-19 18:08:09 +01:00
function getStreaminfoUrl (siteurl) {
return siteurl + '/streaminfo.json' // XXX: improve this logic
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
function getManifestUrl (siteurl) {
return siteurl + '/radiomanifest.xml' // XXX: improve this logic
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
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
2021-11-16 23:22:16 +01:00
}
2021-11-18 15:35:32 +01:00
sources.push(src)
}
2021-11-19 18:08:09 +01:00
sources.sort(function cmp (a, b) {
return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10))
2021-11-18 15:35:32 +01:00
})
2021-11-19 18:08:09 +01:00
2021-11-18 15:35:32 +01:00
res = xml.evaluate('/radio-manifest/schedule', xml)
2021-11-19 18:08:09 +01:00
const scheduleEl = res.iterateNext()
let schedule = null
if (scheduleEl !== null) {
schedule = scheduleEl.getAttribute('url')
2021-11-18 15:35:32 +01:00
}
2021-11-19 18:08:09 +01:00
2021-11-18 15:35:32 +01:00
res = xml.evaluate('/radio-manifest/shows', xml)
2021-11-19 18:08:09 +01:00
const showsEl = res.iterateNext()
let shows = null
if (showsEl !== null) {
shows = showsEl.getAttribute('src')
2021-11-18 15:35:32 +01:00
}
2021-11-19 18:08:09 +01:00
const manifest = new Radio(sources, schedule, shows)
2021-11-18 15:35:32 +01:00
return manifest
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
function Radio (sources, schedule, shows) {
2021-11-18 15:35:32 +01:00
this.streaming = new RadioStreaming(sources)
this.schedule = schedule
this.shows = shows
2021-11-19 18:08:09 +01:00
this.name = ''
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
Radio.prototype.getStreaming = function () {
2021-11-18 15:35:32 +01:00
return this.streaming
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
Radio.prototype.setName = function (name) {
2021-11-18 15:35:32 +01:00
this.name = name
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
function RadioStreaming (sources) {
2021-11-18 15:35:32 +01:00
this.sources = sources
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
RadioStreaming.prototype.getOptions = function () {
return this.sources.map(function (x) {
2021-11-18 15:35:32 +01:00
return x.getAttribute('name')
})
2021-11-16 23:22:16 +01:00
}
RadioStreaming.prototype.getSource = function (name) {
2021-11-19 18:08:09 +01:00
if (name === undefined) {
2021-11-18 15:35:32 +01:00
}
2021-11-19 18:08:09 +01:00
const s = this.sources.find(function (x) {
2021-11-18 15:35:32 +01:00
return x.getAttribute('name') === name
})
2021-11-19 18:08:09 +01:00
if (s === undefined) return s
2021-11-18 15:35:32 +01:00
return s.getAttribute('src')
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
async function get (siteurl, options) {
let resp = await fetch(getManifestUrl(siteurl))
2021-11-18 15:35:32 +01:00
let text = await resp.text()
2021-11-19 18:08:09 +01:00
const parser = new DOMParser()
const dom = parser.parseFromString(text, 'text/xml')
const manifest = parseRadioManifest(dom)
2021-11-18 15:35:32 +01:00
resp = null
try {
2021-11-19 18:08:09 +01:00
resp = await fetch(getStreaminfoUrl(siteurl))
2021-11-18 15:35:32 +01:00
text = await resp.text()
2021-11-19 18:08:09 +01:00
const data = JSON.parse(text)
const name = data['icy-name']
if (name !== undefined) {
2021-11-18 15:35:32 +01:00
manifest.setName(name)
2021-11-16 23:22:16 +01:00
}
2021-11-18 15:35:32 +01:00
} catch (e) {
2021-11-19 18:08:09 +01:00
if (e instanceof TypeError && e.message.startsWith('NetworkError')) {
2021-11-18 15:35:32 +01:00
// the fetch has failed
true
2021-11-19 18:08:09 +01:00
} else if (e instanceof SyntaxError && e.message.startsWith('JSON.parse')) {
2021-11-18 15:35:32 +01:00
true
2021-11-19 18:08:09 +01:00
} else {
2021-11-18 15:35:32 +01:00
console.error('Error', e)
throw e
}
}
2021-11-19 18:08:09 +01:00
2021-11-18 15:35:32 +01:00
// XXX: in base alle options fai fetch anche di altra roba
return manifest
2021-11-16 23:22:16 +01:00
}
2021-11-19 18:08:09 +01:00
function parseM3U (body) {
body.split('\n').filter((e) => {
if (e.startsWith('#')) {
2021-11-18 15:35:32 +01:00
return false
} else {
2021-11-19 18:08:09 +01:00
try { new URL(e); return true } catch { return false }
2021-11-18 15:35:32 +01:00
}
})
2021-11-16 23:22:16 +01:00
}
2021-11-19 17:03:22 +01:00
module.exports = {
2021-11-19 18:08:09 +01:00
get: get,
objs: {
Radio: Radio,
RadioStreaming: RadioStreaming
},
parsers: {
M3U: parseM3U,
radioManifest: parseRadioManifest
}
2021-11-19 17:03:22 +01:00
}