Class: Fluent::MixiCommunityInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_mixi_community.rb

Instance Method Summary collapse

Instance Method Details

#configure(config) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fluent/plugin/in_mixi_community.rb', line 27

def configure(config)
  super
  @thread_title_pattern = Regexp.new(@thread_title_pattern, {}, 'n')

   = Pit.get(@pit_id, require: {
    'email' => 'mail',
    'password' => 'password',
  })
  @fetcher = Mixi::Community::Fetcher.new(
    ['email'],
    ['password']
  )
  @community = Mixi::Community.new(@community_id)
  # {[community_id, bbs_id] => last_comment_id}
  @last_comment_ids = {}
end

#fetch_and_emitObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/in_mixi_community.rb', line 66

def fetch_and_emit
  @community.fetch(@fetcher)
  @community.recent_bbses[0...@recent_threads_num].select{|th| th.title =~ @thread_title_pattern}.each do|th|
    sleep 1
    th.fetch(@fetcher)
    th.recent_comments.each do|comment|
      last_comment_id = @last_comment_ids[[@community.id, th.id]]
      next if last_comment_id && comment.id <= last_comment_id
      @last_comment_ids[[@community.id, th.id]] = comment.id

      next if @silent_startup && @first_time

      Fluent::Engine.emit(@tag, Fluent::Engine.now, {
        'community' => {
          'id' => @community.id,
        },
        'thread' => {
          'id' => th.id,
          'title' => th.title,
        },
        'comment' => {
          'id' => comment.id,
          'num' => comment.num,
          'user_name' => comment.user_name,
          'body_text' => comment.body_text,
        }
      })
    end
  end
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/in_mixi_community.rb', line 52

def run
  @first_time = true
  loop do
    begin
      fetch_and_emit
    rescue StandardError, Timeout::Error
      $log.error("mixi_community(community_id=#{@community_id}): #{$!.inspect}")
      $log.error_backtrace
    end
    @first_time = false
    sleep @interval_sec
  end
end

#shutdownObject



48
49
50
# File 'lib/fluent/plugin/in_mixi_community.rb', line 48

def shutdown
  @thread.kill
end

#startObject



44
45
46
# File 'lib/fluent/plugin/in_mixi_community.rb', line 44

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