Added reddit handler and concept for future specific targeted processing.

This commit is contained in:
Martin Donnelly 2016-03-30 11:36:09 +01:00
parent 8d563dabc7
commit f6d181cd3e
4 changed files with 459 additions and 378 deletions

View File

@ -17,7 +17,7 @@ app.use(logger('dev'));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'app'))); app.use(express.static(path.join(__dirname, 'dist')));
app.use('/', keeper); app.use('/', keeper);

View File

@ -5,7 +5,7 @@
"main": "recipe-server.js", "main": "recipe-server.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"update": "git pull && npm install && gulp default" "refresh": "git pull && npm install && gulp default"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -37,6 +37,7 @@
"gulp-notify": "^2.2.0", "gulp-notify": "^2.2.0",
"gulp-rename": "^1.2.2", "gulp-rename": "^1.2.2",
"gulp-uglify": "^1.5.3", "gulp-uglify": "^1.5.3",
"html-to-markdown": "^1.0.0",
"jshint": "^2.9.1", "jshint": "^2.9.1",
"jsonfile": "^2.2.3", "jsonfile": "^2.2.3",
"log4js": "^0.6.31", "log4js": "^0.6.31",

View File

@ -4,9 +4,10 @@
*/ */
var express = require('express'); var express = require('express');
var http = require('http'), request = require('request'), cheerio = require( var http = require('http'), request = require('request'), cheerio = require(
'cheerio'), util = require('util'); 'cheerio'), util = require('util');
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require( var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
'string'); 'string');
var converter = require('html-to-markdown');
var zlib = require("zlib"); var zlib = require("zlib");
var log4js = require('log4js'); var log4js = require('log4js');
var logger = log4js.getLogger(); var logger = log4js.getLogger();
@ -26,164 +27,168 @@ var jsonFile = __dirname + '/' + 'output.json';
var bodyfile = __dirname + '/' + 'body.html'; var bodyfile = __dirname + '/' + 'body.html';
var htmlfile = __dirname + '/' + 'testoutput.html'; var htmlfile = __dirname + '/' + 'testoutput.html';
var generics = [ var generics = [
'ARTICLE', 'ARTICLE',
'div.content_column', 'div.content_column',
'div.post', 'div.post',
'div.page', 'div.page',
'#recipe-single' '#recipe-single',
'div.content.body'
]; ];
var specialHandlers = [{
url: 'www.reddit.com', fn: function (body, url) {
return doReddit(body, url);
}
}];
function cleaner(b) { function cleaner(b) {
var _b = b; var _b = b;
var unwanted = [ var unwanted = [
'div#disqus_thread', 'LINK',
'SCRIPT', 'META',
'FOOTER', 'TITLE',
'div.ssba', 'div#disqus_thread',
'.shareaholic-canvas', 'SCRIPT',
'.yarpp-related', 'FOOTER',
'div.dfad', 'div.ssba',
'div.postFooterShare', '.shareaholic-canvas',
'div#nextPrevLinks', '.yarpp-related',
'.post-comments', 'div.dfad',
'HEADER', 'div.postFooterShare',
'.post-title', 'div#nextPrevLinks',
'#side-menu', '.post-comments',
'.footer-container', 'HEADER',
'#pre-footer', '.post-title',
'#cakephp-global-navigation', '#side-menu',
'.masthead', '.footer-container',
'.breadcrumb-header', '#pre-footer',
'.single-recipe-sidebar', '#cakephp-global-navigation',
'#recipe-related-videos' '.masthead',
]; '.breadcrumb-header',
'.single-recipe-sidebar',
'#recipe-related-videos'
for (var i = 0; i < unwanted.length; i++) { ];
_b.find(unwanted[i]).remove();
}
return _b; for (var i = 0; i < unwanted.length; i++) {
_b.find(unwanted[i]).remove();
}
return _b;
} }
function insertBookmark(obj) { function insertBookmark(obj) {
logger.debug('Inserting into couch...'); logger.debug('Inserting into couch...');
logger.info(util.inspect(obj)); // logger.info(util.inspect(obj));
dbCouch.insert(obj, function (err, body, header) { dbCouch.insert(obj, function (err, body, header) {
if (err) { if (err) {
logger.error('Error inserting into couch'); logger.error('Error inserting into couch');
return; return;
} }
}); });
logger.debug('Insert done..'); logger.debug('Insert done..');
} }
function updateBookmark(obj, _id, _rev) { function updateBookmark(obj, _id, _rev) {
logger.debug('Updating couch...'); logger.debug('Updating couch...');
var _obj = obj; var _obj = obj;
_obj._id = _id; _obj._id = _id;
_obj._rev = _rev; _obj._rev = _rev;
dbCouch.insert(_obj, function (err, body, header) { dbCouch.insert(_obj, function (err, body, header) {
if (err) { if (err) {
logger.error('Error updating into couch'); logger.error('Error updating into couch');
return; return;
} else { } else {
logger.info('I think we updated ok...'); logger.info('I think we updated ok...');
busEmitter.emit("updateTagsDB"); busEmitter.emit("updateTagsDB");
} }
}); });
logger.debug('Update done..'); logger.debug('Update done..');
} }
var doInsertBookmark = (obj) => var doInsertBookmark = (obj) => {
{ // logger.info('sendSocket: ' + JSON.stringify(obj));
// logger.info('sendSocket: ' + JSON.stringify(obj)); insertBookmark(obj);
insertBookmark(obj);
}; };
var doUpdateBookmark = (obj, _id, _rev) => var doUpdateBookmark = (obj, _id, _rev) => {
{ // logger.info('sendSocket: ' + JSON.stringify(obj));
// logger.info('sendSocket: ' + JSON.stringify(obj)); updateBookmark(obj, _id, _rev);
updateBookmark(obj, _id, _rev);
}; };
var doGetBookmark = (obj) => var doGetBookmark = (obj) => {
{ // logger.info('sendSocket: ' + JSON.stringify(obj));
// logger.info('sendSocket: ' + JSON.stringify(obj)); genericGrab(obj);
genericGrab(obj);
}; };
var doGetBookmarkRedo = (obj) => var doGetBookmarkRedo = (obj) => {
{ // logger.info('sendSocket: ' + JSON.stringify(obj));
// logger.info('sendSocket: ' + JSON.stringify(obj)); genericGrab(obj);
genericGrab(obj);
}; };
var doGetBookmarkRes = (url, res) => var doGetBookmarkRes = (url, res) => {
{ logger.debug('doGetBookmarkRes');
logger.debug('doGetBookmarkRes'); // logger.info('sendSocket: ' + JSON.stringify(obj));
// logger.info('sendSocket: ' + JSON.stringify(obj)); genericGrab(url, res);
genericGrab(url, res);
}; };
var doUpdateTagsDB = () => { var doUpdateTagsDB = () => {
logger.debug('Update the tags database...'); logger.debug('Update the tags database...');
dbCouch.view('getAllTags', 'getAllTags', function (err, body) { dbCouch.view('getAllTags', 'getAllTags', function (err, body) {
var masterList = []; var masterList = [];
if (!err) {
body.rows.forEach(function (doc) {
masterList = masterList.concat(doc.value);
});
masterList = masterList.filter((value, index, self) => {
return self.indexOf(value) === index;
});
dbCouch.view('taglist', 'taglist', function (err, body) {
// logger.debug(body);
if (!err) { if (!err) {
body.rows.forEach(function (doc) {
masterList = masterList.concat(doc.value); var outJSON = {};
});
masterList = masterList.filter((value, index, self) => { body.rows.forEach(function (doc) {
return self.indexOf(value) === index; doSaveTagsDB(doc.value, masterList);
}); });
dbCouch.view('taglist', 'taglist', function (err, body) {
logger.debug(body);
if (!err) {
var outJSON = {};
body.rows.forEach(function (doc) {
doSaveTagsDB(doc.value,masterList);
});
}
else {
logger.error('NO TAG LIST EXISTS');
}
});
} }
else { else {
logger.error('NO TAG LIST EXISTS');
} }
}); });
}
else {
}
});
}; };
var doSaveTagsDB = (orig, newList) => var doSaveTagsDB = (orig, newList) => {
{ logger.debug('doSaveTagsDB');
logger.debug('doSaveTagsDB');
var _obj = orig; var _obj = orig;
_obj.taglist = newList; _obj.taglist = newList;
dbCouch.insert(_obj, function (err, body, header) { dbCouch.insert(_obj, function (err, body, header) {
if (err) { if (err) {
logger.error('Error updating into couch'); logger.error('Error updating into couch');
return; return;
} else { } else {
logger.info('Updated the tags list...'); logger.info('Updated the tags list...');
} }
}); });
}; };
// Events // Events
@ -197,354 +202,429 @@ busEmitter.on('updateTagsDB', doUpdateTagsDB);
busEmitter.on('saveTagsDB', doSaveTagsDB); busEmitter.on('saveTagsDB', doSaveTagsDB);
function doReddit(body, url)
{
logger.info('GRABBING REDDIT');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
tdihbody = $('DIV.entry');
tdihbody.find('A.thumbnail').each(function (i, elem) {
logger.warn($(this));
});
logger.info('++++++');
// logger.debug(tdihbody.html());
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
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;
}
function genericProcessor(body, url) {
logger.info('USING DEFAULT PROCESSOR');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
i = 0;
while (($(generics[i]).length == 0) && (i < generics.length)) {
i++;
}
logger.debug(i);
if (i < generics.length) {
logger.warn('Used a generic');
tdihbody = $(generics[i]);
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
}
else {
logger.warn('Using whole body');
// bah. nothing to reduce so just grab the body, tidy it and use that
tdihbody = $('BODY');
if (tdihbody.length === 0) {
tdihbody = $(":root");
}
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
}
// logger.info(util.inspect(tdihbody));
urlObj = URL.parse(url);
urlPrefix = urlObj.protocol + '//' + urlObj.host + '/';
try {
tdihbody.find('IMG').each(function (i, elem) {
let s, src = $(this).attr("src");
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;
$(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);
return obj;
}
function processBody(body, url, _id, _rev) { function processBody(body, url, _id, _rev) {
var obj, tdihbody, i, urlObj, urlPrefix; var obj = {}, i, urlObj, urlPrefix;
var $ = cheerio.load(body);
var title = $('TITLE').text();
// try to find a body to grab
i = 0;
while (($(generics[i]).length == 0) && (i < generics.length)) {
i++; // try to find a body to grab
urlObj = URL.parse(url);
logger.debug('host:', urlObj.host);
var flag;
for (i=0;i<specialHandlers.length;i++)
{
if (urlObj.host === specialHandlers[i].url) {
flag=true;
obj = specialHandlers[i].fn(body,url);
} }
logger.debug(i); }
obj = {}; if (!flag) {
if (i < generics.length) { // do generic processing
tdihbody = $(generics[i]); obj = genericProcessor(body,url);
logger.debug(tdihbody.length); }
tdihbody = cleaner(tdihbody);
logger.debug(title);
} // logger.warn(obj.reduced);
else {
// bah. nothing to reduce so just grab the body, tidy it and use that
tdihbody = $('BODY');
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
logger.debug(title);
}
urlObj = URL.parse(url); obj.host = urlObj.host;
urlPrefix = urlObj.protocol + '//' + urlObj.host + '/';
obj.host = urlObj.host;
try {
tdihbody.find('IMG').each(function (i, elem) {
let s, src = $(this).attr("src");
if (!STRING(src).startsWith('http')) {
src = urlPrefix + STRING(src).stripLeft('/').trim().s;
}
if (typeof obj.thumbnail === 'undefined') {
obj.thumbnail = src;
}
jsonfile.writeFile(jsonFile, obj, function (err) {
console.error(err);
});
s = 'http://image.silvrtree.co.uk/900,fit/' + src; if (_id !== null) {
busEmitter.emit("updateBookmarkData", obj, _id, _rev);
}
else {
busEmitter.emit("saveBookmarkData", obj);
}
$(this).attr("src", s); return obj;
});
}
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;
jsonfile.writeFile(jsonFile, obj, function (err) { console.error(err); });
if (_id !== null) {
busEmitter.emit("updateBookmarkData", obj, _id, _rev);
}
else {
busEmitter.emit("saveBookmarkData", obj);
}
return obj;
} }
function genericGrab(obj, res) { function genericGrab(obj, res) {
var url, _id = null, _ver = null; var url, _id = null, _ver = null;
if (typeof obj === 'string') { if (typeof obj === 'string') {
logger.info(obj); logger.info(obj);
url = obj; url = obj;
} }
else { else {
url = obj.url; url = obj.url;
_id = obj._id || null; _id = obj._id || null;
_ver = obj._rev || null; _ver = obj._rev || null;
} }
logger.warn(typeof obj); logger.warn(typeof obj);
logger.info(url);
logger.info(_id);
logger.info(_ver);
logger.info(url);
logger.info(_id);
logger.info(_ver);
var options = { var options = {
url: url, url: url,
headers: { 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' '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, jar: true,
followRedirect:true, followRedirect: true,
followAllRedirects:true followAllRedirects: true
}; };
request(options, function (err, resp, body) { request(options, function (err, resp, body) {
if (err) if (err)
throw err; throw err;
if (resp.headers.hasOwnProperty('content-encoding')) { if (resp.headers.hasOwnProperty('content-encoding')) {
logger.warn('content-encoding'); logger.warn('content-encoding');
if (resp.headers['content-encoding'] == 'gzip') { if (resp.headers['content-encoding'] == 'gzip') {
// to test http://chaosinthekitchen.com/2009/07/lime-and-coconut-chicken/ // to test http://chaosinthekitchen.com/2009/07/lime-and-coconut-chicken/
var gunzip = zlib.createGunzip();
var jsonString = '';
resp.pipe(gunzip);
gunzip.on('data', function (chunk) {
jsonString += chunk;
});
gunzip.on('end', function () {
console.log((jsonString));
callback(JSON.stringify(jsonString));
});
gunzip.on('error', function (e) {
console.log(e);
});
}
else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed');
}
}
var gunzip = zlib.createGunzip();
var jsonString = '';
resp.pipe(gunzip);
gunzip.on('data', function (chunk) {
jsonString += chunk;
});
gunzip.on('end', function () {
// console.log((jsonString));
callback(JSON.stringify(jsonString));
});
gunzip.on('error', function (e) {
console.log(e);
});
}
else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed');
} }
else { }
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed', {data: b});
}
}
}); }
else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed', {data: b});
}
}
});
} }
router.get('/list', function (req, res) { router.get('/list', function (req, res) {
logger.debug('list..'); logger.debug('list..');
dbCouch.view('titles', 'titles', function (err, body) { dbCouch.view('titles', 'titles', function (err, body) {
if (!err) { if (!err) {
var outJSON = []; var outJSON = [];
body.rows.forEach(function (doc) { body.rows.forEach(function (doc) {
outJSON.push({id: doc.id, title: doc.value}); outJSON.push({id: doc.id, title: doc.value});
}); });
//logger.debug(util.inspect(body)); //logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"}); res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify({list: outJSON})); res.end(JSON.stringify({list: outJSON}));
} }
else { else {
res.writeHead(500, {"ContentType": "application/json"}); res.writeHead(500, {"ContentType": "application/json"});
res.end(JSON.stringify({})); res.end(JSON.stringify({}));
} }
}); });
}); });
router.get('/entry/:id', function (req, res) { router.get('/entry/:id', function (req, res) {
logger.debug('entry..'); logger.debug('entry..');
logger.debug(req.params.id); logger.debug(req.params.id);
dbCouch.get(req.params.id, function (err, body) { dbCouch.get(req.params.id, function (err, body) {
if (!err) { if (!err) {
var outJSON = {}; var outJSON = {};
outJSON._id = body._id; outJSON._id = body._id;
outJSON._rev = body._rev; outJSON._rev = body._rev;
outJSON.title = body.title; outJSON.title = body.title;
outJSON.reduced = body.reduced; outJSON.reduced = body.reduced;
outJSON.url = body.url; outJSON.url = body.url;
outJSON.tags = body.tags || {solid: '', list: []}; outJSON.tags = body.tags || {solid: '', list: []};
//logger.debug(util.inspect(body)); //logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"}); res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify(outJSON)); res.end(JSON.stringify(outJSON));
} }
else { else {
res.writeHead(500, {"ContentType": "application/json"}); res.writeHead(500, {"ContentType": "application/json"});
res.end(JSON.stringify({})); res.end(JSON.stringify({}));
} }
}); });
}); });
router.get('/tags', function (req, res) { router.get('/tags', function (req, res) {
logger.debug('tag list..'); logger.debug('tag list..');
logger.debug(req.params.id); logger.debug(req.params.id);
dbCouch.view('taglist', 'taglist', function (err, body) {
if (!err) {
logger.debug(body);
var outJSON = [];
body.rows.forEach(function (doc) {
logger.info(doc.value.taglist);
if (doc.value[0]==req.params.id)
{
outJSON = doc.value.taglist.sort();
}
});
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify({list: outJSON}));
dbCouch.view('taglist', 'taglist', function (err, body) {
if (!err) {
logger.debug(body);
var outJSON = [];
body.rows.forEach(function (doc) {
logger.info(doc.value.taglist);
if (doc.value[0] == req.params.id) {
outJSON = doc.value.taglist.sort();
} }
else { });
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"}); //logger.debug(util.inspect(body));
res.end(JSON.stringify({})); res.writeHead(200, {"ContentType": "application/json"});
} res.end(JSON.stringify({list: outJSON}));
});
}
else {
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"});
res.end(JSON.stringify({}));
}
});
}); });
router.get('/tags/:id', function (req, res) { router.get('/tags/:id', function (req, res) {
logger.debug('entry..'); logger.debug('entry..');
logger.debug(req.params.id); logger.debug(req.params.id);
dbCouch.view('getTagByKey', 'getTagByKey', function (err, body) {
if (!err) {
// logger.debug(body);
var outJSON = [];
body.rows.forEach(function (doc) {
// logger.debug(doc);
if (doc.value[0]==req.params.id)
{
outJSON.push({id: doc.id, title: doc.value[1]})
}
});
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify({list: outJSON}));
dbCouch.view('getTagByKey', 'getTagByKey', function (err, body) {
if (!err) {
// logger.debug(body);
var outJSON = [];
body.rows.forEach(function (doc) {
// logger.debug(doc);
if (doc.value[0] == req.params.id) {
outJSON.push({id: doc.id, title: doc.value[1]})
} }
else { });
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"}); //logger.debug(util.inspect(body));
res.end(JSON.stringify({})); res.writeHead(200, {"ContentType": "application/json"});
} res.end(JSON.stringify({list: outJSON}));
});
}
else {
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"});
res.end(JSON.stringify({}));
}
});
}); });
router.post('/add', function (req, res) { router.post('/add', function (req, res) {
logger.debug('add entry..'); logger.debug('add entry..');
var t = req.body; var t = req.body;
if (t.hasOwnProperty('url')) { if (t.hasOwnProperty('url')) {
var url = JSON.parse(t.url.toString()); var url = JSON.parse(t.url.toString());
logger.debug(url); logger.debug(url);
busEmitter.emit("getBookmark", t); busEmitter.emit("getBookmark", t);
} }
else { else {
logger.error('No data block!'); logger.error('No data block!');
} }
res.writeHead(200, {"ContentType": "application/json"}); res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify({adding: url})); res.end(JSON.stringify({adding: url}));
}); });
router.post('/tags', function (req, res) { router.post('/tags', function (req, res) {
var t = req.body; var t = req.body;
console.log(t); console.log(t);
logger.info('regetting:' + req.body._id); logger.info('regetting:' + req.body._id);
dbCouch.get(req.body._id, function (err, body) { dbCouch.get(req.body._id, function (err, body) {
if (!err) { if (!err) {
var obj = {}; var obj = {};
obj.url = body.url; obj.url = body.url;
obj.html = body.html; obj.html = body.html;
obj.reduced = body.reduced; obj.reduced = body.reduced;
obj.title = body.title; obj.title = body.title;
obj.tags = req.body.tags; obj.tags = req.body.tags;
logger.info('Updating...'); logger.info('Updating...');
busEmitter.emit("updateBookmarkData", obj, body._id, body._rev, res); busEmitter.emit("updateBookmarkData", obj, body._id, body._rev, res);
var outJSON = {}; var outJSON = {};
outJSON._id = body._id; outJSON._id = body._id;
outJSON._rev = body._rev; outJSON._rev = body._rev;
outJSON.title = body.title; outJSON.title = body.title;
outJSON.reduced = body.reduced; outJSON.reduced = body.reduced;
outJSON.url = body.url; outJSON.url = body.url;
outJSON.tags = req.body.tags; outJSON.tags = req.body.tags;
//logger.debug(util.inspect(body)); //logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"}); res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify(outJSON)); res.end(JSON.stringify(outJSON));
}
} else {
else { res.writeHead(500, {"ContentType": "application/json"});
res.writeHead(500, {"ContentType": "application/json"}); res.end(JSON.stringify({}));
res.end(JSON.stringify({})); }
} });
});
}); });
router.post('/redo', function (req, res) { router.post('/redo', function (req, res) {
logger.debug('redoing entry..'); logger.debug('redoing entry..');
var t = req.body; var t = req.body;
console.log(t); console.log(t);
if (t.hasOwnProperty('url')) { if (t.hasOwnProperty('url')) {
var url = t.url.toString(); var url = t.url.toString();
logger.debug(url); logger.debug(url);
busEmitter.emit("getBookmark", t); busEmitter.emit("getBookmark", t);
} }
else { else {
logger.error('No data block!'); logger.error('No data block!');
} }
res.writeHead(200, {"ContentType": "application/json"}); res.writeHead(200, {"ContentType": "application/json"});
res.end(JSON.stringify({adding: url})); res.end(JSON.stringify({adding: url}));
}); });
router.get('/new', function (req, res) { router.get('/new', function (req, res) {
logger.debug('Save new'); logger.debug('Save new');
busEmitter.emit("getBookmarkRes", req.query.url, res); busEmitter.emit("getBookmarkRes", req.query.url, res);
}); });
busEmitter.emit('updateTagsDB'); busEmitter.emit('updateTagsDB');
module.exports = router; module.exports = router;

0
server/processor.js Normal file
View File