const cron = require('node-cron'); const LocalStorage = require('node-localstorage').LocalStorage; // var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984'); const logger = require('log4js').getLogger(); const prettyMs = require('pretty-ms'); const Sugar = require('sugar'); const calHandler = require('./lib/calHandler'); const swedishWord = require('./lib/swedishword'); const weather = require('./lib/weather'); const trains = require('./lib/trains'); const history = require('./lib/history'); const mdMailer = require('./lib/mailer'); // const mdFitbit = require('./lib/fitbit'); const todayFTSE = require('./lib/todayftse'); const quotes = require('./lib/quotes'); const euronews = require('./lib/euronews'); // var db_name = 'silvrgit'; // var dbCloudant = nano.use(db_name); const memwatch = require('memwatch-next'); memwatch.on('leak', (info) => { console.error('Memory leak detected:\n', info); }); localStorage = new LocalStorage('./scratch'); logger.level = 'trace'; 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': 'today' }; const Cloudant = require('cloudant'); const cloudant = Cloudant({ 'account': credentials.username, 'password': credentials.password }); const dbCloudant = cloudant.db.use(credentials.database); String.prototype.hashCode = function () { if (Array.prototype.reduce) return this.split('').reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0); else { 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); hash = ((hash << 5) - hash) + chr; hash |= 0; // Convert to 32bit integer } return hash; } }; const todayCacheSrc = { '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) }; function saveLastRunTime() { const now = new Date(); logger.info('Logging last run...'); localStorage.setItem('lastRun', now.getTime()); } function saveToday() { logger.info('Saving today...'); localStorage.setItem('today', JSON.stringify(todayCache)); } function runable() { try { const lastRun = localStorage.getItem('lastRun'); logger.debug('lastrun', lastRun); const now = new Date().getTime(); const timeDiff = now - lastRun; logger.info('last updated', prettyMs(timeDiff)); if (now - lastRun < 3600000) return false; else { todayCache.last = now; return true; } } catch (e) { logger.debug('Creating new today object.. Lets go!'); todayCache = Object.assign({}, todayCacheSrc); todayCache.last = new Date().getTime(); return true; } } async function testNews() { await euronews.getEuroNews() .then((d) => { // todayCache.data.news = d; // logger.debug(d); // console.log(); h = euronews.render(d.items); let html = ` Title ${h} `; logger.debug(html); }) .catch((e) => { logger.error(e); }); } async function preLoadToday() { function compare(a, b) { if (a.ts < b.ts) return -1; if (a.ts > b.ts) return 1; return 0; } logger.debug('preload'); todayCache.data.cal = { 'today': [], 'tomorrow': [], 'week': [] }; await weather.newDoGetWeather() .then((d) => { todayCache.data.weather = d; }) .catch((e) => { logger.error(e); }); await trains.updateTrains() .then((d) => { 'use strict'; console.log('Trains: ', d); todayCache.data.trains.data = d; todayCache.data.trains.last = new Date(); }) .catch((e) => { 'use strict'; console.error(e); }); await history.updateHistory() .then((d) => { 'use strict'; console.log('History result: ', d); todayCache.data.history = d; }) .catch((e) => { 'use strict'; console.error(e); }); await calHandler.getSimpleCalV3( 'http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc') .then((d) => { 'use strict'; todayCache.data.tv = d; }) .catch((e) => { 'use strict'; logger.error(e); }); await todayFTSE.getFTSE() .then((d) => { todayCache.data.ftse = d; }) .catch((e) => { logger.error(e); }); await quotes.GetQuotes() .then((d) => { todayCache.data.quotes = d; }) .catch((e) => { logger.error(e); }); for (const item of calHandler.calendars) await calHandler.getAdvancedCalV3(item) .then((d) => { todayCache.data.cal.today = todayCache.data.cal.today.concat(d.today); todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.concat(d.tomorrow); todayCache.data.cal.week = todayCache.data.cal.week.concat(d.week); }) .catch((e) => { 'use strict'; logger.error(e); }); console.log('>> SORT!!'); todayCache.data.cal.today = todayCache.data.cal.today.sort(compare); todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.sort(compare); todayCache.data.cal.week = todayCache.data.cal.week.sort(compare); await swedishWord.getSwedishWord() .then((d) => { 'use strict'; logger.debug('Got swedish...'); todayCache.data.swedish = d; }) .catch((e) => { 'use strict'; logger.error(e); }); await euronews.getEuroNews() .then((d) => { todayCache.data.news = d; }) .catch((e) => { logger.error(e); }); await saveToday(); } // saveLastRunTime(); setTimeout(function () { // loadData(); if (runable()) { logger.trace('Runable'); preLoadToday(); // testNews(); } // Module.exports.preLoadToday(); }, 5000);