From db37320e4ca7292a26367a539b49aa75a552e676 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Thu, 24 Mar 2016 17:09:42 +0000 Subject: [PATCH] added archiving to today --- db/maker.js | 27 +++++++++++++++++++++++++++ lib/today.js | 31 ++++++++++++++++++++++++++++--- package.json | 3 ++- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 db/maker.js diff --git a/db/maker.js b/db/maker.js new file mode 100644 index 0000000..8c94205 --- /dev/null +++ b/db/maker.js @@ -0,0 +1,27 @@ +var nano = require('nano')('http://localhost:5984'); + +// clean up the database we created previously +nano.db.destroy('silvrgit', function () { + // create a new database + nano.db.create('silvrgit', function () { + // specify the database we are going to use + var keeper = nano.use('silvrgit'); + // and insert a document in it + /* keeper.insert({ crazy: true }, 'rabbit', function(err, body, header) { + if (err) { + console.log('[alice.insert] ', err.message); + return; + } + console.log('you have inserted the rabbit.') + console.log(body); + });*/ + + +}); +}); +/** + * Created by Martin on 02/03/2016. + */ + + + diff --git a/lib/today.js b/lib/today.js index 659e973..57460bf 100644 --- a/lib/today.js +++ b/lib/today.js @@ -8,11 +8,17 @@ 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('./calHandler'); var swedishWord = require('./swedishword'); +var db_name = 'silvrgit'; +var dbCouch = nano.use(db_name); + + var todayCache = { last: 0, data: { trains: {last: 0, data: []}, @@ -59,6 +65,18 @@ function saveData() { jsonfile.writeFileSync(file, todayCache); } +function saveToDB(data) { + logger.debug('Inserting into couch...'); + // logger.info(util.inspect(obj)); + dbCouch.insert(data, function (err, body, header) { + if (err) { + logger.error('Error inserting into couch'); + logger.error(err); + return; + } + }); +} + function nth(d) { if (d > 3 && d < 21) return 'th'; // thanks kennebec switch (d % 10) { @@ -81,6 +99,11 @@ function dayNumber() { 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} */ @@ -436,6 +459,7 @@ module.exports = { logger.error(e); } + todayCache.date = breakDay(); // word of the day http://wotd.transparent.com/rss/swedish-widget.xml?t=1455840000000 // time stamp @@ -497,9 +521,9 @@ setTimeout(function() { module.exports.preLoadToday(); }, 15000); -/*setTimeout(function () { - sendEmailV1(); - }, 45000);*/ +setTimeout(function () { + saveToDB(todayCache); + }, 45000); cron.schedule('45 6 * * *', function() { module.exports.preLoadToday(); @@ -513,6 +537,7 @@ cron.schedule('0 */1 * * *', function() { cron.schedule('0 7 * * *', function() { sendEmailV1(); + saveToDB(todayCache); // console.log('tick'); return -1; }); diff --git a/package.json b/package.json index c9e658b..baf7d7b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "lodash": "^4.3.0", "log4js": "^0.6.31", "mammoth": "^0.3.25-pre.1", + "nano": "^6.2.0", "request": "^2.67.0", "simple-weather": "^1.2.2", "wordsoap": "^0.2.0", @@ -39,6 +40,6 @@ }, "scripts": { "start": "node web-server.js", - "copy" : "sudo cp -r ./. /var/www/silvrtree" + "copy": "sudo cp -r ./. /var/www/silvrtree" } }