Commit 9c5bb9dc authored by yPhil's avatar yPhil
Browse files

Saving, passing & testing lastItem

parent cf74aa76
Pipeline #411558 passed with stage
in 49 seconds
......@@ -99,16 +99,28 @@ PTL.feed = {
PTL.feed.populate($(this), progress);
});
const $feedControls = $('<div>').attr('class', 'feedControls dataStore')
.data('index', feedIndex)
.data('url', url)
.data('name', name)
.data('type', type)
.data('limit', limit)
.data('status', status)
.data('iconhash', iconhash)
.data('nbitems', nbitems)
.data('lastitem', lastitem);
// const $feedControls = $('<div>').attr('class', 'feedControls dataStore')
// .data('index', feedIndex)
// .data('url', url)
// .data('name', name)
// .data('type', type)
// .data('limit', limit)
// .data('status', status)
// .data('iconhash', iconhash)
// .data('nbitems', nbitems)
// .data('lastitem', lastitem);
const $feedControls = $('<div>')
.attr('class', 'feedControls dataStore')
.attr('data-index', feedIndex)
.attr('data-url', url)
.attr('data-name', name)
.attr('data-type', type)
.attr('data-limit', limit)
.attr('data-status', status)
.attr('data-iconhash', iconhash)
.attr('data-nbitems', nbitems)
.attr('data-lastitem', lastitem);
if ($feedControls.data('status') == 'on') {
$refreshIcon.removeClass('icon-pin')
......@@ -188,8 +200,6 @@ PTL.feed = {
},
build:function(data, $dataStore) {
console.log('YOLOOO: %s (%s)');
const $refreshButton = $dataStore.find('i.feedRefresh'),
$feedHeader = $dataStore.parent(),
$panel = $dataStore.parent().parent().parent(),
......@@ -537,14 +547,24 @@ PTL.feed = {
$refreshButton.addClass('spin');
$feedLink.removeClass('danger');
if (typeof localStorage.getItem(feedUrl) === 'undefined') {
$feedBody.append(localStorage.getItem(feedUrl));
var saved = localStorage.getItem(feedUrl);
if (saved) {
console.log('YOZ: %s (%s)');
$feedBody.append(saved);
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp))
.removeClass('spin');
} else {
var lastItem = $dataStore.data('lastitem');
$.get("/feed", {
url: feedUrl,
count: 3,
dataType: 'json'
dataType: 'json',
lastItem: lastItem
}, function() {
// $feedBodyUl.empty();
......@@ -553,8 +573,6 @@ PTL.feed = {
PTL.util.say(PTL.tr('Problem reading feed [%1] Error type [%2]', feedUrl, error), 'error');
}).done(function(data) {
// console.log('data: %s (%s)', JSON.stringify(data));
if (feedName) {
feedTitle = feedName;
} else if (data.feedTitle) {
......@@ -562,9 +580,9 @@ PTL.feed = {
$dataStore.data('name', feedTitle);
}
// if (data.lastGuid) {
// $dataStore.data('lastitem', data.lastGuid);
// }
if (data.lastItem) {
$dataStore.data('lastitem', data.lastItem);
}
$feedLink.text(feedTitle)
.attr('href', data.feedLink)
......@@ -582,14 +600,19 @@ PTL.feed = {
}
$feedBody.append(PTL.feed.build(data, $dataStore));
var $feedBodyUl = PTL.feed.build(data, $dataStore);
$feedBody.append($feedBodyUl);
localStorage.setItem(feedUrl, $feedBodyUl.prop('outerHTML'));
}).always(function() {
$refreshButton.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp));
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp))
.removeClass('spin');
if (progress) progress.increment();
$refreshButton.removeClass('spin');
});
......
......@@ -34,7 +34,7 @@ function getParams(str) {
return params;
}
function getFeed (feedUrl, count, callback) {
function getFeed (feedUrl, lastItem, callback) {
// Get a response stream
fetch(feedUrl, {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
......@@ -53,7 +53,7 @@ function getFeed (feedUrl, count, callback) {
var responseStream = res.body;
responseStream = maybeTranslate(responseStream, charset);
responseStream.pipe(feedparser);
var lastGuid;
var newLastItem;
var i = 0;
......@@ -66,24 +66,20 @@ function getFeed (feedUrl, count, callback) {
try {
var item = this.read();
if (item !== null && item.guid) {
console.error('### ITEM: %s (%s)', item.guid || 'NOTHING', feedUrl);
} else {
console.error('### NOPE: (%s)', feedUrl);
}
// console.error('ITEM: %s (%s)', JSON.stringify(item));
if (item !== null){
i++;
if (typeof lastGuid === 'undefined') {
lastGuid = item.guid;
if (typeof newLastItem === 'undefined') {
newLastItem = item.link;
}
if (i < count) {
console.error('### PUSHING: #%s GUID: %s (count is %s)', i, item.guid || 'No guid', count);
if (item.link !== lastItem) {
console.error('### PUSHING: #%s GUID: %s', i, lastItem);
feedItems.push(item);
} else {
console.error('### Count reached i:%s, count:%s, LAST: [%s]', i, count, lastGuid);
console.error('### Count reached i:%s, lastItem: [%s]', i, lastItem);
this.resume();
}
......@@ -97,7 +93,7 @@ function getFeed (feedUrl, count, callback) {
}).on ('end', function () {
var meta = this.meta;
resolve();
return callback (null, feedItems, meta.title || 'Untitled', meta.link || feedUrl, lastGuid);
return callback (null, feedItems, meta.title || 'Untitled', meta.link || feedUrl, lastItem);
});
});
......
......@@ -70,14 +70,14 @@ router.use(morgan('combined'));
router.get('/feed', function(req, res) {
feeder.getFeed(req.query.url, req.query.count, function (err, feedItems, feedTitle, feedLink, lastGuid) {
feeder.getFeed(req.query.url, req.query.lastItem, function (err, feedItems, feedTitle, feedLink, lastItem) {
if (feedItems && !res.headersSent) {
res.send({
feedItems: feedItems,
feedLink: feedLink,
feedTitle: feedTitle,
lastGuid: lastGuid
lastItem: lastItem
});
} else if (!res.headersSent) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment