mirror of
https://gitlab.silvrtree.co.uk/martind2000/old-silvrgit.git
synced 2025-01-10 21:05:09 +00:00
185 lines
6.0 KiB
JavaScript
185 lines
6.0 KiB
JavaScript
var request = require('request');
|
|
var log4js = require('log4js');
|
|
var logger = log4js.getLogger();
|
|
var STRING = require('string');
|
|
var util = require('util');
|
|
var Elapsed = require('elapsed');
|
|
require('sugar-date');
|
|
|
|
function processICAL(ical) {
|
|
"use strict";
|
|
logger.info('+ processICAL');
|
|
var workingBlock = [];
|
|
var segments = {
|
|
meetingStartID: "DTSTART;TZID=Europe/London:",
|
|
meetingStartAlt: 'DTSTART:',
|
|
meetingEndID: "DTEND;TZID=Europe/London:",
|
|
meetingEndAlt: 'DTEND:',
|
|
meetingDescID: "DESCRIPTION:",
|
|
summaryID: 'SUMMARY:',
|
|
begin: 'BEGIN:VEVENT',
|
|
end: 'END:VEVENT',
|
|
beginAlarm: 'BEGIN:VALARM',
|
|
recur : 'RRULE'
|
|
};
|
|
|
|
function processBlock(block) {
|
|
var workBlock = {summary: '', dtstart: null, dtend: null, description: '', timeStart : null, timeEnd : null, duration:0, combined:''};
|
|
var alarmFlag = false, ws, blockStep;
|
|
for (var step = 0; step < block.length; step++) {
|
|
blockStep = block[step];
|
|
if (blockStep.indexOf(segments.summaryID) >= 0) {
|
|
workBlock.summary = STRING(block[step].split(segments.summaryID)[1]).collapseWhitespace().s;
|
|
}
|
|
if (blockStep.indexOf(segments.meetingStartID) >= 0) {
|
|
ws = STRING(block[step].split(segments.meetingStartID)[1]).collapseWhitespace().s;
|
|
workBlock.dtstart = Date.create(ws);
|
|
}
|
|
if (blockStep.indexOf(segments.meetingEndID) >= 0) {
|
|
ws = STRING(block[step].split(segments.meetingEndID)[1]).collapseWhitespace().s;
|
|
workBlock.dtend = Date.create(ws);
|
|
}
|
|
if (blockStep.indexOf(segments.meetingStartAlt) >= 0) {
|
|
ws = STRING(block[step].split(segments.meetingStartAlt)[1]).collapseWhitespace().s;
|
|
workBlock.dtstart = Date.create(ws);
|
|
}
|
|
if (blockStep.indexOf(segments.meetingEndAlt) >= 0) {
|
|
ws = STRING(block[step].split(segments.meetingEndAlt)[1]).collapseWhitespace().s;
|
|
workBlock.dtend = Date.create(ws);
|
|
}
|
|
if (blockStep.indexOf(segments.meetingDescID) >= 0) {
|
|
if (!alarmFlag) {
|
|
workBlock.description = STRING(block[step].split(segments.meetingDescID)[1]).collapseWhitespace().s;
|
|
}
|
|
}
|
|
if (blockStep.indexOf(segments.beginAlarm) >= 0) {
|
|
alarmFlag = true;
|
|
}
|
|
}
|
|
|
|
if (workBlock.dtstart !== null ){
|
|
workBlock.timeStart = workBlock.dtstart.format('{12hr}:{mm}:{ss} {tt}');
|
|
workBlock.combined = '<em>' + workBlock.timeStart + '</em> - ';
|
|
}
|
|
workBlock.combined = workBlock.combined + workBlock.summary;
|
|
if (workBlock.dtend !== null ){
|
|
workBlock.timeEnd = workBlock.dtend.format('{12hr}:{mm}:{ss} {tt}');
|
|
}
|
|
if (workBlock.dtstart !== null && workBlock.dtend !== null) {
|
|
var elapsedTime = new Elapsed(workBlock.dtstart, workBlock.dtend);
|
|
workBlock.duration = elapsedTime.optimal;
|
|
workBlock.combined = workBlock.combined + ', ' + elapsedTime.optimal;
|
|
}
|
|
|
|
|
|
|
|
return workBlock;
|
|
}
|
|
|
|
var lines = ical.split('\r\n'), l = lines.length, counter = 0;
|
|
|
|
while (counter < l) {
|
|
if (lines[counter].indexOf(segments.begin) < 0) {
|
|
counter++;
|
|
} else {
|
|
var subcounter = 0, subBlock = [];
|
|
while (subcounter < 75) {
|
|
if (lines[counter + subcounter].indexOf(segments.end) < 0) {
|
|
subBlock.push(lines[counter + subcounter]);
|
|
subcounter++;
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
counter = counter + subcounter;
|
|
var b = processBlock(subBlock);
|
|
if (b.dtstart !== null) {
|
|
workingBlock.push(b);
|
|
}
|
|
|
|
}
|
|
}
|
|
logger.info('- processICAL');
|
|
// if (workingBlock.dtstart == null) return {};
|
|
|
|
return workingBlock;
|
|
}
|
|
|
|
module.exports = {
|
|
jsonBlock: [],
|
|
getTodaysSimple: function () {
|
|
"use strict";
|
|
logger.info('+ getTodaysSimple');
|
|
var today = {
|
|
entries: []
|
|
};
|
|
|
|
for (var t = 0; t < this.jsonBlock.length; t++) {
|
|
if (this.jsonBlock[t].dtstart.isToday()) {
|
|
|
|
today.entries.push(this.jsonBlock[t]);
|
|
}
|
|
}
|
|
// logger.debug(today);
|
|
logger.info('- getTodaysSimple');
|
|
return today;
|
|
},
|
|
getTodaysMeetings: function () {
|
|
"use strict";
|
|
logger.info('+ getTodaysMeetings');
|
|
var today = {
|
|
previous: [], upcoming: [], current: {}
|
|
};
|
|
var now = new Date();
|
|
|
|
for (var t = 0; t < this.jsonBlock.length; t++) {
|
|
if (this.jsonBlock[t].dtstart.isToday()) {
|
|
|
|
if (this.jsonBlock[t].dtstart.isAfter(now)) {
|
|
today.upcoming.push(this.jsonBlock[t]);
|
|
}
|
|
else {
|
|
today.previous.push(this.jsonBlock[t]);
|
|
}
|
|
|
|
if (now.isBetween(this.jsonBlock[t].dtstart, this.jsonBlock[t].dtend)) {
|
|
today.current = this.jsonBlock[t];
|
|
}
|
|
}
|
|
}
|
|
// logger.debug(today);
|
|
logger.info('- getTodaysMeetings');
|
|
return today;
|
|
}, getSimpleCalV2: function (url, cb) {
|
|
"use strict";
|
|
var self = this;
|
|
|
|
// var calJson = [];
|
|
|
|
request(url, function (err, res, body) {
|
|
if (err) {
|
|
logger.error('Get remote Calendar Request failed');
|
|
// callback.call(null, new Error('Request failed'));
|
|
return;
|
|
}
|
|
|
|
self.jsonBlock = processICAL(body);
|
|
|
|
// logger.debug(self.jsonBlock);
|
|
var st = self.getTodaysSimple();
|
|
|
|
if (typeof cb === 'function') {
|
|
cb(st);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Created by Martin on 16/02/2016.
|
|
*/
|
|
};
|