radiomanifest.js/shows.js

144 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-01-30 14:01:38 +01:00
/**
* Represents a single show. This show could be defined in the shows.xml file, or could be inferred from the
* schedule.
*/
2023-01-23 12:42:25 +01:00
2022-01-30 01:06:35 +01:00
class RadioShow {
constructor(name, description, website, feed, schedule, radio_calendar) {
2023-09-14 01:07:07 +02:00
this.name = name;
this.description = description;
this.website = website;
this.feed = feed;
this.schedule = schedule;
this.radio_calendar = radio_calendar;
2022-01-30 01:06:35 +01:00
}
getName() {
2023-09-14 01:07:07 +02:00
return this.name;
2022-01-30 01:06:35 +01:00
}
getWebsite() {
2023-09-14 01:07:07 +02:00
return this.website;
2022-01-30 01:06:35 +01:00
}
getFeed() {
2023-09-14 01:07:07 +02:00
return this.feed;
2022-01-30 01:06:35 +01:00
}
getSchedule() {
2023-09-14 01:07:07 +02:00
return this.schedule;
2022-01-30 01:06:35 +01:00
}
}
2022-01-30 14:01:38 +01:00
/**
* @private
* @return {Array<RadioShow>}
*/
2022-01-30 01:06:35 +01:00
function parseRadioShows(xml) {
2023-09-14 01:07:07 +02:00
const doc = xml.cloneNode(true);
const bookmarks = doc.evaluate(
"//bookmark",
doc,
showsNamespaceResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null,
);
const shows = [];
2022-01-30 01:06:35 +01:00
for (let i = 0; i < bookmarks.snapshotLength; i++) {
2023-09-14 01:07:07 +02:00
const bm = bookmarks.snapshotItem(i);
2022-01-30 01:06:35 +01:00
2023-09-14 01:07:07 +02:00
let name = doc.evaluate(
"./info/metadata/show:name",
bm,
showsNamespaceResolver,
XPathResult.STRING_TYPE,
).stringValue;
if (name === "") {
name = doc.evaluate(
"./title",
bm,
showsNamespaceResolver,
XPathResult.STRING_TYPE,
).stringValue;
2022-01-30 01:06:35 +01:00
}
2023-09-14 01:07:07 +02:00
let website = doc.evaluate(
"./info/metadata/show:website",
bm,
showsNamespaceResolver,
XPathResult.STRING_TYPE,
).stringValue;
if (website === "") {
website = bm.getAttribute("href");
2022-01-30 01:06:35 +01:00
}
2023-09-14 01:07:07 +02:00
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(
"./desc",
bm,
showsNamespaceResolver,
XPathResult.STRING_TYPE,
).stringValue;
2022-01-30 01:06:35 +01:00
}
2023-09-14 01:07:07 +02:00
const show = new RadioShow(
name,
description || null,
website || null,
feed || null,
schedule || null,
);
shows.push(show);
2022-01-30 01:06:35 +01:00
}
2023-09-14 01:07:07 +02:00
return shows;
2022-01-30 01:06:35 +01:00
}
async function getShows(manifest) {
if (manifest.showsURL) {
2023-09-14 01:07:07 +02:00
let resp = null;
2022-01-30 01:06:35 +01:00
try {
2023-09-14 01:07:07 +02:00
resp = await fetch(manifest.showsURL);
2022-01-30 01:06:35 +01:00
} catch (e) {
2023-09-14 01:07:07 +02:00
true;
2022-01-30 01:06:35 +01:00
}
if (resp !== null) {
try {
2023-09-14 01:07:07 +02:00
const text = await resp.text();
const parser = new DOMParser();
const showsDom = parser.parseFromString(text, "text/xml");
return parseRadioShows(showsDom);
2022-01-30 01:06:35 +01:00
} catch (e) {
2023-09-14 01:07:07 +02:00
console.error("Error while parsing shows file", e);
throw e;
2022-01-30 01:06:35 +01:00
}
}
}
}
function showsNamespaceResolver(prefix) {
const prefixes = {
2023-09-14 01:07:07 +02:00
show: "https://radiomanifest.degenerazione.xyz/shows/",
};
return prefixes[prefix] || null;
2022-01-30 01:06:35 +01:00
}
2023-01-23 12:42:25 +01:00
export default {
2022-01-30 01:06:35 +01:00
get: getShows,
parse: parseRadioShows,
RadioShow: RadioShow,
2023-09-14 01:07:07 +02:00
};