shows.js 2.6 KB

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