adding some extras to filtering

This commit is contained in:
Martin Donnelly 2016-04-26 10:06:09 +01:00
parent 168d08baa3
commit 9fcb85992b
2 changed files with 131 additions and 146 deletions

View File

@ -1,4 +1,4 @@
"use strict";
'use strict';
/**
* Created by Martin on 22/02/2016.
*/
@ -8,7 +8,7 @@ var http = require('http'), request = require('request'), cheerio = require(
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
'string');
var converter = require('html-to-markdown');
var zlib = require("zlib");
var zlib = require('zlib');
var log4js = require('log4js');
var logger = log4js.getLogger();
var URL = require('url');
@ -87,7 +87,9 @@ function cleaner(b) {
'#recipe-related-videos',
'#tnav',
'.footer',
'#tb-wrapper'
'#tb-wrapper',
'#comments',
'#menu'
];
@ -100,7 +102,7 @@ function cleaner(b) {
function insertBookmark(obj) {
logger.debug('Inserting into couch...');
// logger.info(util.inspect(obj));
// Logger.info(util.inspect(obj));
dbCouch.insert(obj, function(err, body, header) {
if (err) {
logger.error('Error inserting into couch');
@ -122,35 +124,35 @@ function updateBookmark(obj, _id, _rev) {
return;
} else {
logger.info('I think we updated ok...');
busEmitter.emit("updateTagsDB");
busEmitter.emit('updateTagsDB');
}
});
logger.debug('Update done..');
}
var doInsertBookmark = (obj) => {
// logger.info('sendSocket: ' + JSON.stringify(obj));
// Logger.info('sendSocket: ' + JSON.stringify(obj));
insertBookmark(obj);
};
var doUpdateBookmark = (obj, _id, _rev) => {
// logger.info('sendSocket: ' + JSON.stringify(obj));
// Logger.info('sendSocket: ' + JSON.stringify(obj));
updateBookmark(obj, _id, _rev);
};
var doGetBookmark = (obj) => {
// logger.info('sendSocket: ' + JSON.stringify(obj));
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(obj);
};
var doGetBookmarkRedo = (obj) => {
// logger.info('sendSocket: ' + JSON.stringify(obj));
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(obj);
};
var doGetBookmarkRes = (url, res) => {
logger.debug('doGetBookmarkRes');
// logger.info('sendSocket: ' + JSON.stringify(obj));
// Logger.info('sendSocket: ' + JSON.stringify(obj));
genericGrab(url, res);
};
@ -170,7 +172,7 @@ var doUpdateTagsDB = () => {
});
dbCouch.view('taglist', 'taglist', function(err, body) {
// logger.debug(body);
// Logger.debug(body);
if (!err) {
var outJSON = {};
@ -179,14 +181,12 @@ var doUpdateTagsDB = () => {
doSaveTagsDB(doc.value, masterList);
});
}
else {
} else {
logger.error('NO TAG LIST EXISTS');
}
});
}
else {
} else {
}
});
@ -221,8 +221,7 @@ busEmitter.on('getBookmarkRedo', doGetBookmarkRedo);
busEmitter.on('updateTagsDB', doUpdateTagsDB);
busEmitter.on('saveTagsDB', doSaveTagsDB);
function doEngadget(body, url)
{
function doEngadget(body, url) {
logger.info('GRABBING Engadget');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
@ -240,7 +239,7 @@ function doEngadget(body, url)
try {
tdihbody.find('IMG').each(function(i, elem) {
let s, src = $(this).attr("src");
let s, src = $(this).attr('src');
if (src !== null) {
if (!STRING(src).startsWith('http')) {
@ -254,7 +253,7 @@ function doEngadget(body, url)
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
$(this).attr("src", s);
$(this).attr('src', s);
}
});
@ -274,8 +273,7 @@ function doEngadget(body, url)
}
function doAndroidDeveloper(body, url)
{
function doAndroidDeveloper(body, url) {
logger.info('GRABBING AndroidDeveloper');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
@ -293,7 +291,7 @@ function doAndroidDeveloper(body, url)
try {
tdihbody.find('IMG').each(function(i, elem) {
let s, src = $(this).attr("src");
let s, src = $(this).attr('src');
if (src !== null) {
if (!STRING(src).startsWith('http')) {
@ -307,7 +305,7 @@ function doAndroidDeveloper(body, url)
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
$(this).attr("src", s);
$(this).attr('src', s);
}
});
@ -327,8 +325,7 @@ function doAndroidDeveloper(body, url)
}
function doReddit(body, url)
{
function doReddit(body, url) {
logger.info('GRABBING REDDIT');
var obj = {}, tdihbody, i, urlObj, urlPrefix;
@ -343,7 +340,7 @@ logger.info('GRABBING REDDIT');
});
logger.info('++++++');
// logger.debug(tdihbody.html());
// Logger.debug(tdihbody.html());
logger.debug(tdihbody.length);
tdihbody = cleaner(tdihbody);
@ -381,15 +378,14 @@ function genericProcessor(body, url) {
tdihbody = cleaner(tdihbody);
logger.debug(title);
}
else {
} else {
logger.warn('Using whole body');
// bah. nothing to reduce so just grab the body, tidy it and use that
// Bah. nothing to reduce so just grab the body, tidy it and use that
tdihbody = $('BODY');
if (tdihbody.length === 0) {
tdihbody = $(":root");
tdihbody = $(':root');
}
@ -398,14 +394,14 @@ function genericProcessor(body, url) {
logger.debug(title);
}
// logger.info(util.inspect(tdihbody));
// 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");
let s, src = $(this).attr('src');
console.log('!!!!' + src);
if (src !== null && typeof src !== 'undefined') {
@ -420,7 +416,7 @@ function genericProcessor(body, url) {
s = 'http://image.silvrtree.co.uk/900,fit/' + src;
$(this).attr("src", s);
$(this).attr('src', s);
}
});
@ -446,15 +442,14 @@ function processBody(body, url, _id, _rev) {
// try to find a body to grab
// 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++)
{
for (i = 0;i < specialHandlers.length;i++) {
if (urlObj.host === specialHandlers[i].url) {
flag = true;
obj = specialHandlers[i].fn(body,url);
@ -462,24 +457,23 @@ function processBody(body, url, _id, _rev) {
}
if (!flag) {
// do generic processing
// Do generic processing
obj = genericProcessor(body,url);
}
// logger.warn(obj.reduced);
// Logger.warn(obj.reduced);
obj.host = urlObj.host;
/* jsonfile.writeFile(jsonFile, obj, function (err) {
/* Jsonfile.writeFile(jsonFile, obj, function (err) {
console.error(err);
});*/
if (_id !== null) {
busEmitter.emit("updateBookmarkData", obj, _id, _rev);
}
else {
busEmitter.emit("saveBookmarkData", obj);
busEmitter.emit('updateBookmarkData', obj, _id, _rev);
} else {
busEmitter.emit('saveBookmarkData', obj);
}
return obj;
@ -492,8 +486,7 @@ function genericGrab(obj, res) {
if (typeof obj === 'string') {
logger.info(obj);
url = obj;
}
else {
} else {
url = obj.url;
_id = obj._id || null;
_ver = obj._rev || null;
@ -532,22 +525,20 @@ function genericGrab(obj, res) {
jsonString += chunk;
});
gunzip.on('end', function() {
// console.log((jsonString));
// Console.log((jsonString));
callback(JSON.stringify(jsonString));
});
gunzip.on('error', function(e) {
console.log(e);
});
}
else {
} else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed');
}
}
}
else {
} else {
var b = processBody(body, url, _id, _ver);
if (res != null) {
res.render('grabbed', {data: b});
@ -571,8 +562,7 @@ console.log(typeof obj.entry.tn);
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;
}
else {
} else {
obj.entry.tn = 'gfx/fm.png';
}
@ -584,9 +574,8 @@ console.log(typeof obj.entry.tn);
logger.debug(util.inspect(body));
logger.info(util.inspect(outJSON));
res.render('pocket', {data: outJSON});
}
else {
res.writeHead(500, {"ContentType": "application/json"});
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -605,13 +594,12 @@ router.get('/list', function (req, res) {
outJSON.push({id: doc.id, title: doc.value});
});
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
}
else {
res.writeHead(500, {"ContentType": "application/json"});
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -633,13 +621,12 @@ router.get('/entry/:id', function (req, res) {
outJSON.url = body.url;
outJSON.tags = body.tags || {solid: '', list: []};
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify(outJSON));
}
else {
res.writeHead(500, {"ContentType": "application/json"});
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -661,14 +648,13 @@ router.get('/tags', function (req, res) {
}
});
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
}
else {
} else {
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"});
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -682,23 +668,22 @@ router.get('/tags/:id', function (req, res) {
dbCouch.view('getTagByKey', 'getTagByKey', function(err, body) {
if (!err) {
// logger.debug(body);
// Logger.debug(body);
var outJSON = [];
body.rows.forEach(function(doc) {
// logger.debug(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"});
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({list: outJSON}));
}
else {
} else {
logger.error(err);
res.writeHead(500, {"ContentType": "application/json"});
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -712,12 +697,11 @@ router.post('/add', function (req, res) {
if (t.hasOwnProperty('url')) {
var url = JSON.parse(t.url.toString());
logger.debug(url);
busEmitter.emit("getBookmark", t);
}
else {
busEmitter.emit('getBookmark', t);
} else {
logger.error('No data block!');
}
res.writeHead(200, {"ContentType": "application/json"});
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({adding: url}));
});
@ -740,7 +724,7 @@ router.post('/tags', function (req, res) {
obj.tags = req.body.tags;
logger.info('Updating...');
busEmitter.emit("updateBookmarkData", obj, body._id, body._rev, res);
busEmitter.emit('updateBookmarkData', obj, body._id, body._rev, res);
var outJSON = {};
outJSON._id = body._id;
@ -750,13 +734,12 @@ router.post('/tags', function (req, res) {
outJSON.url = body.url;
outJSON.tags = req.body.tags;
//logger.debug(util.inspect(body));
res.writeHead(200, {"ContentType": "application/json"});
//Logger.debug(util.inspect(body));
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify(outJSON));
}
else {
res.writeHead(500, {"ContentType": "application/json"});
} else {
res.writeHead(500, {ContentType: 'application/json'});
res.end(JSON.stringify({}));
}
});
@ -771,30 +754,27 @@ router.post('/redo', function (req, res) {
if (t.hasOwnProperty('url')) {
var url = t.url.toString();
logger.debug(url);
busEmitter.emit("getBookmark", t);
}
else {
busEmitter.emit('getBookmark', t);
} else {
logger.error('No data block!');
}
res.writeHead(200, {"ContentType": "application/json"});
res.writeHead(200, {ContentType: 'application/json'});
res.end(JSON.stringify({adding: url}));
});
router.get('/new', function(req, res) {
logger.debug('Save new');
busEmitter.emit("getBookmarkRes", req.query.url, res);
busEmitter.emit('getBookmarkRes', req.query.url, res);
});
router.post('/new', function(req, res) {
logger.debug('Posted Save new');
logger.info(req.body);
if (Object.keys(req.body).length !== 0)
{
busEmitter.emit("getBookmarkRes", req.body.url, res);
}
else {
if (Object.keys(req.body).length !== 0) {
busEmitter.emit('getBookmarkRes', req.body.url, res);
} else {
res.status(422).end();
}

View File

@ -108,7 +108,12 @@
<%= data[i].entry.host %>
</div>
<div class="mui--text-body1 tags">
<%= data[i].entry.tags %>
<% if (typeof data[i].entry.tags !== 'undefined') { %>
TAGS:
<%=data[i].entry.tags.solid %>
<% } %>
</div>
</div>
</div>