Skip to content

Commit

Permalink
added basic config options for the bot
Browse files Browse the repository at this point in the history
added optional truncation of old feed entries
  • Loading branch information
universal committed May 12, 2008
1 parent 97e318c commit 93e4395
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
21 changes: 15 additions & 6 deletions leaves/rss_reader.rb
Expand Up @@ -4,7 +4,18 @@
class RssReader < Autumn::Leaf

before_filter :authenticate, :only => [:reload, :quit, :addFeed, :removeFeed, :silence]


attr_reader :truncate, :update_interval, :get_rss

# load config for rss reader
def will_start_up
config = {}
if File.exists?('leaves/rss_reader_config.yml')
config = YAML.load(File.open('leaves/rss_reader_config.yml'))
end
@truncate = config['truncate'] || false
@update_interval = config['update_interval'] || 120
end
# send self a message to initialize the background task for retrieving
# new feed entries
def did_start_up
Expand Down Expand Up @@ -38,7 +49,6 @@ def listFeeds_command(stem, sender, reply_to, msg)
end

def silence_command(stem, sender, reply_to, msg)
logger.info reply_to
feeds = Feed.all :channel => reply_to, :server => server_identifier(stem)
sil = !feeds.first.silence
feeds.each{|f| f.update_attributes(:silence => sil)}
Expand All @@ -49,7 +59,7 @@ def silence_command(stem, sender, reply_to, msg)
end
stem.message response, reply_to
end

def initrss_command(stem, sender, reply_to, msg)
return unless (sender[:nick] == "universal" || sender[:nick] == stem.nickname)
if "stop" == msg
Expand Down Expand Up @@ -86,15 +96,14 @@ def server_identifier(stem)
def rssGetter
while @get_rss
updateRss
sleep 25.0
sleep @update_interval
end
end

def updateRss
stems.each do |stem|
logger.info "rss update running"
feeds = Feed.all :server => server_identifier(stem), :silence => false
feeds.each{|f| f.update_feed_data}
feeds.each{|f| f.update_feed_data @truncate}
feeds.each do |feed|
if feed.new_from_last_update.size > 0
(feed.new_from_last_update.size - 1).downto 0 do |i|
Expand Down
3 changes: 3 additions & 0 deletions leaves/rss_reader_config.yml
@@ -0,0 +1,3 @@
---
truncate: true
update_interval: 60
7 changes: 5 additions & 2 deletions support/rss_reader/feed.rb
Expand Up @@ -16,7 +16,7 @@ class Feed < DataMapper::Base

# get new feed entries for this feed and collect new ones in
# <code>:new_from_last_update</code>
def update_feed_data
def update_feed_data(truncate = false)
self.new_from_last_update = []
# Parse the feed, dumping its contents to rss
logger.info "help me plx, i'm nil" if self.nil?
Expand All @@ -34,6 +34,9 @@ def update_feed_data
end
end
end
42
if truncate && last = self.new_from_last_update.last
fds = FeedData.all :pubDate.lt => last.pubDate, :feed_id => self.id
fds.each{|fd| fd.destroy!}
end
end
end

0 comments on commit 93e4395

Please sign in to comment.