Commit fe6e0d5b by cwy

b站弹幕采集修改

parent 2a8c7a4d
......@@ -5,9 +5,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.crawler.core.HttpBoot;
import com.zhiwei.crawler.core.RequestUtils;
import com.zhiwei.live.bean.RoomInfo;
......@@ -36,13 +35,13 @@ public class BilibiliClient {
RoomInfo roomInfo = BilibiliRoomInfoCrawler.getRoomInfoByRoomUrl(roomUrl);
if(Objects.nonNull(roomInfo)) {
//获取弹幕服务器地址
String url = "https://live.bilibili.com/api/player?id=cid:" + roomInfo.getRoomId();
String url = "https://api.live.bilibili.com/room/v1/Danmu/getConf?room_id=" + roomInfo.getRoomId();
Request request = RequestUtils.wrapGet(url);
String host = null;
try(Response response = httpBoot.syncCall(request)) {
Document doc = Jsoup.parse(response.body().string());
String[] address = doc.select("dm_server_list").text().split(",");
host = address[0];
String result = response.body().string();
JSONObject json = JSONObject.parseObject(result);
host = json.getJSONObject("data").getJSONArray("host_server_list").getJSONObject(0).getString("host");
} catch(Exception e) {
throw new IllegalArgumentException("获取聊天服务器地址失败", e);
}
......
......@@ -64,17 +64,26 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
try {
ByteBuf buf = (ByteBuf) msg;
// System.out.println("收到消息: \n" + ByteBufUtil.prettyHexDump(buf));
System.out.println("收到消息: \n" + ByteBufUtil.prettyHexDump(buf));
byte[] bt = new byte[buf.writerIndex() - buf.readerIndex() - 16];
byte[] byd = new byte[16];
buf.readBytes(byd, 0, 16);
// System.out.println("bt == " + new String(bt));
System.out.println("byd === " + byd[0] + " == " + byd[4] + " == " + byd[6] + " == " + byd[8] + " == " + byd[12]);
String source = buf.toString(CharsetUtil.UTF_8);
Matcher matcher = pattern.matcher(source);
// System.out.println(source);
while(matcher.find()) {
JSONObject dataJson = JSONObject.parseObject(matcher.group());
BilibiliMessage bilibiliMessage = new BilibiliMessage(dataJson);
bilibiliMessage.setRoom_id(roomId);
dataCallBack.onData(bilibiliMessage);
// System.out.println(bilibiliMessage);
}
} catch (Exception e) {
e.printStackTrace();
}
}
ReferenceCountUtil.release(msg);
......
......@@ -7,7 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack;
public class BilibiliDanMuTest {
public static void main(String[] args) throws InterruptedException {
String roomUrl = "https://live.bilibili.com/139";
String roomUrl = "https://live.bilibili.com/529";
try {
BilibiliClient.getDanmu(new DataCallBack() {
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment