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 = `