updated .gitignore file

This commit is contained in:
martind2000 2016-03-31 23:00:47 +01:00
parent f7b9642aab
commit 2057c171a0
4 changed files with 549 additions and 632 deletions

View File

@ -5,6 +5,7 @@
"window",
"-Promise"
],
"node":true,
"browser": true,
"boss": true,
"curly": true,

View File

@ -1,15 +1,11 @@
/**
* Created by marti on 30/01/2016.
*/
var http = require('http'), request = require('request'), cheerio = require(
'cheerio'), util = require('util'), UltraSES = require(
var http = require('http'), request = require('request'), cheerio = require('cheerio'), util = require('util'), UltraSES = require(
'ultrases'), cron = require('node-cron');
var jade = require('jade'), _ = require('lodash'), dateFormat = require(
'dateformat');
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
'string');
var jade = require('jade'), _ = require('lodash'), dateFormat = require('dateformat');
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require('string');
var nano = require('nano')('http://localhost:5984');
var log4js = require('log4js');
var logger = log4js.getLogger();
var calHandler = require('./today/calHandler');
@ -17,44 +13,32 @@ var swedishWord = require('./today/swedishword');
var weather = require('./today/weather');
var trains = require('./today/trains');
var history = require('./today/history');
var db_name = 'silvrgit';
var dbCouch = nano.use(db_name);
var todayCache = {
last: 0, data: {
trains: {last: 0, data: []},
weather: {},
history: [],
today: '',
tv: {entries: []},
cal: {entries: []},
swedish: {}
trains: {last: 0, data: []}, weather: {}, history: [], today: '', tv: {entries: []}, cal: {entries: []}, swedish: {}
}, expire: ((60 * 1000) * 60)
};
var mailer = new UltraSES({
aws: {
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
secretAccessKey: '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',
"region": "eu-west-1"
region: 'eu-west-1'
}, defaults: {
from: 'Martin Donnelly <martind2000@gmail.com>'
}
});
var file = __dirname + '/' + 'newdata.json';
var htmlfile = __dirname + '/' + 'today.html';
function saveData() {
logger.info('Saving...');
jsonfile.writeFileSync(file, todayCache);
}
function saveToDB(data) {
logger.debug('Inserting into couch...');
// logger.info(util.inspect(obj));
dbCouch.insert(data, function (err, body, header) {
// Logger.info(util.inspect(obj));
dbCouch.insert(data, function(err, body, header) {
if (err) {
logger.error('Error inserting into couch');
logger.error(err);
@ -62,21 +46,10 @@ function saveToDB(data) {
}
});
}
function nth(d) {
if (d > 3 && d < 21) return 'th'; // thanks kennebec
switch (d % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
if (d > 3 && d < 21) {return 'th';} // Thanks kennebec
if (d % 10 === 1) {return 'st';} else if (d % 10 === 2) {return 'nd';} else if (d % 10 === 3) {return 'rd';} else {return 'th';}
}
function dayNumber() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
@ -84,90 +57,75 @@ function dayNumber() {
var oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
function breakDay() {
var now = new Date();
return {year: now.getFullYear(), month: parseInt(now.getMonth()) + 1, day: now.getDate()}
}
/**
* @return {number}
*/
function DayDiff(CurrentDate) {
var TYear = CurrentDate.getFullYear();
var TDay = new Date("January, 01, " + (parseInt(TYear) + 1));
var TDay = new Date('January, 01, ' + (parseInt(TYear) + 1));
TDay.getFullYear(TYear);
var DayCount = (TDay - CurrentDate) / (1000 * 60 * 60 * 24);
DayCount = Math.round(DayCount);
return (DayCount);
}
Array.prototype.indexOfOld = Array.prototype.indexOf;
Array.prototype.indexOf = function (e, fn) {
Array.prototype.indexOf = function(e, fn) {
if (!fn) {
return this.indexOfOld(e)
}
else {
return this.indexOfOld(e);
} else {
if (typeof fn === 'string') {
var att = fn;
fn = function (e) {
fn = function(e) {
return e[att];
}
}
return this.map(fn).indexOfOld(e);
}
};
module.exports = {
getClock: function (req, res) {
// console.log(todayCache);
getClock: function(req, res) {
// Console.log(todayCache);
res.render('pages/clock', todayCache);
}, getToday: function (req, res) {
}, getToday: function(req, res) {
logger.info(todayCache);
res.render('pages/today', todayCache);
}, getData: function (req, res) {
}, getData: function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(todayCache));
}, getTodayDate: function () {
}, getTodayDate: function() {
var s, d = new Date();
todayCache.data.history = [];
s = '<strong>' + dateFormat(d, "mmmm d") + '</strong> - ';
s = '<strong>' + dateFormat(d, 'mmmm d') + '</strong> - ';
s = s + 'The ' + dayNumber() + nth(dayNumber) + ' day of ' + dateFormat(d,
"yyyy") + ', and there are ' + DayDiff(d) + ' days left until the end of the year.';
'yyyy') + ', and there are ' + DayDiff(d) + ' days left until the end of the year.';
logger.debug(s);
todayCache.data.today = s;
},
refreshTrainAndWeather: function () {
}, refreshTrainAndWeather: function() {
weather.newDoGetWeather()
.then((d)=> {
.then((d) => {
todayCache.data.weather = d;
})
.catch((e) => {
}).catch((e) => {
logger.error(e);
});
trains.updateTrains()
.then((d) => {
"use strict";
'use strict';
console.log('Trains: ', d);
todayCache.data.trains.data = d;
todayCache.data.trains.last = new Date();
})
.catch((e)=> {
"use strict";
'use strict';
console.error(e);
});
}, preLoadToday: function () {
}, preLoadToday: function() {
module.exports.getTodayDate();
var self = this;
todayCache.data.cal.entries = [];
weather.newDoGetWeather()
.then((d)=> {
todayCache.data.weather = d;
@ -175,171 +133,132 @@ module.exports = {
.catch((e) => {
logger.error(e);
});
trains.updateTrains()
.then((d) => {
"use strict";
'use strict';
console.log('Trains: ', d);
todayCache.data.trains.data = d;
todayCache.data.trains.last = new Date();
})
.catch((e)=> {
"use strict";
'use strict';
console.error(e);
});
history.updateHistory()
.then((d) => {
"use strict";
console.log('History result: ' , d);
'use strict';
console.log('History result: ', d);
todayCache.data.history = d;
})
.catch((e)=>{
"use strict";
.catch((e)=> {
'use strict';
console.error(e);
});
calHandler.getSimpleCalV3('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc')
.then((d)=>{
"use strict";
.then((d) => {
'use strict';
todayCache.data.tv = d;
})
.catch(e)=>{
.catch((e) => {
'use strict';
logger.error(e);
}
});
calHandler.getSimpleCalV3(
'https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics'
)
.then((d)=>{
"use strict";
'https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics')
.then((d) => {
'use strict';
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
})
.catch(e)=>{
.catch((e) => {
'use strict';
logger.error(e);
}
});
calHandler.getSimpleCalV3(
'https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics'
)
.then((d)=>{
"use strict";
'https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics')
.then((d) => {
'use strict';
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
})
.catch(e)=>{
.catch((e) => {
'use strict';
logger.error(e);
}
});
calHandler.getSimpleCalV3(
'https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics'
)
.then((d)=>{
"use strict";
'https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics')
.then((d) => {
'use strict';
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
})
.catch(e)=>{
.catch((e) => {
'use strict';
logger.error(e);
}
});
swedishWord.getSwedishWord()
.then((d) => {
"use strict";
console.log('Swedish result: ' , d);
todayCache.data.swedish =d;
'use strict';
console.log('Swedish result: ', d);
todayCache.data.swedish = d;
})
.catch((e)=>{
"use strict";
.catch((e)=> {
'use strict';
console.error(e);
});
todayCache.date = breakDay();
// word of the day http://wotd.transparent.com/rss/swedish-widget.xml?t=1455840000000
// time stamp
}
};
function sendEmailV1() {
var now = new Date();
var email = {
to: 'martind2000@gmail.com',
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
to: 'martind2000@gmail.com', subject: 'Today - ' + dateFormat(now, 'dddd, mmmm dS, yyyy')
};
var template = {
file: __dirname + '/' + 'jade/today.jade',
locals: todayCache
file: __dirname + '/' + 'jade/today.jade', locals: todayCache
};
logger.debug(__dirname);
logger.debug(__dirname.substr(__dirname.lastIndexOf('/'), __dirname.length));
//if (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length))
mailer.sendTemplate(email, template, function (err) {
// If (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length))
mailer.sendTemplate(email, template, function(err) {
if (err) throw err;
logger.info('compiled template email sent');
});
// saveData();
// SaveData();
var fn = jade.compileFile(template.file);
//console.log(fn(todayCache));
// fs.writeFileSync(htmlfile, fn(todayCache));
// Console.log(fn(todayCache));
// Fs.writeFileSync(htmlfile, fn(todayCache));
}
function sendEmail() {
logger.log('Simple email');
var now = new Date();
var email = {
to: 'martind2000@gmail.com',
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
to: 'martind2000@gmail.com', 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 () {
setTimeout(function() {
module.exports.preLoadToday();
}, 5000);
setTimeout(function () {
setTimeout(function() {
saveToDB(todayCache);
}, 45000);
cron.schedule('45 6 * * *', function () {
cron.schedule('45 6 * * *', function() {
module.exports.preLoadToday();
return -1;
});
cron.schedule('0 */1 * * *', function () {
cron.schedule('0 */1 * * *', function() {
module.exports.refreshTrainAndWeather();
return -1;
});
cron.schedule('0 7 * * *', function () {
cron.schedule('0 7 * * *', function() {
sendEmailV1();
saveToDB(todayCache);
// console.log('tick');
// Console.log('tick');
return -1;
});

View File

@ -7,15 +7,15 @@ var Elapsed = require('elapsed');
require('sugar-date');
function processICAL(ical) {
"use strict";
'use strict';
logger.info('+ processICAL');
var workingBlock = [];
var segments = {
meetingStartID: "DTSTART;TZID=Europe/London:",
meetingStartID: 'DTSTART;TZID=Europe/London:',
meetingStartAlt: 'DTSTART:',
meetingEndID: "DTEND;TZID=Europe/London:",
meetingEndID: 'DTEND;TZID=Europe/London:',
meetingEndAlt: 'DTEND:',
meetingDescID: "DESCRIPTION:",
meetingDescID: 'DESCRIPTION:',
summaryID: 'SUMMARY:',
begin: 'BEGIN:VEVENT',
end: 'END:VEVENT',
@ -94,8 +94,7 @@ function processICAL(ical) {
if (lines[counter + subcounter].indexOf(segments.end) < 0) {
subBlock.push(lines[counter + subcounter]);
subcounter++;
}
else {
} else {
break;
}
}
@ -108,15 +107,15 @@ function processICAL(ical) {
}
}
logger.info('- processICAL');
// if (workingBlock.dtstart == null) return {};
// If (workingBlock.dtstart == null) return {};
return workingBlock;
}
module.exports = {
jsonBlock: [],
getTodaysSimple: function () {
"use strict";
getTodaysSimple: function() {
'use strict';
logger.info('+ getTodaysSimple');
var today = {
entries: []
@ -128,12 +127,12 @@ module.exports = {
today.entries.push(this.jsonBlock[t]);
}
}
// logger.debug(today);
// Logger.debug(today);
logger.info('- getTodaysSimple');
return today;
},
getTodaysMeetings: function () {
"use strict";
getTodaysMeetings: function() {
'use strict';
logger.info('+ getTodaysMeetings');
var today = {
previous: [], upcoming: [], current: {}
@ -145,8 +144,7 @@ module.exports = {
if (this.jsonBlock[t].dtstart.isAfter(now)) {
today.upcoming.push(this.jsonBlock[t]);
}
else {
} else {
today.previous.push(this.jsonBlock[t]);
}
@ -155,31 +153,31 @@ module.exports = {
}
}
}
// logger.debug(today);
// Logger.debug(today);
logger.info('- getTodaysMeetings');
return today;
}, getSimpleCalV2: function (url, cb) {
"use strict";
}, getSimpleCalV2: function(url, cb) {
'use strict';
var self = this;
// var calJson = [];
// Var calJson = [];
try {
request(url, function (err, res, body) {
request(url, function(err, res, body) {
if (err) {
logger.error('Get remote Calendar Request failed');
// callback.call(null, new Error('Request failed'));
// Callback.call(null, new Error('Request failed'));
return;
}
self.jsonBlock = processICAL(body);
// logger.debug(self.jsonBlock);
// Logger.debug(self.jsonBlock);
var st = self.getTodaysSimple();
if (typeof cb === 'function') {
cb(st);
}
}, function (error, response, body) {
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
@ -189,26 +187,26 @@ module.exports = {
console.log(e);
}
},getSimpleCalV3: function (url) {
"use strict";
}, getSimpleCalV3: function(url) {
'use strict';
var self = this;
return new Promise(function(resolve, reject) {
try {
request(url, function (err, res, body) {
request(url, function(err, res, body) {
if (err) {
// logger.error(err);
// Logger.error(err);
return reject(err);
// throw err;
// Throw err;
}
self.jsonBlock = processICAL(body);
// logger.debug(self.jsonBlock);
// Logger.debug(self.jsonBlock);
var st = self.getTodaysSimple();
return resolve(st);
}, function (error, response, body) {
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
@ -222,7 +220,7 @@ module.exports = {
});
// var calJson = [];
// Var calJson = [];
}

View File

@ -1,25 +1,27 @@
/**
* Created by Martin on 31/03/2016.
*/
var request = require('request'), cheerio = require('cheerio')
var request = require('request');
var cheerio = require('cheerio');
var STRING = require('string');
var logger = require('log4js').getLogger();
module.exports = {
getTechHistory: function () {
var url, d, day, month, monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
getTechHistory: function() {
var url;
var d;
var day;
var month;
var monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
d = new Date();
@ -30,30 +32,28 @@ module.exports = {
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
logger.debug(url);
return new Promise(function (resolve, reject) {
"use strict";
return new Promise(function(resolve, reject) {
'use strict';
request(url, function (err, resp, body) {
request(url, function(err, resp, body) {
if (err) {
// logger.error(err);
// Logger.error(err);
return reject(err);
// throw err;
// Throw err;
}
$ = cheerio.load(body);
var $ = cheerio.load(body);
var tdihbody = $('#tdihbody');
var output = [];
tdihbody.find('.tdihevent > p').each(function (div) {
tdihbody.find('.tdihevent > p').each(function(div) {
var s = $(this).text();
output.push(STRING(s).collapseWhitespace().s);
});
//todayCache.data.history = todayCache.data.history.concat(output);
// logger.info(todayCache.data.history);
return resolve(output);
}, function (error, response, body) {
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
@ -62,24 +62,26 @@ module.exports = {
});
});
}, getHistory: function () {
var url, d, day, month, monthNames = [
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december"
}, getHistory: function() {
var url;
var d = new Date();
var day;
var month;
var monthNames = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
];
d = new Date();
month = monthNames[d.getMonth()];
day = d.getDate();
@ -91,21 +93,21 @@ module.exports = {
logger.debug(url);
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
request(url, function (err, resp, body) {
request(url, function(err, resp, body) {
if (err) {
// logger.error(err);
// Logger.error(err);
return reject(err);
// throw err;
// Throw err;
}
$ = cheerio.load(body);
var $ = cheerio.load(body);
var body = $('DIV#bbcPageContent').first();
var nbody = $('DIV#bbcPageContent').first();
var output = [];
body.find('.story > p').each(function (div) {
nbody.find('.story > p').each(function(div) {
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
@ -114,11 +116,8 @@ module.exports = {
});
return resolve(output);
// todayCache.data.history = todayCache.data.history.concat(output);
// logger.info(todayCache.data.history);
// module.getTechHistory();
}, function (error, response, body) {
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
@ -129,10 +128,10 @@ module.exports = {
});
},
updateHistory: function () {
"use strict";
updateHistory: function() {
'use strict';
var output = [];
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
module.exports.getHistory()
.then((d)=> {