Sha256: fbdb44f3105c20c3206e17c03936b2d094eb550e8242fabc6597709866ed33d9

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'rss'

module Fluent
  class RSSInput < Fluent::Input
    Plugin.register_input 'rss', self

    config_param :tag, :string
    config_param :url, :string
    config_param :interval, :time, default: '5m'
    config_param :attrs, :string, default: 'date, title, description'

    def configure(conf)
      super

      @attrs = @attrs.split(',').map {|attr| attr.strip }
      @current_time = Time.now
    end

    def start
      @thread = Thread.new(&method(:run))
    end

    def shutdown
      Thread.kill(@thread)
    end

    def run
      loop do
        Thread.new(&method(:emit_rss))
        sleep @interval
      end
    end

    def emit_rss
      begin
        next_current_time = @current_time
        rss = RSS::Parser.parse(@url)
        rss.items.each do |item|
          record = {}
          @attrs.each do |attr|
            record[attr] = item.send(attr) if item.send(attr)
          end
          time = Time.parse item.date.to_s
          if time > @current_time
            Fluent::Engine.emit @tag, Time.parse(item.date.to_s), record
            next_current_time = time if time > next_current_time
          end
        end
        @current_time = next_current_time
      rescue => e
        log.error e
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-rss-0.0.1 lib/fluent/plugin/in_rss.rb