Class: DanmuClient

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/danmu/clients/danmu_client.rb

Constant Summary collapse

DANMU_SERVER =
"danmu.douyutv.com"
DANMU_PORT =
8602

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger, logger=

Constructor Details

#initialize(room, auth_dst_ip, auth_dst_port) ⇒ DanmuClient

Returns a new instance of DanmuClient.



19
20
21
22
23
24
25
# File 'lib/danmu/clients/danmu_client.rb', line 19

def initialize(room,auth_dst_ip,auth_dst_port)
  @room = room
  @room_id = room.room_id
  @auth_dst_ip = auth_dst_ip
  @auth_dst_port = auth_dst_port
  @dev_id = SecureRandom.uuid.to_s.gsub('-','').upcase 
end

Instance Method Details

#align_left_str(raw_str, max_length, filled_chr) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/danmu/clients/danmu_client.rb', line 199

def align_left_str(raw_str,max_length,filled_chr)
  my_length = 0
  for i in 0...raw_str.size
    if raw_str[i].ord > 127 || raw_str[i].ord <=0
      my_length += 1
    end
    my_length += 1
  end
  if (max_length - my_length) > 0
    raw_str + filled_chr * ( max_length - my_length )
  else
    raw_str
  end
end

#do_keepliveObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/danmu/clients/danmu_client.rb', line 85

def do_keeplive
  Thread.new do
    loop do
      #puts "--> KeepAlive"
      send_keeplive_msg
      sleep 40
      @danmu_socket.write message("type@=keeplive/tick@=" + timestamp + "/")
    end
  end
end

#do_loginObject



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
# File 'lib/danmu/clients/danmu_client.rb', line 40

def 
  @danmu_auth_socket = TCPSocket.new @auth_dst_ip,@auth_dst_port
  @danmu_socket = TCPSocket.new DANMU_SERVER,DANMU_PORT
  logger.info("初始化DAMMU_SOCKET和DANMU_AUTH_SOCKET")
  send_auth_loginreq_msg
  str = @danmu_auth_socket.recv(4000)
   #puts "<>1<><"+str
  if str.include? "live_stat@=0"
    @live_stat = "离线"
  elsif str.include? "live_stat@=1"
    @live_stat = "在线"
    @username= str[/\/username@=(.+)\/nickname/,1]
     #puts "----" + @username
    str = @danmu_auth_socket.recv(4000)
     #puts "<>2<><"+str
    @gid = str[/\/gid@=(\d+)\//,1]
    @weight = str[/\/weight@=(\d+)\//,1]
    @fans_count = str[/\/fans_count@=(\d+)\//,1]
    #puts "<->" + @live_stat.to_s + @weight.to_s + @fans_count.to_s
     #puts @gid + "----"
    send_qrl_msg
    str = @danmu_auth_socket.recv(4000)
     #puts "<>3<><"+str
    str = @danmu_auth_socket.recv(4000)
     #puts "<>4<><"+str
    send_keeplive_msg
    str = @danmu_auth_socket.recv(4000)
     #puts "<>5<><"+str
    data = "type@=loginreq/username@="+@username+"/password@=1234567890123456/roomid@=" + @room_id.to_s + "/"
    all_data = message(data)
    @danmu_socket.write all_data
    #puts all_data
    str = @danmu_socket.recv(4000)
     #puts str
    send_join_group_msg 
    logger.info("发送心跳包")
    do_keeplive
  end

end

#do_mainObject



31
32
33
34
35
36
37
# File 'lib/danmu/clients/danmu_client.rb', line 31

def do_main
  logger.info("准备登陆认证")
  
  print_room_status
  logger.info("准备获取弹幕")
  loop { get_danmu  }
end

#get_danmuObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/danmu/clients/danmu_client.rb', line 126

def get_danmu
  danmu_data = @danmu_socket.recv(4000).force_encoding("UTF-8")
  if not danmu_data.include? "type@="
    logger.info "非消息"
  elsif danmu_data.include? "type@=error"
    logger.info "弹幕认证超时"
  else
    #logger.info(danmu_data)
    #puts danmu_data
    type = danmu_data[danmu_data.index("type@=")..-3]
    str = type.gsub('@S','/').gsub('@A=',':').gsub('@=',':').encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
    type = str[/type:(.+?)\//,1]
    #puts type
    if type == "chatmessage"
      type_zh = "弹幕"
      sender_id = str[/\/sender:(.+?)\//,1]
      nickname = str[/\/snick:(.+?)\//,1]
      content =  str[/\/content:(.+?)\//,1]
      strength = str[/\/strength:(.+?)\//,1]
      level = str[/\/level:(\d+?)\//,1]
      time  = Time.now.to_s
      puts "|" + type_zh + "| " + align_left_str(nickname,20," ") + align_left_str("<Lv:#{level}>",8," ") + align_left_str("(#{sender_id})",13," ") + align_left_str("[#{strength}]",10," ") + "@ #{time}: #{content} "
    elsif type == "userenter"
      type_zh = "入房"
      user_id = str[/\/:id:(.+?)\//,1]
      nickname = str[/\/nick:(.+?)\//,1]
      strength = str[/\/strength:(.+?)\//,1]
      time  = Time.now.to_s
      level = str[/\/level:(\d+)\//,1]
      puts "|" + type_zh + "| " + align_left_str(nickname,20," ") + align_left_str("<Lv:#{level}>",8," ") + align_left_str("(#{user_id})",13," ") + align_left_str("[#{strength}]",10," ") + "@ #{time}"
      #puts "|#{type_zh}| #{nickname}  <Lv:#{level}> (#{user_id}) [#{strength}] @ #{time}"
    elsif type == "dgn"
      type_zh = "未知"
      level = str[/\/level:(\d+?)\//,1]
      user_id = str[/\/sid:(.+?)\//,1]
      nickname = str[/\/src_ncnm:(.+?)\//,1]
      hits = str[/\/hits:(.+?)\//,1]
      time  = Time.now.to_s
      puts "|" + type_zh + "| " + align_left_str(nickname,20," ") + align_left_str("<Lv:#{level}>",8," ") + align_left_str("(#{user_id})",13," ") + align_left_str("[#{"Unknown"}]",10," ") + "@ #{time}: #{hits} hits "
      #puts "|#{type_zh}| #{nickname}  <Lv:#{level}> (#{user_id}) @ #{time}"
    end

  end
end

#message(content) ⇒ Object



81
82
83
# File 'lib/danmu/clients/danmu_client.rb', line 81

def message(content)
  Message.new(content).to_s
end


184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/danmu/clients/danmu_client.rb', line 184

def print_room_status()
  puts "========================================="
  puts "= Room Infomation                       ="
  puts "========================================="
  puts "= 房间: #{@room.room_name}(#{@room.room_id})"
  puts "= 主播: #{@room.owner_name}"
  puts "= 公告: #{@room.room_gg_show}"
  puts "= 标签: #{@room.room_tags}"
  puts "= 在线: #{@live_stat}"
  puts "= 粉丝: #{@fans_count}"
  puts "= 财产: #{@weight}"
  puts "= DANMU IP DST : #{@auth_dst_ip}:#{@auth_dst_port}\t="
  puts "========================================="
end

#recvObject



171
172
173
# File 'lib/danmu/clients/danmu_client.rb', line 171

def recv
  @danmu_socket.recv(4000)
end

#send_auth_loginreq_msgObject



101
102
103
104
105
106
107
# File 'lib/danmu/clients/danmu_client.rb', line 101

def send_auth_loginreq_msg
    time = timestamp 
    vk = Digest::MD5.hexdigest(time + "7oE9nPEG9xXV69phU31FYCLUagKeYtsF" + @dev_id)
    data = "type@=loginreq/username@=/ct@=0/password@=/roomid@="+@room_id.to_s+"/devid@="+@dev_id + "/rt@="+timestamp+"/vk@="+vk+"/ver@=20150929/"
    msg = message(data)
    @danmu_auth_socket.write msg
end

#send_join_group_msgObject



114
115
116
117
118
# File 'lib/danmu/clients/danmu_client.rb', line 114

def send_join_group_msg
    data  = "type@=joingroup/rid@=" + @room_id.to_s + "/gid@="+@gid+"/"
    msg = message(data)
    @danmu_socket.write msg
end

#send_keeplive_msgObject



96
97
98
99
100
# File 'lib/danmu/clients/danmu_client.rb', line 96

def send_keeplive_msg
    data = "type@=keeplive/tick@=" + timestamp + "/vbw@=0/k@=19beba41da8ac2b4c7895a66cab81e23/"
    msg = message(data)
    @danmu_auth_socket.write msg
end

#send_loginreq_msgObject



109
110
111
112
113
# File 'lib/danmu/clients/danmu_client.rb', line 109

def send_loginreq_msg
    data = "type@=loginreq/username@="+@username+"/password@=1234567890123456/roomid@="+@room_id.to_s+"/"
    msg = message(data)
    @danmu_socket.write msg
end

#send_qrl_msgObject



120
121
122
123
124
# File 'lib/danmu/clients/danmu_client.rb', line 120

def send_qrl_msg
    data  = "type@=qrl/rid@=" + @room_id.to_s + "/"
    msg = message(data)
    @danmu_auth_socket.write msg
end

#startObject



27
28
29
# File 'lib/danmu/clients/danmu_client.rb', line 27

def start
  do_main
end

#stopObject



175
176
177
178
# File 'lib/danmu/clients/danmu_client.rb', line 175

def stop
  @danmu_socket.close
  @danmu_auth_socket.close
end

#timestampObject



180
181
182
# File 'lib/danmu/clients/danmu_client.rb', line 180

def timestamp
  Time.now.to_i.to_s
end