Class: NicovideoLiveBrodcast

Inherits:
NicovideoAccount show all
Defined in:
lib/comment_move.rb

Constant Summary

Constants included from CommentMove

CommentMove::VERSION

Instance Attribute Summary

Attributes inherited from NicovideoAccount

#password, #user_session, #username

Instance Method Summary collapse

Methods inherited from NicovideoAccount

#create_usersission

Methods included from CommentMove

#get_playerstatus, #get_waybackkey, #login

Instance Method Details

#get_comment(lv) ⇒ Object

[View source]

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/comment_move.rb', line 17

def get_comment(lv)
  lv = lv
  playerstatus = get_playerstatus(lv, @user_session)
  waybackkey = get_waybackkey(playerstatus[:thread], @user_session)

  data = {
    thread: playerstatus[:thread],
    version: 20061206,
    res_from: -1000,
    waybackkey: waybackkey,
    user_id: playerstatus[:user]
  }

  comments = []
  TCPSocket.open(playerstatus[:addr], playerstatus[:port]) do |socket|
    req = "<thread #{data.map {|k,v| "#{k}=\"#{v}\"" }.join(' ')}/>\0"
    socket.write(req)

    loop do
      stream = socket.gets("\0")
      xml = Nokogiri::XML(stream)

      next if xml.xpath('//chat').empty?
      break if xml.text == '/disconnect' && xml.xpath('/chat').attr('premium').text == '2'

      h = xml.xpath('/chat').first.attributes.map{|k,v| [k.to_sym,v.text] }.to_h
      h.delete_if {|k,_| [:thread, :vpos, :date_usec, :locale, :name, :mail].include?(k) }

      h[:type] ||= nil
      h[:premium] ||= 0
      h[:anonymity] ||= false
      h[:comment] = xml.text.gsub(/[\r\n]/, '')

      h.each do |k,v|
        case k
        when :no
          h[k] = v.to_i
        when :anonymity
          h[k] = true if v == '1'
        when :date
          h[k] = Time.at(v.to_i)
        when :premium
          case v.to_i
          when 0
            h[k] = false
            h[:type] = 'user'
          when 1
            h[k] = true
            h[:type] = 'user'
          when 2
            h[k] = nil
            h[:type] = 'system'
          when 3
            h[k] = nil
            h[:type] = 'operator'
          end
        end
      end

      comments.push(h)
    end
  end
  comments
end