radiomanifest.js/radiomanifest.js
2022-01-30 00:26:07 +01:00

284 lines
6.9 KiB
JavaScript

const fetch = require('isomorphic-unfetch')
function getStreaminfoUrl (siteurl) {
return siteurl + '/streaminfo.json' // XXX: improve this logic
}
function getManifestUrl (siteurl) {
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;
}
class Radio {
constructor (sources, schedule, showsURL, feed) {
this.streaming = new RadioStreaming(sources)
this.schedule = schedule
this.showsURL = showsURL
this.feed = feed
this.name = ''
this.shows = []
}
getStreaming () {
return this.streaming
}
setName (name) {
this.name = name
}
getShows() {
return this.shows
}
getShowByName (showName) {
return this.shows.find(s => s.name === showName)
}
getSchedule () {
}
getShowAtTime () {
}
static fromDOM (xml) {
const doc = xml.cloneNode(true)
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) {
continue
}
sources.push(src)
}
res = doc.evaluate('/radio-manifest/schedule', doc)
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 showsURL = null
if (showsEl !== null) {
showsURL = 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, showsURL, feed)
return manifest
}
}
class RadioStreaming {
constructor (sources) {
this.sources = sources.sort(
(a,b) => this.getPriority(a) < this.getPriority(a)
)
}
getOptions() {
return this.sources.map(function (x) {
return x.getAttribute('name')
})
}
// this is private
getPriority(element) {
return parseInt(getAttribute(element, 'priority', '1'))
}
// this is private
getTopPrioritySources() {
var topPriority = this.getPriority(this.sources[0])
return this.sources.filter(
(src) => parseInt(src.getAttribute('priority'), 10) === topPriority
)
}
getSource(name) {
if (name === undefined) {
return this.getTopPrioritySources()[0]
}
const s = this.sources.find(function (x) {
return x.getAttribute('name') === name
})
if (s === undefined) return s
return s.getAttribute('src')
}
async pickURLs() {
var allSources = this.getTopPrioritySources()
var allAudios = []
for(let src of allSources) {
let url = src.getAttribute('src')
let resp = await fetch(url)
allAudios.unshift(... parseM3U(await resp.text()))
}
return allAudios
}
async pickURL() {
var allAudios = await this.pickURLs()
return allAudios[0]
}
}
async function get (siteurl, options) {
let resp = await fetch(getManifestUrl(siteurl))
let text = await resp.text()
const parser = new DOMParser()
const dom = parser.parseFromString(text, 'text/xml')
const manifest = Radio.fromDOM(dom)
try {
manifest.shows = await getShows(manifest)
} catch (e) {
console.error("Error while fetching shows file", e)
}
resp = null
try {
resp = await fetch(getStreaminfoUrl(siteurl))
} catch (e) {
true
}
if(resp !== null) {
try {
text = await resp.text()
const data = JSON.parse(text)
const name = data['icy-name']
if (name !== undefined) {
manifest.setName(name)
}
} catch (e) {
if (e instanceof SyntaxError) {
true
} else {
console.error('Error', e)
throw e
}
}
}
return manifest
}
async function getShows(manifest) {
if (manifest.showsURL) {
let resp = null
try {
resp = await fetch(manifest.showsURL)
} catch (e) {
true
}
if (resp !== null) {
try {
text = await resp.text()
const parser = new DOMParser()
const showsDom = parser.parseFromString(text, 'text/xml')
return parseRadioShows(showsDom)
} catch (e) {
console.error('Error while parsing shows file', e)
throw e
}
}
}
}
function parseM3U (body) {
return body.split('\n').filter((line) => {
if (line.startsWith('#')) {
return false
} else {
try {
new URL(line); return true
} catch {
return false
}
}
})
}
class RadioShow {
constructor(name, description, website, feed, schedule, radio_calendar) {
this.name = name
this.description = description
this.website = website
this.feed = feed
this.schedule = schedule
this.radio_calendar = radio_calendar
}
getName() {
return this.name
}
getWebsite() {
return this.website
}
getFeed() {
return this.feed
}
getSchedule() {
return this.schedule
}
}
function showsNamespaceResolver(prefix) {
const prefixes = {
show: 'https://radiomanifest.degenerazione.xyz/shows/',
}
return prefixes[prefix] || null
}
function parseRadioShows(xml) {
const doc = xml.cloneNode(true)
const bookmarks = doc.evaluate('//bookmark', doc, showsNamespaceResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
const shows = []
for (let i = 0; i < bookmarks.snapshotLength; i++) {
const bm = bookmarks.snapshotItem(i)
const name = doc.evaluate('./info/metadata/show:name', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
const website = doc.evaluate('./info/metadata/show:website', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
const feed = doc.evaluate('./info/metadata/show:feed', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
const schedule = doc.evaluate('./info/metadata/show:schedule', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
let description = doc.evaluate('./info/metadata/show:description', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
if(description === '') {
description = doc.evaluate('./description', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
}
const show = new RadioShow(name || null, description, website || null, feed || null, schedule || null)
shows.push(show)
}
return shows
}
module.exports = {
get: get,
objs: {
Radio: Radio,
RadioStreaming: RadioStreaming
},
parsers: {
M3U: parseM3U,
radioManifest: Radio.fromDOM,
shows: parseRadioShows,
}
}