Commit 03582197 by zhiwei

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

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