Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/watcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

{EventEmitter} = require 'events'
parser = require 'parse-rss'
crypto = require('crypto');

class Watcher extends EventEmitter

constructor:(feedUrl)->
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 = =>

Expand All @@ -25,11 +26,17 @@ 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({
"date": article.pubDate,
"title": article.title
})).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)
Expand All @@ -52,7 +59,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?

Expand Down