Commit 03582197 by zhiwei

弹幕采集修改为由roomId修改为roomUrl,发现有很多链接中没有房间id信息

parent 65bd8bb3
......@@ -31,9 +31,9 @@ public class BilibiliClient {
* @param roomId
* @throws Exception
*/
public static void getDanmu(DataCallBack dataCallBack,String roomId) throws Exception {
public static void getDanmu(DataCallBack dataCallBack,String roomUrl) throws Exception {
//根据房间号获取真实房间号
RoomInfo roomInfo = BilibiliRoomInfoCrawler.getRoomInfoByRoomUrl(roomId);
RoomInfo roomInfo = BilibiliRoomInfoCrawler.getRoomInfoByRoomUrl(roomUrl);
if(Objects.nonNull(roomInfo)) {
//获取弹幕服务器地址
String url = "https://live.bilibili.com/api/player?id=cid:" + roomInfo.getRoomId();
......
......@@ -31,9 +31,9 @@ public class DouyuClient {
* @param roomId
* @throws Exception
*/
public static void getDanmu(DataCallBack dataCallBack, String roomId) throws Exception {
public static void getDanmu(DataCallBack dataCallBack, String roomUrl) throws Exception {
// 根据房间号获取真实房间号
RoomInfo roomInfo = DouYuRoomInfoCrawler.getRoomInfoByRoomUrl(roomId);
RoomInfo roomInfo = DouYuRoomInfoCrawler.getRoomInfoByRoomUrl(roomUrl);
if (Objects.nonNull(roomInfo)) {
// 建立弹幕连接
Connector.asynchronizedTcpConnect(new NioEventLoopGroup(), HOST, PORT, new IdleStateHandler(0, 30, 45),
......
package com.zhiwei.live.danmu.pandam;
import java.util.Objects;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.crawler.core.HttpBoot;
import com.zhiwei.crawler.core.RequestUtils;
import com.zhiwei.live.bean.RoomInfo;
import com.zhiwei.live.danmu.util.Connector;
import com.zhiwei.live.danmu.util.DataCallBack;
import com.zhiwei.live.roominfo.PandamTVRoomInfoCrawler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.timeout.IdleStateHandler;
......@@ -27,32 +31,38 @@ public class PandamClient {
* @param roomId
* @throws Exception
*/
public static void getDanmu(DataCallBack dataCallBack,String roomId) throws Exception {
public static void getDanmu(DataCallBack dataCallBack,String roomUrl) throws Exception {
//根据房间号获取弹幕服务器地址
Request request = RequestUtils.wrapGet("http://riven.panda.tv/chatroom/getinfo?roomid=" + roomId);
String host = null;
int port = -1;
JSONObject json = null;
try(Response response = httpBoot.syncCall(request)) {
json = JSON.parseObject(response.body().string());
if(json.getInteger("errno") != 0) {
throw new IllegalStateException("房间号: " + roomId + " 不存在");
}
json = json.getJSONObject("data");
String[] address = json.getJSONArray("chat_addr_list").getString(0).split(":");
host = address[0];
port = Integer.parseInt(address[1]);
} catch(Exception e) {
throw new IllegalArgumentException("获取聊天服务器地址失败", e);
}
String appid = json.getString("appid");
String rid = json.getString("rid");
String sign = json.getString("sign");
String authType = json.getString("authType");
String ts = json.getString("ts");
Connector.asynchronizedTcpConnect(new NioEventLoopGroup(), host, port,
new IdleStateHandler(0, 30, 45), new PandamMessageHandler(dataCallBack, appid, rid, ts, sign, authType)).sync();
RoomInfo roomInfo = PandamTVRoomInfoCrawler.getRoomInfoByRoomUrl(roomUrl);
if(Objects.nonNull(roomInfo)) {
String roomId = roomInfo.getRoomId();
Request request = RequestUtils.wrapGet("http://riven.panda.tv/chatroom/getinfo?roomid=" + roomId);
String host = null;
int port = -1;
JSONObject json = null;
try(Response response = httpBoot.syncCall(request)) {
json = JSON.parseObject(response.body().string());
if(json.getInteger("errno") != 0) {
throw new IllegalStateException("房间号: " + roomUrl + " 不存在");
}
json = json.getJSONObject("data");
String[] address = json.getJSONArray("chat_addr_list").getString(0).split(":");
host = address[0];
port = Integer.parseInt(address[1]);
} catch(Exception e) {
throw new IllegalArgumentException("获取聊天服务器地址失败", e);
}
String appid = json.getString("appid");
String rid = json.getString("rid");
String sign = json.getString("sign");
String authType = json.getString("authType");
String ts = json.getString("ts");
Connector.asynchronizedTcpConnect(new NioEventLoopGroup(), host, port,
new IdleStateHandler(0, 30, 45), new PandamMessageHandler(dataCallBack, appid, rid, ts, sign, authType)).sync();
}else {
}
}
}
......@@ -8,7 +8,6 @@ public class BilibiliDanMuTest {
public static void main(String[] args) throws InterruptedException {
String roomUrl = "https://live.bilibili.com/139";
String roomId = roomUrl.replaceAll("https://live.bilibili.com/", "");
try {
BilibiliClient.getDanmu(new DataCallBack() {
@Override
......@@ -18,7 +17,7 @@ public class BilibiliDanMuTest {
System.out.println("-------------" + bilibiliMessage.toString());
}
}
},roomId);
}, roomUrl);
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -7,8 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack;
public class DouYuDanMuTest {
public static void main(String[] args) throws InterruptedException {
String roomUrl = "https://live.bilibili.com/139";
String roomId = roomUrl.replaceAll("https://live.bilibili.com/", "");
String roomUrl = "https://www.douyu.com/4372875";
try {
DouyuClient.getDanmu(new DataCallBack() {
@Override
......@@ -18,7 +17,7 @@ public class DouYuDanMuTest {
System.out.println("-------------" + douyuMessage.toString());
}
}
}, roomId);
}, roomUrl);
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -7,7 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack;
public class PanDamDanMuTest {
public static void main(String[] args) throws Exception {
String roomId = "337852";
String roomUrl = "https://www.panda.tv/337852";
try {
PandamClient.getDanmu(new DataCallBack() {
@Override
......@@ -17,11 +17,9 @@ public class PanDamDanMuTest {
System.out.println("-------------" + pandamMessage.toString());
}
}
}, roomId);
}, roomUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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