From 658bbea5a461bcaafb3ba45ff47cbcfc7787b037 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Tue, 16 Feb 2016 16:16:36 +0000 Subject: [PATCH] added string and use it to fixe white space and missing .'s --- lib/calHandler.js | 190 ++++++++++++++++++++++++++++++++++++++++++++ lib/caltest.js | 58 ++++++++++++++ lib/jade/today.jade | 14 +++- lib/today.js | 35 +++++++- package.json | 3 + 5 files changed, 294 insertions(+), 6 deletions(-) create mode 100644 lib/calHandler.js create mode 100644 lib/caltest.js diff --git a/lib/calHandler.js b/lib/calHandler.js new file mode 100644 index 0000000..bd2a142 --- /dev/null +++ b/lib/calHandler.js @@ -0,0 +1,190 @@ +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' + }; + + 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 = '' + workBlock.timeStart + ' - '; + } + 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('+ getTodaysMeetings'); + 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('- getTodaysMeetings'); + 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. + */ +}; + +/*module.exports.getSimpleCalV2('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc', function (v) { + console.log(v); +});*/ + +// https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics +// http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc diff --git a/lib/caltest.js b/lib/caltest.js new file mode 100644 index 0000000..3739495 --- /dev/null +++ b/lib/caltest.js @@ -0,0 +1,58 @@ +/** + * Created by Martin on 15/02/2016. + */ +var http = require('http'), request = require('request'), ical2json = require('ical2json'), util = require('util'); +var jsonfile = require('jsonfile'); +var log4js = require('log4js'); +var logger = log4js.getLogger(); + +require('sugar-date'); + +var file = __dirname + '/' + 'cal.json'; + +function saveData(v) { + jsonfile.writeFileSync(file, v); +} + +module.exports = { + getCal: function () { + + var url = 'https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics'; + + request(url, function (err, resp, body) { + if (err) + throw err; + + var output = ical2json.convert(body); + + // saveData(output); + + for (var i = 0; i < output.VCALENDAR[0].VEVENT.length; i++) { + var item = output.VCALENDAR[0].VEVENT[i]; + var d; + if (item.hasOwnProperty('DTSTART')) { + + d = Date.create(item.DTSTART); + + } + else { + + d = Date.create(item['DTSTART;VALUE=DATE']); + + } + + + if (d.getFullYear() == 2016) { + logger.debug(item); + //console.log(item['SUMMARY']); + } + } + + // console.log(util.inspect(output)); + // output.VCALENDAR.VEVENT[1]; + }); + + } +}; + +module.exports.getCal(); \ No newline at end of file diff --git a/lib/jade/today.jade b/lib/jade/today.jade index c93f98d..c094fa2 100644 --- a/lib/jade/today.jade +++ b/lib/jade/today.jade @@ -6,7 +6,7 @@ html(lang="en") body h1 Today .stuff - | !{data.today} + | !{data.today} .weather h2 Weather p Currently: @@ -30,7 +30,11 @@ html(lang="en") p= alert.description else p Nothing to report. - + .calendar + if data.cal.entries.length > 0 + h2 Calendar + each line in data.tv.entries + p !{line.combined} .history h2 Today in history if data.history.length > 0 @@ -39,5 +43,11 @@ html(lang="en") else p Nothing of note happened today. + .tv + if data.tv.entries.length > 0 + h2 Todays TV + each line in data.tv.entries + p !{line.combined} + \ No newline at end of file diff --git a/lib/today.js b/lib/today.js index 573152f..459cad0 100644 --- a/lib/today.js +++ b/lib/today.js @@ -6,6 +6,7 @@ var jade = require('jade'), _ = require('lodash'), dateFormat = require('datefor var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require('string'); var log4js = require('log4js'); var logger = log4js.getLogger(); +var calHandler = require('./calHandler'); var todayCache = { last: 0, @@ -13,7 +14,9 @@ var todayCache = { trains: {last: 0, data: []}, weather: {}, history: [], - today: '' + today: '', + tv:{entries:[]}, + cal:{entries:[]} }, expire: ((60 * 1000) * 60) }; @@ -40,10 +43,11 @@ var mailer = new UltraSES({ } }); -var file = __dirname + '/' + 'data.json'; +var file = __dirname + '/' + 'newdata.json'; var htmlfile = __dirname + '/' + 'today.html'; function saveData() { + logger.info('Saving...'); jsonfile.writeFileSync(file, todayCache); } @@ -267,6 +271,7 @@ module.exports = { }, preLoadToday: function () { module.exports.getTodayDate(); + var self = this; try { module.exports.doGetWeatherOutlook(); @@ -291,6 +296,24 @@ module.exports = { // statements to handle any exceptions logger.error(e); } + + try { + calHandler.getSimpleCalV2('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc', function(v) { + todayCache.data.tv = v; + }); + } + catch (e) { + logger.error(e); + } + + try { + calHandler.getSimpleCalV2('https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics', function(v) { + todayCache.data.cal = v; + }); + } + catch (e) { + logger.error(e); + } } } ; @@ -310,11 +333,13 @@ function sendEmailV1() { logger.debug(__dirname.substr(__dirname.lastIndexOf('/'), __dirname.length)); //if (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length)) - mailer.sendTemplate(email, template, function (err) { + + mailer.sendTemplate(email, template, function (err) { if (err) throw err; console.log('compiled template email sent'); }); + //saveData(); var fn = jade.compileFile(template.file); console.log(fn(todayCache)); @@ -332,11 +357,13 @@ function sendEmail() { subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy") }; - mailer.sendText(email, 'Look at this fantastic email body!', function (err) { + /* mailer.sendText(email, 'Look at this fantastic email body!', function (err) { if (err) throw err; console.log('email sent!'); }); +*/ + saveData(); } setTimeout(function () { diff --git a/package.json b/package.json index 967bcdc..b309259 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,13 @@ "wordsoap": "^0.2.0" }, "dependencies": { + "elapsed": "0.0.7", "express": "3.x", + "ical2json": "^0.2.0", "node-cron": "^1.0.0", "scrape": "^0.2.3", "string": "^3.3.1", + "sugar-date": "^1.5.1", "ultrases": "^0.1.3", "unstyler": "^0.2.2" },