Sha256: dcc2d0f2d23e21fdcb481f98d12db1d7dc9dbd7dd9ae79a809016ef5e32ada91

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'open-uri'
require 'rss'

module Cinch
  module Plugins
    class Rss
      include Cinch::Plugin

      set :plugin_name, 'rss'
      set :help, <<EOT
The RSS plugin will poll rss feed every options['cogconf']['rss']['polling'] minutes
EOT

      def initialize(bot)
        super
        @bot = bot
        @feeds = {}
        timer = Timer @bot.config.options['cogconf']['rss']['polling'], method: :fetch_rss
        timers << timer
      end

      def fetch_rss
        @bot.debug 'Fetching rss'
        @feeds ||= {}
        @bot.config.options['cogconf']['rss']['channels'].each do |chan|
          prefix = chan['prefix']
          open(chan['url']) do |rss|
            feed = RSS::Parser.parse(rss)
            @feeds[prefix] ||= []
            feed.items.reverse.each do |item|
              unless @feeds[prefix].include? item.link
                @feeds[prefix] << item.link
                # to prevent the first run displays all the items
                if @feeds[prefix].length > feed.items.length
                  chan['announce'].each do |announce|
                    Channel(announce).send "#{prefix} #{item.title} (#{item.link})"
                  end
                end
              end
            end
            if @feeds[prefix].length > feed.items.length
              @feeds[prefix] = @feeds[prefix][-feed.items.length, feed.items.length]
            end
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogbot-0.1.7 plugins/rss.rb