From 4d8cdbe8d6e5a028879262b6f2f365d637475a20 Mon Sep 17 00:00:00 2001 From: Michael Jett Date: Tue, 29 Dec 2015 15:45:53 -0500 Subject: [PATCH 1/2] Better way to discover new entries --- src/watcher.coffee | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/watcher.coffee b/src/watcher.coffee index 8f7610d..b8cd2b3 100644 --- a/src/watcher.coffee +++ b/src/watcher.coffee @@ -8,6 +8,7 @@ {EventEmitter} = require 'events' parser = require 'parse-rss' +crypto = require('crypto'); class Watcher extends EventEmitter @@ -15,8 +16,8 @@ class Watcher extends EventEmitter throw new Error("arguments error.") if not feedUrl or feedUrl is undefined @feedUrl = feedUrl @interval = null - @lastPubDate = null - @lastPubTitle = null + @hashCache = []; + @initialized = false; @timer = null @watch = => @@ -25,11 +26,14 @@ class Watcher extends EventEmitter return @emit 'error', err if err for article in articles - if (@lastPubDate is null and @lastPubTitle is null) or - (@lastPubDate <= article.pubDate/1000 and @lastPubTitle isnt article.title) - @emit 'new article',article - @lastPubDate = article.pubDate / 1000 - @lastPubTitle = article.title + hash = crypto.createHash('md5').update(JSON.stringify(article)).digest('hex'); + if (@hashCache.indexOf(hash) == -1) + @emit 'new article',article if @initialized + @hashCache.unshift(hash); + # Free up memory + @hashCache.pop() if @hashCache.length > 100; + + @initialized = true return setInterval -> fetch(@feedUrl) @@ -52,7 +56,6 @@ class Watcher extends EventEmitter initialize = (callback)=> request @feedUrl,(err,articles)=> return callback new Error(err),null if err? and callback? - @lastPubDate = articles[articles.length-1].pubDate / 1000 @timer = @watch() return callback null,articles if callback? From fc769a25ac9417a541a314f497df48a69b68b641 Mon Sep 17 00:00:00 2001 From: Michael Jett Date: Tue, 29 Dec 2015 17:22:59 -0500 Subject: [PATCH 2/2] Changing hash values --- src/watcher.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/watcher.coffee b/src/watcher.coffee index b8cd2b3..1ea9075 100644 --- a/src/watcher.coffee +++ b/src/watcher.coffee @@ -26,7 +26,10 @@ class Watcher extends EventEmitter return @emit 'error', err if err for article in articles - hash = crypto.createHash('md5').update(JSON.stringify(article)).digest('hex'); + hash = crypto.createHash('md5').update(JSON.stringify({ + "date": article.pubDate, + "title": article.title + })).digest('hex'); if (@hashCache.indexOf(hash) == -1) @emit 'new article',article if @initialized @hashCache.unshift(hash);