keeper/server/keeper.js

820 lines
19 KiB
JavaScript
Raw Normal View History

2016-04-26 09:06:09 +00:00
'use strict';
2016-03-02 13:53:28 +00:00
/**
* Created by Martin on 22/02/2016.
*/
var express = require('express');
2016-03-21 10:41:03 +00:00
var http = require('http'), request = require('request'), cheerio = require(
'cheerio'), util = require('util');
2016-03-21 10:41:03 +00:00
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
'string');
var converter = require('html-to-markdown');
2016-04-26 09:06:09 +00:00
var zlib = require('zlib');
2016-03-02 13:53:28 +00:00
var log4js = require('log4js');
var logger = log4js.getLogger();
2016-03-04 16:33:48 +00:00
var URL = require('url');
2016-03-02 13:53:28 +00:00
var router = express.Router();
var EventEmitter = require('events');
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
2016-03-02 13:53:28 +00:00
var busEmitter = new EventEmitter();
var db_name = 'keeper';
//var dbCouch = nano.use(db_name);
/*
We've moved to cloudant through IBM Bluemix for the database
https://25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com/dashboard.html#usage
*/
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"
};
var Cloudant = require('cloudant');
var cloudant = Cloudant({account:credentials.username, password:credentials.password});
var dbCloudant = cloudant.db.use(credentials.database);
2016-03-02 13:53:28 +00:00
2016-03-24 10:22:17 +00:00
var jsonFile = __dirname + '/' + 'output.json';
2016-03-02 13:53:28 +00:00
var bodyfile = __dirname + '/' + 'body.html';
var htmlfile = __dirname + '/' + 'testoutput.html';
2016-03-21 10:41:03 +00:00
var generics = [
'ARTICLE',
'div.content_column',
'div.post',
'div.page',
'#recipe-single',
'div.content.body'
2016-03-21 10:41:03 +00:00
];
2016-03-02 13:53:28 +00:00
var specialHandlers = [{
2016-04-26 09:06:09 +00:00
url: 'www.reddit.com', fn: function(body, url) {
2016-03-30 12:53:19 +00:00
return doReddit(body, url);
}
2016-03-30 12:53:19 +00:00
},
{
2016-04-26 09:06:09 +00:00
url: 'developer.android.com', fn: function(body, url) {
return doAndroidDeveloper(body, url);
}
2016-03-30 16:21:49 +00:00
},
{
2016-04-26 09:06:09 +00:00
url: 'www.engadget.com', fn: function(body, url) {
return doEngadget(body, url);
}
2016-03-30 12:53:19 +00:00
}
2016-03-30 16:21:49 +00:00
2016-03-30 12:53:19 +00:00
];
2016-03-21 13:47:26 +00:00
function cleaner(b) {
var _b = b;
var unwanted = [
'LINK',
'META',
'TITLE',
'div#disqus_thread',
'SCRIPT',
'FOOTER',
'div.ssba',
'.shareaholic-canvas',
'.yarpp-related',
'div.dfad',
'div.postFooterShare',
'div#nextPrevLinks',
'.post-comments',
'HEADER',
'.post-title',
'#side-menu',
'.footer-container',
'#pre-footer',
'#cakephp-global-navigation',
'.masthead',
'.breadcrumb-header',
'.single-recipe-sidebar',
2016-03-30 12:33:26 +00:00
'#recipe-related-videos',
'#tnav',
2016-03-30 12:53:19 +00:00
'.footer',
2016-04-26 09:06:09 +00:00
'#tb-wrapper',
'#comments',
'#menu'
];
for (var i = 0; i < unwanted.length; i++) {
_b.find(unwanted[i]).remove();
}
return _b;
2016-03-02 13:53:28 +00:00
}
function insertBookmark(obj) {
logger.debug('Inserting into couch...');
logger.info(util.inspect(obj));
// dbCouch.insert(obj, function(err, body, header) {
dbCloudant.insert(obj, function(err, body, header) {
if (err) {
logger.error('Error inserting into couch');
return;
}
});
logger.debug('Insert done..');
2016-03-02 13:53:28 +00:00
}
2016-03-21 10:41:03 +00:00
function updateBookmark(obj, _id, _rev) {
logger.debug('Updating couch...');
var _obj = obj;
_obj._id = _id;
_obj._rev = _rev;
//dbCouch.insert(_obj, function(err, body, header) {
dbCloudant.insert(_obj, function(err, body, header) {
if (err) {
logger.error('Error updating into couch');
return;
} else {
logger.info('I think we updated ok...');
2016-04-26 09:06:09 +00:00
busEmitter.emit('updateTagsDB');
2016-03-21 13:47:26 +00:00
}
});
logger.debug('Update done..');
2016-03-04 16:33:48 +00:00
}
var doInsertBookmark = (obj) => {
2016-04-26 09:06:09 +00:00
// Logger.info('sendSocket: ' + JSON.stringify(obj));
insertBookmark(obj);
2016-03-02 13:53:28 +00:00
};
var doUpdateBookmark = (obj, _id, _rev) => {
2016-04-26 09:06:09 +00:00
// Logger.info('sendSocket: ' + JSON.stringify(obj));
updateBookmark(obj, _id, _rev);
2016-03-04 16:33:48 +00:00
};
var doGetBookmark = (obj) => {
2016-04-26 09:06:09 +00:00
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(obj);
2016-03-04 16:33:48 +00:00
};
var doGetBookmarkRedo = (obj) => {
2016-04-26 09:06:09 +00:00
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(obj);
2016-03-02 13:53:28 +00:00
};
var doGetBookmarkRes = (url, res) => {
logger.debug('doGetBookmarkRes');
2016-04-26 09:06:09 +00:00
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(url, res);
2016-03-02 13:53:28 +00:00
};
2016-03-21 16:23:19 +00:00
var doUpdateTagsDB = () => {
logger.debug('Update the tags database...');
2016-03-21 16:23:19 +00:00
// dbCouch.view('getAllTags', 'getAllTags', function(err, body) {
dbCloudant.view('getAllTags', 'getAllTags', function(err, body) {
var masterList = [];
if (!err) {
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
2016-03-21 16:23:19 +00:00
masterList = masterList.concat(doc.value);
});
2016-03-21 16:23:19 +00:00
masterList = masterList.filter((value, index, self) => {
return self.indexOf(value) === index;
});
2016-03-21 16:23:19 +00:00
//dbCouch.view('taglist', 'taglist', function(err, body) {
dbCloudant.view('taglist', 'taglist', function(err, body) {
2016-04-26 09:06:09 +00:00
// Logger.debug(body);
if (!err) {
2016-03-21 16:23:19 +00:00
var outJSON = {};
2016-03-21 16:23:19 +00:00
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
doSaveTagsDB(doc.value, masterList);
});
2016-03-21 16:23:19 +00:00
2016-04-26 09:06:09 +00:00
} else {
logger.error('NO TAG LIST EXISTS');
2016-03-21 16:23:19 +00:00
}
});
2016-03-21 16:23:19 +00:00
2016-04-26 09:06:09 +00:00
} else {
}
});
2016-03-21 16:23:19 +00:00
};
var doSaveTagsDB = (orig, newList) => {
logger.debug('doSaveTagsDB');
2016-03-21 16:23:19 +00:00
var _obj = orig;
2016-03-21 16:23:19 +00:00
_obj.taglist = newList;
2016-03-21 16:23:19 +00:00
//dbCouch.insert(_obj, function(err, body, header) {
dbCloudant.insert(_obj, function(err, body, header) {
if (err) {
logger.error('Error updating into couch');
return;
} else {
logger.info('Updated the tags list...');
2016-03-21 16:23:19 +00:00
}
});
2016-03-21 16:23:19 +00:00
};
2016-03-21 13:47:26 +00:00
2016-03-02 13:53:28 +00:00
// Events
busEmitter.on('saveBookmarkData', doInsertBookmark);
2016-03-04 16:33:48 +00:00
busEmitter.on('updateBookmarkData', doUpdateBookmark);
2016-03-02 13:53:28 +00:00
busEmitter.on('getBookmark', doGetBookmark);
busEmitter.on('getBookmarkRes', doGetBookmarkRes);
2016-03-04 16:33:48 +00:00
busEmitter.on('getBookmarkRedo', doGetBookmarkRedo);
2016-03-21 13:47:26 +00:00
busEmitter.on('updateTagsDB', doUpdateTagsDB);
2016-03-21 16:23:19 +00:00
busEmitter.on('saveTagsDB', doSaveTagsDB);
2016-03-21 13:47:26 +00:00
2016-04-26 09:06:09 +00:00
function doEngadget(body, url) {
2016-03-30 16:21:49 +00:00
logger.info('GRABBING Engadget');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
tdihbody = $('DIV#page_body');
logger.debug('Length:' , tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug('Title: ', title);
urlObj = URL.parse(url);
urlPrefix = urlObj.protocol + '//' + urlObj.host + '/';
try {
2016-04-26 09:06:09 +00:00
tdihbody.find('IMG').each(function(i, elem) {
let s, src = $(this).attr('src');
2016-03-30 16:21:49 +00:00
if (src !== null) {
if (!STRING(src).startsWith('http')) {
logger.debug('Stripping:' + src);
src = urlPrefix + STRING(src).stripLeft('/').trim().s;
}
if (typeof obj.thumbnail === 'undefined') {
obj.thumbnail = src;
}
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
2016-04-26 09:06:09 +00:00
$(this).attr('src', s);
2016-03-30 16:21:49 +00:00
}
});
}
catch (e) {
logger.error(e);
}
obj.url = STRING(url).trim().s;
obj.html = $.html();
obj.reduced = STRING(tdihbody.html()).trim().s;
obj.nib = STRING(tdihbody.text()).collapseWhitespace().trim().left(300).s;
obj.title = STRING(title).collapseWhitespace().s;
obj.markdown = converter.convert(obj.reduced);
return obj;
}
2016-04-26 09:06:09 +00:00
function doAndroidDeveloper(body, url) {
2016-03-30 12:53:19 +00:00
logger.info('GRABBING AndroidDeveloper');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
tdihbody = $('DIV.jd-descr');
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
2016-03-30 15:52:15 +00:00
urlObj = URL.parse(url);
urlPrefix = urlObj.protocol + '//' + urlObj.host + '/';
try {
2016-04-26 09:06:09 +00:00
tdihbody.find('IMG').each(function(i, elem) {
let s, src = $(this).attr('src');
2016-03-30 15:52:15 +00:00
if (src !== null) {
if (!STRING(src).startsWith('http')) {
logger.debug('Stripping:' + src);
src = urlPrefix + STRING(src).stripLeft('/').trim().s;
}
if (typeof obj.thumbnail === 'undefined') {
obj.thumbnail = src;
}
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
2016-04-26 09:06:09 +00:00
$(this).attr('src', s);
2016-03-30 15:52:15 +00:00
}
});
}
catch (e) {
logger.error(e);
}
2016-03-30 12:53:19 +00:00
obj.url = STRING(url).trim().s;
obj.html = $.html();
obj.reduced = STRING(tdihbody.html()).trim().s;
obj.nib = STRING(tdihbody.text()).collapseWhitespace().trim().left(300).s;
obj.title = STRING(title).collapseWhitespace().s;
obj.markdown = converter.convert(obj.reduced);
return obj;
}
2016-03-04 16:33:48 +00:00
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
function doReddit(body, url) {
logger.info('GRABBING REDDIT');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
2016-03-02 13:53:28 +00:00
var $ = cheerio.load(body);
var title = $('TITLE').text();
2016-03-02 13:53:28 +00:00
tdihbody = $('DIV.entry');
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
tdihbody.find('A.thumbnail').each(function(i, elem) {
2016-03-02 13:53:28 +00:00
logger.warn($(this));
});
2016-03-02 13:53:28 +00:00
logger.info('++++++');
2016-04-26 09:06:09 +00:00
// Logger.debug(tdihbody.html());
2016-03-02 13:53:28 +00:00
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
2016-03-04 16:33:48 +00:00
obj.url = STRING(url).trim().s;
obj.html = $.html();
obj.reduced = STRING(tdihbody.html()).trim().s;
obj.nib = STRING(tdihbody.text()).collapseWhitespace().trim().left(300).s;
obj.title = STRING(title).collapseWhitespace().s;
obj.markdown = converter.convert(obj.reduced);
2016-03-24 10:22:17 +00:00
return obj;
}
2016-03-24 10:22:17 +00:00
function genericProcessor(body, url) {
logger.info('USING DEFAULT PROCESSOR');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
2016-03-02 16:32:06 +00:00
i = 0;
2016-03-02 16:32:06 +00:00
while (($(generics[i]).length == 0) && (i < generics.length)) {
i++;
}
logger.debug(i);
2016-03-02 16:32:06 +00:00
if (i < generics.length) {
logger.warn('Used a generic');
tdihbody = $(generics[i]);
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
} else {
logger.warn('Using whole body');
2016-04-26 09:06:09 +00:00
// Bah. nothing to reduce so just grab the body, tidy it and use that
tdihbody = $('BODY');
2016-03-24 10:22:17 +00:00
if (tdihbody.length === 0) {
2016-03-24 10:22:17 +00:00
2016-04-26 09:06:09 +00:00
tdihbody = $(':root');
2016-03-24 10:22:17 +00:00
2016-03-21 13:47:26 +00:00
}
2016-03-04 16:33:48 +00:00
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
}
2016-04-26 09:06:09 +00:00
// Logger.info(util.inspect(tdihbody));
urlObj = URL.parse(url);
urlPrefix = urlObj.protocol + '//' + urlObj.host + '/';
try {
2016-04-26 09:06:09 +00:00
tdihbody.find('IMG').each(function(i, elem) {
let s, src = $(this).attr('src');
2016-03-30 16:21:49 +00:00
console.log('!!!!' + src);
if (src !== null && typeof src !== 'undefined') {
if (!STRING(src).startsWith('http')) {
logger.debug('Stripping:' + src);
src = urlPrefix + STRING(src).stripLeft('/').trim().s;
}
if (typeof obj.thumbnail === 'undefined') {
obj.thumbnail = src;
}
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
2016-04-26 09:06:09 +00:00
$(this).attr('src', s);
}
});
}
catch (e) {
logger.error(e);
}
obj.url = STRING(url).trim().s;
obj.html = $.html();
obj.reduced = STRING(tdihbody.html()).trim().s;
obj.nib = STRING(tdihbody.text()).collapseWhitespace().trim().left(300).s;
obj.title = STRING(title).collapseWhitespace().s;
obj.markdown = converter.convert(obj.reduced);
2016-03-02 13:53:28 +00:00
return obj;
2016-03-02 13:53:28 +00:00
}
function processBody(body, url, _id, _rev) {
2016-03-04 16:33:48 +00:00
var obj = {}, i, urlObj, urlPrefix;
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
// Try to find a body to grab
urlObj = URL.parse(url);
logger.debug('host:', urlObj.host);
var flag;
2016-04-26 09:06:09 +00:00
for (i = 0;i < specialHandlers.length;i++) {
if (urlObj.host === specialHandlers[i].url) {
2016-04-26 09:06:09 +00:00
flag = true;
obj = specialHandlers[i].fn(body,url);
2016-03-21 10:41:03 +00:00
}
}
if (!flag) {
2016-04-26 09:06:09 +00:00
// Do generic processing
obj = genericProcessor(body,url);
}
2016-04-26 09:06:09 +00:00
// Logger.warn(obj.reduced);
obj.host = urlObj.host;
2016-04-26 09:06:09 +00:00
/* Jsonfile.writeFile(jsonFile, obj, function (err) {
console.error(err);
});*/
if (_id !== null) {
2016-04-26 09:06:09 +00:00
busEmitter.emit('updateBookmarkData', obj, _id, _rev);
} else {
busEmitter.emit('saveBookmarkData', obj);
}
return obj;
2016-03-21 10:41:03 +00:00
}
function genericGrab(obj, res) {
var url, _id = null, _ver = null;
2016-03-21 13:47:26 +00:00
if (typeof obj === 'string') {
logger.info(obj);
url = obj;
2016-04-26 09:06:09 +00:00
} else {
url = obj.url;
_id = obj._id || null;
_ver = obj._rev || null;
}
2016-03-21 13:47:26 +00:00
logger.warn(typeof obj);
logger.info(url);
logger.info(_id);
logger.info(_ver);
2016-03-23 10:29:00 +00:00
var options = {
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
},
jar: true,
followRedirect: true,
followAllRedirects: true
2016-03-23 10:29:00 +00:00
};
2016-04-26 09:06:09 +00:00
request(options, function(err, resp, body) {
if (err)
throw err;
2016-03-21 13:47:26 +00:00
if (resp.headers.hasOwnProperty('content-encoding')) {
logger.warn('content-encoding');
if (resp.headers['content-encoding'] == 'gzip') {
// to test http://chaosinthekitchen.com/2009/07/lime-and-coconut-chicken/
var gunzip = zlib.createGunzip();
var jsonString = '';
resp.pipe(gunzip);
2016-04-26 09:06:09 +00:00
gunzip.on('data', function(chunk) {
jsonString += chunk;
});
2016-04-26 09:06:09 +00:00
gunzip.on('end', function() {
// Console.log((jsonString));
callback(JSON.stringify(jsonString));
});
2016-04-26 09:06:09 +00:00
gunzip.on('error', function(e) {
console.log(e);
});
2016-04-26 09:06:09 +00:00
} else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed');
2016-03-21 13:47:26 +00:00
}
}
2016-03-21 13:47:26 +00:00
2016-04-26 09:06:09 +00:00
} else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed', {data: b});
}
}
});
2016-03-02 13:53:28 +00:00
}
2016-04-26 09:06:09 +00:00
router.get('/pocket', function(req, res) {
2016-04-13 13:37:14 +00:00
logger.debug('list..');
// dbCouch.view('pocketList', 'pocketList', function(err, body) {
dbCloudant.view('pocketList', 'pocketList', function(err, body) {
2016-04-13 13:37:14 +00:00
if (!err) {
var outJSON = [];
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
2016-04-13 13:37:14 +00:00
var obj = {id: doc.id, entry: doc.value};
2016-04-26 09:06:09 +00:00
console.log(typeof obj.entry.tn);
2016-04-13 13:37:14 +00:00
if (typeof obj.entry.tn === 'string') {
console.log('its a string:', typeof obj.entry.tn)
obj.entry.tn = 'http://image.silvrtree.co.uk/100,fit,q80/' + obj.entry.tn;
2016-04-26 09:06:09 +00:00
} else {
2016-04-13 13:37:14 +00:00
obj.entry.tn = 'gfx/fm.png';
}
outJSON.push(obj);
});
logger.debug(util.inspect(body));
logger.info(util.inspect(outJSON));
res.render('pocket', {data: outJSON});
2016-04-26 09:06:09 +00:00
} else {
res.writeHead(500, {ContentType: 'application/json'});
2016-04-13 13:37:14 +00:00
res.end(JSON.stringify({}));
}
});
});
2016-04-26 09:06:09 +00:00
router.get('/list', function(req, res) {
logger.debug('list..');
2016-03-02 13:53:28 +00:00
//dbCouch.view('titles', 'titles', function(err, body) {
dbCloudant.view('titles', 'titles', function(err, body) {
if (!err) {
2016-03-02 13:53:28 +00:00
var outJSON = [];
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
outJSON.push({id: doc.id, title: doc.value});
});
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
2016-03-02 13:53:28 +00:00
});
2016-04-26 09:06:09 +00:00
router.get('/entry/:id', function(req, res) {
logger.debug('entry..');
2016-03-02 13:53:28 +00:00
logger.debug(req.params.id);
2016-03-02 13:53:28 +00:00
//dbCouch.get(req.params.id, function(err, body) {
dbCloudant.get(req.params.id, function(err, body) {
if (!err) {
2016-03-02 13:53:28 +00:00
var outJSON = {};
outJSON._id = body._id;
outJSON._rev = body._rev;
outJSON.title = body.title;
outJSON.reduced = body.reduced;
outJSON.url = body.url;
outJSON.tags = body.tags || {solid: '', list: []};
2016-03-21 12:49:54 +00:00
2016-04-26 09:06:09 +00:00
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify(outJSON));
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
2016-03-02 13:53:28 +00:00
2016-03-21 16:23:19 +00:00
});
2016-07-26 09:51:25 +00:00
router.route('/tags')
.get(function(req, res, next) {
logger.debug('tag list..');
logger.debug(req.params.id);
//dbCouch.view('taglist', 'taglist', function(err, body) {
dbCloudant.view('taglist', 'taglist', function(err, body) {
if (!err) {
logger.debug(body);
var outJSON = [];
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
logger.info(doc.value.taglist);
if (doc.value[0] == req.params.id) {
outJSON = doc.value.taglist.sort();
}
});
2016-03-21 16:23:19 +00:00
2016-04-26 09:06:09 +00:00
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
2016-03-21 16:23:19 +00:00
2016-04-26 09:06:09 +00:00
} else {
logger.error(err);
2016-04-26 09:06:09 +00:00
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
2016-03-21 16:23:19 +00:00
2016-07-26 09:51:25 +00:00
}).post(function(req, res, next) {
var t = req.body;
console.log(t);
logger.info('regetting:' + req.body._id);
//dbCouch.get(req.body._id, function(err, body) {
dbCloudant.get(req.body._id, function(err, body) {
2016-07-26 09:51:25 +00:00
if (!err) {
var obj = {};
obj.url = body.url;
obj.html = body.html;
obj.reduced = body.reduced;
obj.title = body.title;
obj.tags = req.body.tags;
logger.info('Updating...');
busEmitter.emit('updateBookmarkData', obj, body._id, body._rev, res);
var outJSON = {};
outJSON._id = body._id;
outJSON._rev = body._rev;
outJSON.title = body.title;
outJSON.reduced = body.reduced;
outJSON.url = body.url;
outJSON.tags = req.body.tags;
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify(outJSON));
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
2016-03-21 16:23:19 +00:00
});
2016-07-26 09:51:25 +00:00
2016-04-26 09:06:09 +00:00
router.get('/tags/:id', function(req, res) {
logger.debug('entry..');
logger.debug(req.params.id);
//dbCouch.view('getTagByKey', 'getTagByKey', function(err, body) {
dbCloudant.view('getTagByKey', 'getTagByKey', function(err, body) {
if (!err) {
2016-04-26 09:06:09 +00:00
// Logger.debug(body);
var outJSON = [];
2016-04-26 09:06:09 +00:00
body.rows.forEach(function(doc) {
// Logger.debug(doc);
if (doc.value[0] == req.params.id) {
outJSON.push({id: doc.id, title: doc.value[1]})
2016-03-21 16:23:19 +00:00
}
});
2016-03-02 13:53:28 +00:00
2016-04-26 09:06:09 +00:00
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
2016-03-21 13:47:26 +00:00
2016-04-26 09:06:09 +00:00
} else {
logger.error(err);
2016-04-26 09:06:09 +00:00
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
2016-03-21 13:47:26 +00:00
}
});
2016-03-02 13:53:28 +00:00
});
2016-04-26 09:06:09 +00:00
router.post('/add', function(req, res) {
logger.debug('add entry..');
var t = req.body;
if (t.hasOwnProperty('url')) {
var url = JSON.parse(t.url.toString());
logger.debug(url);
2016-04-26 09:06:09 +00:00
busEmitter.emit('getBookmark', t);
} else {
logger.error('No data block!');
}
2016-04-26 09:06:09 +00:00
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({adding: url}));
2016-03-21 12:49:54 +00:00
});
2016-03-21 12:49:54 +00:00
2016-03-04 16:33:48 +00:00
2016-04-26 09:06:09 +00:00
router.post('/redo', function(req, res) {
logger.debug('redoing entry..');
var t = req.body;
console.log(t);
if (t.hasOwnProperty('url')) {
var url = t.url.toString();
logger.debug(url);
2016-04-26 09:06:09 +00:00
busEmitter.emit('getBookmark', t);
} else {
logger.error('No data block!');
}
2016-04-26 09:06:09 +00:00
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({adding: url}));
2016-03-02 13:53:28 +00:00
});
2016-07-26 09:51:25 +00:00
router.route('/new')
.get(function(req, res, next) {
logger.debug('Save new');
2016-04-26 09:06:09 +00:00
busEmitter.emit('getBookmarkRes', req.query.url, res);
2016-07-26 09:51:25 +00:00
}).post(function(req, res, next) {
2016-03-30 11:02:13 +00:00
logger.debug('Posted Save new');
2016-04-26 09:06:09 +00:00
logger.info(req.body);
if (Object.keys(req.body).length !== 0) {
busEmitter.emit('getBookmarkRes', req.body.url, res);
} else {
2016-03-30 11:02:13 +00:00
res.status(422).end();
}
});
2016-03-21 16:23:19 +00:00
busEmitter.emit('updateTagsDB');
2016-03-02 13:53:28 +00:00
module.exports = router;