added haymarket train and updated to some es6 stuff

This commit is contained in:
Martin Donnelly 2017-01-03 12:05:02 +00:00
parent 7c5d42b74b
commit 457473dded
3 changed files with 94 additions and 90 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,22 +1,24 @@
/**
* Created by marti on 30/01/2016.
*/
var http = require('http'), request = require('request'), cheerio = require(
'cheerio'), util = require('util'), cron = require('node-cron');
var dateFormat = require('dateformat');
var jsonfile = require('jsonfile'), fs = require('fs');
* Created by marti on 30/01/2016.
*/
let http = require('http'), request = require('request'), cheerio = require(
'cheerio'), util = require('util');
const cron = require('node-cron');
const dateFormat = require('dateformat');
const jsonfile = require('jsonfile');
let fs = require('fs');
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
var log4js = require('log4js');
var logger = log4js.getLogger();
var calHandler = require('./today/calHandler');
var swedishWord = require('./today/swedishword');
var weather = require('./today/weather');
var trains = require('./today/trains');
var history = require('./today/history');
var mdMailer = require('./today/mailer');
var mdFitbit = require('./today/fitbit');
var todayFTSE = require('./today/todayftse');
var quotes = require('./today/quotes');
const log4js = require('log4js');
const logger = log4js.getLogger();
const calHandler = require('./today/calHandler');
const swedishWord = require('./today/swedishword');
const weather = require('./today/weather');
const trains = require('./today/trains');
const history = require('./today/history');
const mdMailer = require('./today/mailer');
const mdFitbit = require('./today/fitbit');
const todayFTSE = require('./today/todayftse');
const quotes = require('./today/quotes');
//var db_name = 'silvrgit';
//var dbCloudant = nano.use(db_name);
@ -29,19 +31,19 @@ var quotes = require('./today/quotes');
*/
var credentials = {
"username": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix",
"password": "8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c",
"host": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"port": 443,
"url": "https://25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix:8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c@25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"database" : "keeper"
const credentials = {
"username": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix",
"password": "8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c",
"host": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"port": 443,
"url": "https://25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix:8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c@25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"database": "keeper"
};
var Cloudant = require('cloudant');
var cloudant = Cloudant({account:credentials.username, password:credentials.password});
const Cloudant = require('cloudant');
const cloudant = Cloudant({account: credentials.username, password: credentials.password});
var dbCloudant = cloudant.db.use(credentials.database);
const dbCloudant = cloudant.db.use(credentials.database);
@ -53,7 +55,7 @@ String.prototype.hashCode = function() {
return this.split('').reduce(function(a,b) {a = ((a << 5) - a) + b.charCodeAt(0);return a & a},0);
} else {
var hash = 0, i, chr, len;
let hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
@ -64,26 +66,26 @@ String.prototype.hashCode = function() {
}
};
var todayCache = {
last: 0, data: {
trains: {last: 0, data: []},
weather: {},
history: [],
today: '',
tv: {entries: []},
cal: {today: [], tomorrow: [], week: []},
swedish: {},
fitbit: {},
ftse: {}
}, expire: ((60 * 1000) * 60)
let todayCache = {
last: 0, data: {
trains: {last: 0, data: []},
weather: {},
history: [],
today: '',
tv: {entries: []},
cal: {today: [], tomorrow: [], week: []},
swedish: {},
fitbit: {},
ftse: {}
}, expire: ((60 * 1000) * 60)
};
var file = __dirname + '/' + 'newdata.json';
var htmlfile = __dirname + '/' + 'today.html';
var eventEmitter;
const file = __dirname + '/' + 'newdata.json';
let htmlfile = __dirname + '/' + 'today.html';
let eventEmitter;
function runable() {
try {
var now = new Date().getTime();
const now = new Date().getTime();
console.log('last updated', ((now - todayCache.last) / 60000));
if (now - todayCache.last < 3600000) {
@ -145,20 +147,20 @@ function saveToDB(data) {
function nth(d) {
// 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';}
var n = d;
const n = d;
return Math.floor(n / 10) === 1 ? 'th' : (n % 10 === 1 ? 'st' : (n % 10 === 2 ? 'nd' : (n % 10 === 3 ? 'rd' : 'th')));
}
function dayNumber() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
const now = new Date();
const start = new Date(now.getFullYear(), 0, 0);
const diff = now - start;
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
function breakDay() {
var now = new Date();
const now = new Date();
return {
year: now.getFullYear(),
month: parseInt(now.getMonth()) + 1,
@ -168,11 +170,11 @@ function breakDay() {
function reduceTrains(d) {
var titles = [], ta = [];
const titles = [], ta = [];
console.log('reducetrains',d);
for (var items in d) {
for (let items in d) {
if (typeof d[items].title !== 'undefined') {
var hash = d[items].title.hashCode();
const hash = d[items].title.hashCode();
if (titles.indexOf(hash) === -1) {
titles.push(hash);
ta.push(d[items]);
@ -188,13 +190,13 @@ function reduceTrains(d) {
* @return {number}
*/
function DayDiff(CurrentDate) {
var TYear = CurrentDate.getFullYear();
var TDay = new Date('January, 01, ' + (parseInt(TYear) + 1));
const TYear = CurrentDate.getFullYear();
const TDay = new Date('January, 01, ' + (parseInt(TYear) + 1));
TDay.getFullYear(TYear);
var DayCount = (TDay - CurrentDate) / (1000 * 60 * 60 * 24);
let DayCount = (TDay - CurrentDate) / (1000 * 60 * 60 * 24);
DayCount = Math.round(DayCount);
var d = new Date();
const d = new Date();
DayCount = d.daysSince('beginning of this year');
return (DayCount);
}
@ -205,7 +207,7 @@ Array.prototype.indexOf = function(e, fn) {
return this.indexOfOld(e);
} else {
if (typeof fn === 'string') {
var att = fn;
const att = fn;
fn = function(e) {
return e[att];
};
@ -228,11 +230,13 @@ module.exports = {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(todayCache));
}, getTodayDate: function() {
var s, d = new Date(), nextYear = (parseInt(d.getFullYear()) + 1).toString() + '-01-01';
let s;
const d = new Date();
let nextYear = (parseInt(d.getFullYear()) + 1).toString() + '-01-01';
console.log(d.daysUntil('beginning of next year'));
var daysSinceStart = d.daysSince('beginning of this year');
var daysRemaining = d.daysUntil('beginning of next year');
const daysSinceStart = d.daysSince('beginning of this year');
const daysRemaining = d.daysUntil('beginning of next year');
todayCache.data.history = [];
s = '<strong>' + d.format('{Weekday} {Month} {dd}, {yyyy}') + '</strong> - ';
@ -245,7 +249,7 @@ module.exports = {
todayCache.data.today = s;
},
refreshTrain: function() {
var self = this;
trains.updateTrains()
.then((d) => {
'use strict';
@ -340,7 +344,7 @@ module.exports = {
for (var t = 0; t < calHandler.calendars.length; t++) {
for (let t = 0; t < calHandler.calendars.length; t++) {
calHandler.getAdvancedCalV3(calHandler.calendars[t])
.then((d) => {
'use strict';
@ -408,7 +412,7 @@ setInterval(function() {
}, (60000));
cron.schedule('45 6 * * *', function() {
cron.schedule('30 6 * * *', function() {
if (runable()) {
module.exports.preLoadToday();
}
@ -432,7 +436,7 @@ cron.schedule('*/15 * * * *', function() {
return -1;
});
cron.schedule('0 7 * * *', function() {
cron.schedule('45 6 * * *', function() {
mdMailer.sendEmailV1(todayCache, __dirname);
saveToDB(todayCache);
// Console.log('tick');

View File

@ -1,31 +1,31 @@
/**
* Created by Martin on 31/03/2016.
*/
var STRING = require('string');
var logger = require('log4js').getLogger();
var request = require('request'), cheerio = require('cheerio');
var _ = require('lodash');
var trainList = [
let STRING = require('string');
const logger = require('log4js').getLogger();
const request = require('request'), cheerio = require('cheerio');
const _ = require('lodash');
const trainList = [
{
id: 'dbeglq',
url: 'http://www.journeycheck.com/scotrail/route?from=DBE&to=GLQ&action=search&savedRoute='
},
{
id: 'glqdbe',
url: 'http://www.journeycheck.com/scotrail/route?from=GLQ&to=DBE&action=search&savedRoute='
id: 'dbehym',
url: 'http://www.journeycheck.com/scotrail/route?from=DBE&to=HYM&action=search&savedRoute='
}
];
module.exports = {
processTrainUpdates: function (body) {
var outputArray = [];
var $ = cheerio.load(body);
var lu = $('DIV#LU').first();
const outputArray = [];
const $ = cheerio.load(body);
const lu = $('DIV#LU').first();
var us = lu.find('.updatesSection').first();
const us = lu.find('.updatesSection').first();
us.find('.updateTitle').each(function (div) {
var wO = {title: '', description: ''};
const wO = {title: '', description: ''};
title = $(this).find('A').first().text().trim();
wO.title = title;
outputArray.push(wO);
@ -33,12 +33,12 @@ module.exports = {
us.find('.updateBodyStart').each(function (div) {
var description = $(this).find('.bodyInner').first().find('.primaryStyle').first().text().trim();
var splitDesc = description.split('\r\n');
let description = $(this).find('.bodyInner').first().find('.primaryStyle').first().text().trim();
const splitDesc = description.split('\r\n');
var wa = [];
for (var i = 0; i < splitDesc.length; i++) {
var contentCheck = splitDesc[i].trim();
const wa = [];
for (let i = 0; i < splitDesc.length; i++) {
let contentCheck = splitDesc[i].trim();
if (contentCheck.indexOf('Impact') > -1) contentCheck = '';
if (contentCheck.indexOf('Additional Information') > -1) contentCheck = '';
if (contentCheck.indexOf('apologise for the delay') > -1) contentCheck = '';
@ -56,9 +56,9 @@ module.exports = {
return new Promise(function (resolve, reject) {
"use strict";
logger.info('Getting train events: ', id);
var url = trainList[id].url;
const url = trainList[id].url;
var now = new Date();
let now = new Date();
// if ((now - eventCache.last) > eventCache.expire) {
request(url, function (err, resp, body) {
@ -68,11 +68,11 @@ module.exports = {
// throw err;
}
var trainData = module.exports.processTrainUpdates(body);
let trainData = module.exports.processTrainUpdates(body);
for (var i = 0; i < out.length; i++) {
var flag = false;
for (var j = 0; j < trainData.length; j++) {
for (let i = 0; i < out.length; i++) {
let flag = false;
for (let j = 0; j < trainData.length; j++) {
flag = _.isEqual(trainData[j], out[i])
}
@ -101,7 +101,7 @@ module.exports = {
}, updateTrains: function () {
logger.info('New Updating trains..');
var output = [];
let output = [];
return new Promise(function (resolve, reject) {
"use strict";
module.exports.getTrainUpdates(0, [])