Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calendarfetcher.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Magic Mirror
  2. * Node Helper: Calendar - CalendarFetcher
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. const CalendarUtils = require("./calendarutils");
  8. const Log = require("logger");
  9. const NodeHelper = require("node_helper");
  10. const ical = require("node-ical");
  11. const fetch = require("node-fetch");
  12. const digest = require("digest-fetch");
  13. const https = require("https");
  14. /**
  15. *
  16. * @param {string} url The url of the calendar to fetch
  17. * @param {number} reloadInterval Time in ms the calendar is fetched again
  18. * @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
  19. * @param {number} maximumEntries The maximum number of events fetched.
  20. * @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
  21. * @param {object} auth The object containing options for authentication against the calendar.
  22. * @param {boolean} includePastEvents If true events from the past maximumNumberOfDays will be fetched too
  23. * @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
  24. * @class
  25. */
  26. const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents, selfSignedCert) {
  27. let reloadTimer = null;
  28. let events = [];
  29. let fetchFailedCallback = function () {};
  30. let eventsReceivedCallback = function () {};
  31. /**
  32. * Initiates calendar fetch.
  33. */
  34. const fetchCalendar = () => {
  35. clearTimeout(reloadTimer);
  36. reloadTimer = null;
  37. const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
  38. let fetcher = null;
  39. let httpsAgent = null;
  40. let headers = {
  41. "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
  42. };
  43. if (selfSignedCert) {
  44. httpsAgent = new https.Agent({
  45. rejectUnauthorized: false
  46. });
  47. }
  48. if (auth) {
  49. if (auth.method === "bearer") {
  50. headers.Authorization = "Bearer " + auth.pass;
  51. } else if (auth.method === "digest") {
  52. fetcher = new digest(auth.user, auth.pass).fetch(url, { headers: headers, agent: httpsAgent });
  53. } else {
  54. headers.Authorization = "Basic " + Buffer.from(auth.user + ":" + auth.pass).toString("base64");
  55. }
  56. }
  57. if (fetcher === null) {
  58. fetcher = fetch(url, { headers: headers, agent: httpsAgent });
  59. }
  60. fetcher
  61. .then(NodeHelper.checkFetchStatus)
  62. .then((response) => response.text())
  63. .then((responseData) => {
  64. let data = [];
  65. try {
  66. data = ical.parseICS(responseData);
  67. Log.debug("parsed data=" + JSON.stringify(data));
  68. events = CalendarUtils.filterEvents(data, {
  69. excludedEvents,
  70. includePastEvents,
  71. maximumEntries,
  72. maximumNumberOfDays
  73. });
  74. } catch (error) {
  75. fetchFailedCallback(this, error);
  76. scheduleTimer();
  77. return;
  78. }
  79. this.broadcastEvents();
  80. scheduleTimer();
  81. })
  82. .catch((error) => {
  83. fetchFailedCallback(this, error);
  84. scheduleTimer();
  85. });
  86. };
  87. /**
  88. * Schedule the timer for the next update.
  89. */
  90. const scheduleTimer = function () {
  91. clearTimeout(reloadTimer);
  92. reloadTimer = setTimeout(function () {
  93. fetchCalendar();
  94. }, reloadInterval);
  95. };
  96. /* public methods */
  97. /**
  98. * Initiate fetchCalendar();
  99. */
  100. this.startFetch = function () {
  101. fetchCalendar();
  102. };
  103. /**
  104. * Broadcast the existing events.
  105. */
  106. this.broadcastEvents = function () {
  107. Log.info("Calendar-Fetcher: Broadcasting " + events.length + " events.");
  108. eventsReceivedCallback(this);
  109. };
  110. /**
  111. * Sets the on success callback
  112. *
  113. * @param {Function} callback The on success callback.
  114. */
  115. this.onReceive = function (callback) {
  116. eventsReceivedCallback = callback;
  117. };
  118. /**
  119. * Sets the on error callback
  120. *
  121. * @param {Function} callback The on error callback.
  122. */
  123. this.onError = function (callback) {
  124. fetchFailedCallback = callback;
  125. };
  126. /**
  127. * Returns the url of this fetcher.
  128. *
  129. * @returns {string} The url of this fetcher.
  130. */
  131. this.url = function () {
  132. return url;
  133. };
  134. /**
  135. * Returns current available events for this fetcher.
  136. *
  137. * @returns {object[]} The current available events for this fetcher.
  138. */
  139. this.events = function () {
  140. return events;
  141. };
  142. };
  143. module.exports = CalendarFetcher;