calendar.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const ICAL = require('ical.js')
  2. const shows = require('./shows.js')
  3. /**
  4. * Represent a schedule (ie: a .ics file)
  5. */
  6. class RadioSchedule {
  7. constructor (calendar, radio) {
  8. this.calendar = calendar // ICAL.Calendar
  9. this.radio = radio // radiomanifest.Radio
  10. }
  11. /**
  12. * Get a list of all known {@link vEvent}s
  13. *
  14. * @returns {Array<external:ICAL~Component>}
  15. * */
  16. getEvents() {
  17. return this.calendar.getAllSubcomponents('vevent');
  18. }
  19. /**
  20. * @returns {external:ICAL~Component} if nothing is going on right now, `null` is returned
  21. */
  22. getNowEvent(now) {
  23. var ev_now = this.getEvents().filter(function(vevent) {
  24. const ev = new ICAL.Event(vevent)
  25. return isNow(ev, now)
  26. })
  27. if(ev_now.length === 0)
  28. return null
  29. if(ev_now.length > 1)
  30. console.log("More than one event right now?!")
  31. return ev_now[0]
  32. }
  33. /**
  34. * @returns {RadioShow} if nothing is going on right now, `null` is returned
  35. */
  36. getNowShow(now) {
  37. var ev = this.getNowEvent(now)
  38. if (ev === null) return null
  39. if (this.radio !== undefined) {
  40. const showid = RadioSchedule.veventGetShowID(ev)
  41. var show = this.radio.getShowByName(showid)
  42. if (show === null || show === undefined) {
  43. return new shows.RadioShow(RadioSchedule.veventGetSummary(ev))
  44. }
  45. return show;
  46. }
  47. return new shows.RadioShow(RadioSchedule.veventGetSummary(ev))
  48. }
  49. /**
  50. * @todo To be implemented
  51. */
  52. getNextEvent(now) {
  53. // XXX
  54. }
  55. /**
  56. * @todo To be implemented
  57. * @returns {NextShow} if there is no next show, nothing will be returned
  58. */
  59. getNextShow(now) {
  60. // XXX
  61. }
  62. static veventGetSummary(vevent) {
  63. return vevent.getFirstProperty('summary').getFirstValue()
  64. }
  65. static veventGetShowID(vevent) {
  66. return RadioSchedule.veventGetSummary(vevent) // XXX: X-Show-ID
  67. }
  68. }
  69. function isNow(vEvent, now) {
  70. if (now === undefined) {
  71. now = ICAL.Time.now()
  72. }
  73. if (vEvent.isRecurring()) {
  74. return isNowRecurring(vEvent, now)
  75. }
  76. return (now < vEvent.endDate) && (now > event.startDate);
  77. }
  78. function isNowRecurring(vEvent, now) {
  79. var expand = vEvent.iterator(vEvent.startDate)
  80. var next, next_end;
  81. while ((next = expand.next())) {
  82. next_end = next.clone()
  83. next_end.addDuration(vEvent.duration)
  84. if (next_end > now) {
  85. break;
  86. }
  87. }
  88. return (now < next_end && now > next);
  89. }
  90. async function get(manifest) {
  91. if (manifest.scheduleURL) {
  92. let resp = null
  93. try {
  94. resp = await fetch(manifest.scheduleURL)
  95. } catch (e) {
  96. true
  97. }
  98. if (resp !== null) {
  99. try {
  100. text = await resp.text()
  101. return parse(text)
  102. } catch (e) {
  103. console.error('Error while parsing schedule', e)
  104. throw e
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Parse ICAL and get a RadioSchedule
  111. *
  112. * @param {string} text The text, in ICS format
  113. * @returns {RadioSchedule}
  114. */
  115. function parse(text) {
  116. var jcalData = ICAL.parse(text);
  117. var vcalendar = new ICAL.Component(jcalData);
  118. return new RadioSchedule(vcalendar)
  119. }
  120. module.exports = {
  121. get: get,
  122. parse: parse,
  123. RadioSchedule: RadioSchedule,
  124. }
  125. /**
  126. * @typedef {Object} NextShow
  127. * @property {RadioShow} show The next show scheduled
  128. * @property {external:ICAL~Time} time When it will start
  129. */
  130. /**
  131. * @external ICAL
  132. * @see https://mozilla-comm.github.io/ical.js/api/index.html
  133. */
  134. /**
  135. * @class Component
  136. * @memberof external:ICAL
  137. * @inner
  138. * @see https://mozilla-comm.github.io/ical.js/api/ICAL.Component.html
  139. */
  140. /**
  141. * @class Time
  142. * @memberof external:ICAL
  143. * @inner
  144. * @see https://mozilla-comm.github.io/ical.js/api/ICAL.Time.html
  145. */