shows.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Represents a single show. This show could be defined in the shows.xml file, or could be inferred from the
  3. * schedule.
  4. */
  5. class RadioShow {
  6. constructor(name, description, website, feed, schedule, radio_calendar) {
  7. this.name = name
  8. this.description = description
  9. this.website = website
  10. this.feed = feed
  11. this.schedule = schedule
  12. this.radio_calendar = radio_calendar
  13. }
  14. getName() {
  15. return this.name
  16. }
  17. getWebsite() {
  18. return this.website
  19. }
  20. getFeed() {
  21. return this.feed
  22. }
  23. getSchedule() {
  24. return this.schedule
  25. }
  26. }
  27. /**
  28. * @private
  29. * @return {Array<RadioShow>}
  30. */
  31. function parseRadioShows(xml) {
  32. const doc = xml.cloneNode(true)
  33. const bookmarks = doc.evaluate('//bookmark', doc, showsNamespaceResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
  34. const shows = []
  35. for (let i = 0; i < bookmarks.snapshotLength; i++) {
  36. const bm = bookmarks.snapshotItem(i)
  37. let name = doc.evaluate('./info/metadata/show:name', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  38. if (name === '') {
  39. name = doc.evaluate('./title', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  40. }
  41. let website = doc.evaluate('./info/metadata/show:website', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  42. if (website === '') {
  43. website = bm.getAttribute('href')
  44. }
  45. const feed = doc.evaluate('./info/metadata/show:feed', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  46. const schedule = doc.evaluate('./info/metadata/show:schedule', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  47. let description = doc.evaluate('./info/metadata/show:description', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  48. if (description === '') {
  49. description = doc.evaluate('./desc', bm, showsNamespaceResolver, XPathResult.STRING_TYPE).stringValue
  50. }
  51. const show = new RadioShow(name, description || null, website || null, feed || null, schedule || null)
  52. shows.push(show)
  53. }
  54. return shows
  55. }
  56. async function getShows(manifest) {
  57. if (manifest.showsURL) {
  58. let resp = null
  59. try {
  60. resp = await fetch(manifest.showsURL)
  61. } catch (e) {
  62. true
  63. }
  64. if (resp !== null) {
  65. try {
  66. const text = await resp.text()
  67. const parser = new DOMParser()
  68. const showsDom = parser.parseFromString(text, 'text/xml')
  69. return parseRadioShows(showsDom)
  70. } catch (e) {
  71. console.error('Error while parsing shows file', e)
  72. throw e
  73. }
  74. }
  75. }
  76. }
  77. function showsNamespaceResolver(prefix) {
  78. const prefixes = {
  79. show: 'https://radiomanifest.degenerazione.xyz/shows/',
  80. }
  81. return prefixes[prefix] || null
  82. }
  83. export default {
  84. get: getShows,
  85. parse: parseRadioShows,
  86. RadioShow: RadioShow,
  87. }