mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-07-07 16:11:14 +00:00
Updating files to the latest versions from 'develop' branch
This commit is contained in:
parent
be3616abe2
commit
2afff6c432
@ -1,12 +1,12 @@
|
|||||||
/* Magic Mirror
|
/* Magic Mirror
|
||||||
* Node Helper: Calendar - CalendarFetcher
|
* Node Helper: Calendar - CalendarFetcher
|
||||||
*
|
*
|
||||||
* By Michael Teeuw http://michaelteeuw.nl
|
* By Michael Teeuw https://michaelteeuw.nl
|
||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
const Log = require("../../../js/logger.js");
|
||||||
var ical = require("./vendor/ical.js");
|
const ical = require("./vendor/ical.js");
|
||||||
var moment = require("moment");
|
const moment = require("moment");
|
||||||
|
|
||||||
var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumNumberOfDays, auth, includePastEvents) {
|
var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumNumberOfDays, auth, includePastEvents) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -21,11 +21,10 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
* Initiates calendar fetch.
|
* Initiates calendar fetch.
|
||||||
*/
|
*/
|
||||||
var fetchCalendar = function () {
|
var fetchCalendar = function () {
|
||||||
|
|
||||||
clearTimeout(reloadTimer);
|
clearTimeout(reloadTimer);
|
||||||
reloadTimer = null;
|
reloadTimer = null;
|
||||||
|
|
||||||
nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
||||||
var opts = {
|
var opts = {
|
||||||
headers: {
|
headers: {
|
||||||
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
|
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
|
||||||
@ -38,7 +37,6 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
opts.auth = {
|
opts.auth = {
|
||||||
bearer: auth.pass
|
bearer: auth.pass
|
||||||
};
|
};
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
opts.auth = {
|
opts.auth = {
|
||||||
user: auth.user,
|
user: auth.user,
|
||||||
@ -60,14 +58,15 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(data);
|
var newEvents = [];
|
||||||
newEvents = [];
|
|
||||||
|
|
||||||
// limitFunction doesn't do much limiting, see comment re: the dates array in rrule section below as to why we need to do the filtering ourselves
|
// limitFunction doesn't do much limiting, see comment re: the dates array in rrule section below as to why we need to do the filtering ourselves
|
||||||
var limitFunction = function(date, i) {return true;};
|
var limitFunction = function (date, i) {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
var eventDate = function (event, time) {
|
var eventDate = function (event, time) {
|
||||||
return (event[time].length === 8) ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time]));
|
return event[time].length === 8 ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time]));
|
||||||
};
|
};
|
||||||
|
|
||||||
for (var e in data) {
|
for (var e in data) {
|
||||||
@ -81,8 +80,7 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
past = moment().startOf("day").subtract(maximumNumberOfDays, "days").toDate();
|
past = moment().startOf("day").subtract(maximumNumberOfDays, "days").toDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME:
|
// FIXME: Ugly fix to solve the facebook birthday issue.
|
||||||
// Ugly fix to solve the facebook birthday issue.
|
|
||||||
// Otherwise, the recurring events only show the birthday for next year.
|
// Otherwise, the recurring events only show the birthday for next year.
|
||||||
var isFacebookBirthday = false;
|
var isFacebookBirthday = false;
|
||||||
if (typeof event.uid !== "undefined") {
|
if (typeof event.uid !== "undefined") {
|
||||||
@ -92,13 +90,12 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "VEVENT") {
|
if (event.type === "VEVENT") {
|
||||||
|
|
||||||
var startDate = eventDate(event, "start");
|
var startDate = eventDate(event, "start");
|
||||||
var endDate;
|
var endDate;
|
||||||
if (typeof event.end !== "undefined") {
|
if (typeof event.end !== "undefined") {
|
||||||
endDate = eventDate(event, "end");
|
endDate = eventDate(event, "end");
|
||||||
} else if (typeof event.duration !== "undefined") {
|
} else if (typeof event.duration !== "undefined") {
|
||||||
dur=moment.duration(event.duration);
|
var dur = moment.duration(event.duration);
|
||||||
endDate = startDate.clone().add(dur);
|
endDate = startDate.clone().add(dur);
|
||||||
} else {
|
} else {
|
||||||
if (!isFacebookBirthday) {
|
if (!isFacebookBirthday) {
|
||||||
@ -176,8 +173,7 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
var addedEvents = 0;
|
var addedEvents = 0;
|
||||||
|
|
||||||
// can cause problems with e.g. birthdays before 1900
|
// can cause problems with e.g. birthdays before 1900
|
||||||
if(rule.options && rule.origOptions && rule.origOptions.dtstart && rule.origOptions.dtstart.getFullYear() < 1900 ||
|
if ((rule.options && rule.origOptions && rule.origOptions.dtstart && rule.origOptions.dtstart.getFullYear() < 1900) || (rule.options && rule.options.dtstart && rule.options.dtstart.getFullYear() < 1900)) {
|
||||||
rule.options && rule.options.dtstart && rule.options.dtstart.getFullYear() < 1900){
|
|
||||||
rule.origOptions.dtstart.setYear(1900);
|
rule.origOptions.dtstart.setYear(1900);
|
||||||
rule.options.dtstart.setYear(1900);
|
rule.options.dtstart.setYear(1900);
|
||||||
}
|
}
|
||||||
@ -200,17 +196,14 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
// because the logic below will filter out any recurrences that don"t actually belong within
|
// because the logic below will filter out any recurrences that don"t actually belong within
|
||||||
// our display range.
|
// our display range.
|
||||||
// Would be great if there was a better way to handle this.
|
// Would be great if there was a better way to handle this.
|
||||||
if (event.recurrences != undefined)
|
if (event.recurrences !== undefined) {
|
||||||
{
|
|
||||||
var pastMoment = moment(past);
|
var pastMoment = moment(past);
|
||||||
var futureMoment = moment(future);
|
var futureMoment = moment(future);
|
||||||
|
|
||||||
for (var r in event.recurrences)
|
for (var r in event.recurrences) {
|
||||||
{
|
|
||||||
// Only add dates that weren't already in the range we added from the rrule so that
|
// Only add dates that weren't already in the range we added from the rrule so that
|
||||||
// we don"t double-add those events.
|
// we don"t double-add those events.
|
||||||
if (moment(new Date(r)).isBetween(pastMoment, futureMoment) != true)
|
if (moment(new Date(r)).isBetween(pastMoment, futureMoment) !== true) {
|
||||||
{
|
|
||||||
dates.push(new Date(r));
|
dates.push(new Date(r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,22 +222,20 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
startDate = moment(date);
|
startDate = moment(date);
|
||||||
|
|
||||||
// For each date that we"re checking, it"s possible that there is a recurrence override for that one day.
|
// For each date that we"re checking, it"s possible that there is a recurrence override for that one day.
|
||||||
if ((curEvent.recurrences != undefined) && (curEvent.recurrences[dateKey] != undefined))
|
if (curEvent.recurrences !== undefined && curEvent.recurrences[dateKey] !== undefined) {
|
||||||
{
|
|
||||||
// We found an override, so for this recurrence, use a potentially different title, start date, and duration.
|
// We found an override, so for this recurrence, use a potentially different title, start date, and duration.
|
||||||
curEvent = curEvent.recurrences[dateKey];
|
curEvent = curEvent.recurrences[dateKey];
|
||||||
startDate = moment(curEvent.start);
|
startDate = moment(curEvent.start);
|
||||||
duration = parseInt(moment(curEvent.end).format("x")) - parseInt(startDate.format("x"));
|
duration = parseInt(moment(curEvent.end).format("x")) - parseInt(startDate.format("x"));
|
||||||
}
|
}
|
||||||
// If there"s no recurrence override, check for an exception date. Exception dates represent exceptions to the rule.
|
// If there"s no recurrence override, check for an exception date. Exception dates represent exceptions to the rule.
|
||||||
else if ((curEvent.exdate != undefined) && (curEvent.exdate[dateKey] != undefined))
|
else if (curEvent.exdate !== undefined && curEvent.exdate[dateKey] !== undefined) {
|
||||||
{
|
|
||||||
// This date is an exception date, which means we should skip it in the recurrence pattern.
|
// This date is an exception date, which means we should skip it in the recurrence pattern.
|
||||||
showRecurrence = false;
|
showRecurrence = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
endDate = moment(parseInt(startDate.format("x")) + duration, "x");
|
endDate = moment(parseInt(startDate.format("x")) + duration, "x");
|
||||||
if (startDate.format("x") == endDate.format("x")) {
|
if (startDate.format("x") === endDate.format("x")) {
|
||||||
endDate = endDate.endOf("day");
|
endDate = endDate.endOf("day");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,29 +268,28 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
}
|
}
|
||||||
// end recurring event parsing
|
// end recurring event parsing
|
||||||
} else {
|
} else {
|
||||||
// console.log("Single event ...");
|
|
||||||
// Single event.
|
// Single event.
|
||||||
var fullDayEvent = (isFacebookBirthday) ? true : isFullDayEvent(event);
|
var fullDayEvent = isFacebookBirthday ? true : isFullDayEvent(event);
|
||||||
|
|
||||||
if (includePastEvents) {
|
if (includePastEvents) {
|
||||||
|
// Past event is too far in the past, so skip.
|
||||||
if (endDate < past) {
|
if (endDate < past) {
|
||||||
//console.log("Past event is too far in the past. So skip: " + title);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// It's not a fullday event, and it is in the past, so skip.
|
||||||
if (!fullDayEvent && endDate < new Date()) {
|
if (!fullDayEvent && endDate < new Date()) {
|
||||||
//console.log("It's not a fullday event, and it is in the past. So skip: " + title);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It's a fullday event, and it is before today, So skip.
|
||||||
if (fullDayEvent && endDate <= today) {
|
if (fullDayEvent && endDate <= today) {
|
||||||
//console.log("It's a fullday event, and it is before today. So skip: " + title);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It exceeds the maximumNumberOfDays limit, so skip.
|
||||||
if (startDate > future) {
|
if (startDate > future) {
|
||||||
//console.log("It exceeds the maximumNumberOfDays limit. So skip: " + title);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,13 +297,12 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjust start date so multiple day events will be displayed as happening today even though they started some days ago already
|
// Adjust start date so multiple day events will be displayed as happening today even though they started some days ago already
|
||||||
if (fullDayEvent && startDate <= today) {
|
if (fullDayEvent && startDate <= today) {
|
||||||
startDate = moment(today);
|
startDate = moment(today);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Every thing is good. Add it to the list.
|
// Every thing is good. Add it to the list.
|
||||||
|
|
||||||
newEvents.push({
|
newEvents.push({
|
||||||
title: title,
|
title: title,
|
||||||
startDate: startDate.format("x"),
|
startDate: startDate.format("x"),
|
||||||
@ -324,7 +313,6 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
geo: geo,
|
geo: geo,
|
||||||
description: description
|
description: description
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -333,7 +321,6 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
return a.startDate - b.startDate;
|
return a.startDate - b.startDate;
|
||||||
});
|
});
|
||||||
|
|
||||||
//console.log(newEvents);
|
|
||||||
events = newEvents;
|
events = newEvents;
|
||||||
|
|
||||||
self.broadcastEvents();
|
self.broadcastEvents();
|
||||||
@ -345,7 +332,6 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
* Schedule the timer for the next update.
|
* Schedule the timer for the next update.
|
||||||
*/
|
*/
|
||||||
var scheduleTimer = function () {
|
var scheduleTimer = function () {
|
||||||
//console.log('Schedule update timer.');
|
|
||||||
clearTimeout(reloadTimer);
|
clearTimeout(reloadTimer);
|
||||||
reloadTimer = setTimeout(function () {
|
reloadTimer = setTimeout(function () {
|
||||||
fetchCalendar();
|
fetchCalendar();
|
||||||
@ -367,7 +353,7 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
var start = event.start || 0;
|
var start = event.start || 0;
|
||||||
var startDate = new Date(start);
|
var startDate = new Date(start);
|
||||||
var end = event.end || 0;
|
var end = event.end || 0;
|
||||||
if (((end - start) % (24 * 60 * 60 * 1000)) === 0 && startDate.getHours() === 0 && startDate.getMinutes() === 0) {
|
if ((end - start) % (24 * 60 * 60 * 1000) === 0 && startDate.getHours() === 0 && startDate.getMinutes() === 0) {
|
||||||
// Is 24 hours, and starts on the middle of the night.
|
// Is 24 hours, and starts on the middle of the night.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -407,7 +393,7 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
var getTitleFromEvent = function (event) {
|
var getTitleFromEvent = function (event) {
|
||||||
var title = "Event";
|
var title = "Event";
|
||||||
if (event.summary) {
|
if (event.summary) {
|
||||||
title = (typeof event.summary.val !== "undefined") ? event.summary.val : event.summary;
|
title = typeof event.summary.val !== "undefined" ? event.summary.val : event.summary;
|
||||||
} else if (event.description) {
|
} else if (event.description) {
|
||||||
title = event.description;
|
title = event.description;
|
||||||
}
|
}
|
||||||
@ -444,7 +430,7 @@ var CalendarFetcher = function(url, reloadInterval, excludedEvents, maximumNumbe
|
|||||||
* Broadcast the existing events.
|
* Broadcast the existing events.
|
||||||
*/
|
*/
|
||||||
this.broadcastEvents = function () {
|
this.broadcastEvents = function () {
|
||||||
//console.log('Broadcasting ' + events.length + ' events.');
|
Log.info("Calendar-Fetcher: Broadcasting " + events.length + " events.");
|
||||||
eventsReceivedCallback(self);
|
eventsReceivedCallback(self);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,30 +1,26 @@
|
|||||||
/* Magic Mirror
|
/* Magic Mirror
|
||||||
* Node Helper: Calendar
|
* Node Helper: Calendar
|
||||||
*
|
*
|
||||||
* By Michael Teeuw http://michaelteeuw.nl
|
* By Michael Teeuw https://michaelteeuw.nl
|
||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var NodeHelper = require("node_helper");
|
const NodeHelper = require("node_helper");
|
||||||
var validUrl = require("valid-url");
|
const validUrl = require("valid-url");
|
||||||
var CalendarFetcher = require("./calendarfetcher.js");
|
const CalendarFetcher = require("./calendarfetcher.js");
|
||||||
|
const Log = require("../../../js/logger");
|
||||||
|
|
||||||
module.exports = NodeHelper.create({
|
module.exports = NodeHelper.create({
|
||||||
// Override start method.
|
// Override start method.
|
||||||
start: function () {
|
start: function () {
|
||||||
var events = [];
|
Log.log("Starting node helper for: " + this.name);
|
||||||
|
|
||||||
this.fetchers = [];
|
this.fetchers = [];
|
||||||
|
|
||||||
console.log("Starting node helper for: " + this.name);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Override socketNotificationReceived method.
|
// Override socketNotificationReceived method.
|
||||||
socketNotificationReceived: function (notification, payload) {
|
socketNotificationReceived: function (notification, payload) {
|
||||||
if (notification === "ADD_CALENDAR") {
|
if (notification === "ADD_CALENDAR") {
|
||||||
//console.log('ADD_CALENDAR: ');
|
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.id);
|
||||||
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -35,42 +31,40 @@ module.exports = NodeHelper.create({
|
|||||||
* attribute url string - URL of the news feed.
|
* attribute url string - URL of the news feed.
|
||||||
* attribute reloadInterval number - Reload interval in milliseconds.
|
* attribute reloadInterval number - Reload interval in milliseconds.
|
||||||
*/
|
*/
|
||||||
|
createFetcher: function (url, fetchInterval, excludedEvents, maximumNumberOfDays, auth, broadcastPastEvents, identifier) {
|
||||||
createFetcher: function(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents) {
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (!validUrl.isUri(url)) {
|
if (!validUrl.isUri(url)) {
|
||||||
self.sendSocketNotification("INCORRECT_URL", {url: url});
|
self.sendSocketNotification("INCORRECT_URL", { id: identifier, url: url });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fetcher;
|
var fetcher;
|
||||||
if (typeof self.fetchers[url] === "undefined") {
|
if (typeof self.fetchers[identifier + url] === "undefined") {
|
||||||
console.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
|
Log.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
|
||||||
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumNumberOfDays, auth, broadcastPastEvents);
|
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumNumberOfDays, auth, broadcastPastEvents);
|
||||||
|
|
||||||
fetcher.onReceive(function (fetcher) {
|
fetcher.onReceive(function (fetcher) {
|
||||||
//console.log('Broadcast events.');
|
|
||||||
//console.log(fetcher.events());
|
|
||||||
|
|
||||||
self.sendSocketNotification("CALENDAR_EVENTS", {
|
self.sendSocketNotification("CALENDAR_EVENTS", {
|
||||||
|
id: identifier,
|
||||||
url: fetcher.url(),
|
url: fetcher.url(),
|
||||||
events: fetcher.events()
|
events: fetcher.events()
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
fetcher.onError(function (fetcher, error) {
|
fetcher.onError(function (fetcher, error) {
|
||||||
console.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
|
Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
|
||||||
self.sendSocketNotification("FETCH_ERROR", {
|
self.sendSocketNotification("FETCH_ERROR", {
|
||||||
|
id: identifier,
|
||||||
url: fetcher.url(),
|
url: fetcher.url(),
|
||||||
error: error
|
error: error
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
self.fetchers[url] = fetcher;
|
self.fetchers[identifier + url] = fetcher;
|
||||||
} else {
|
} else {
|
||||||
//console.log('Use existing news fetcher for url: ' + url);
|
Log.log("Use existing calendar fetcher for url: " + url);
|
||||||
fetcher = self.fetchers[url];
|
fetcher = self.fetchers[identifier + url];
|
||||||
fetcher.broadcastEvents();
|
fetcher.broadcastEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user