Commit 87226592 by zhiwei

B站弹幕获取

parent fe6e0d5b
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<dependency> <dependency>
<groupId>com.zhiwei.crawler</groupId> <groupId>com.zhiwei.crawler</groupId>
<artifactId>crawler-core</artifactId> <artifactId>crawler-core</artifactId>
<version>0.1.1-RELEASE</version> <version>0.6.2.1-RELEASE</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
...@@ -25,7 +25,12 @@ ...@@ -25,7 +25,12 @@
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
<version>4.1.33.Final</version> <version>4.1.48.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
package com.zhiwei.live.bean; package com.zhiwei.live.bean;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class RoomInfo { public class RoomInfo {
String pt; // 平台类型 /**
* 平台类型
*/
String pt;
String roomId; // 房间号 /**
* 房间号
*/
String roomId;
String nickName; // 主播昵称 /**
* 主播昵称
*/
String nickName;
String roomName; // 房间名称 /**
* 房间名称
*/
String roomName;
Integer hotNum; // 直播间热度 /**
* 直播间热度
*/
Integer hotNum;
Integer fans; // 订阅数 /**
* 订阅数
*/
Integer fans;
public RoomInfo() { public RoomInfo() {
} }
...@@ -26,62 +49,4 @@ public class RoomInfo { ...@@ -26,62 +49,4 @@ public class RoomInfo {
this.fans = fans; this.fans = fans;
} }
@Override
public String toString() {
return "new RoomInfo[" + "pt = " + pt + ", roomId = " + roomId + ", roomName = " + roomName + ", nickName = "
+ nickName + ", hotNum = " + hotNum + ", fans = " + fans + "]";
}
public Integer getFans() {
return fans;
}
public void setHotNum(Integer hotNum) {
this.hotNum = hotNum;
}
public void setFans(Integer fans) {
this.fans = fans;
}
public String getPt() {
return pt;
}
public String getRoomId() {
return roomId;
}
public String getNickName() {
return nickName;
}
public String getRoomName() {
return roomName;
}
public int getHotNum() {
return hotNum;
}
public void setPt(String pt) {
this.pt = pt;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
public void setHotNum(int hotNum) {
this.hotNum = hotNum;
}
} }
...@@ -3,12 +3,12 @@ package com.zhiwei.live.danmu.bilibili; ...@@ -3,12 +3,12 @@ package com.zhiwei.live.danmu.bilibili;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.zhiwei.crawler.utils.RequestUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
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.live.bean.RoomInfo; 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;
...@@ -21,13 +21,13 @@ import okhttp3.Response; ...@@ -21,13 +21,13 @@ import okhttp3.Response;
public class BilibiliClient { public class BilibiliClient {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
private static Logger logger = LogManager.getLogger(BilibiliClient.class); private static Logger logger = LogManager.getLogger(BilibiliClient.class);
private static final int PORT = 2243; private static final int PORT = 2243;
/** /**
* 根据房间号获取弹幕信息 * 根据房间号获取弹幕信息
* @param roomId * @param roomUrl
* @throws Exception * @throws Exception
*/ */
public static void getDanmu(DataCallBack dataCallBack,String roomUrl) throws Exception { public static void getDanmu(DataCallBack dataCallBack,String roomUrl) throws Exception {
......
...@@ -4,20 +4,41 @@ import java.util.Date; ...@@ -4,20 +4,41 @@ import java.util.Date;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class BilibiliMessage { public class BilibiliMessage {
String messageType; //弹幕消息类型 /**
* 弹幕消息类型
String user_id; //发布者uid */
String messageType;
String nickName; //发布者昵称
/**
Date time; //弹幕时间 * 发布者uid
*/
String content; //弹幕内容 String userId;
String room_id; //房间id /**
* 发布者昵称
*/
String nickName;
/**
* 弹幕时间
*/
Date time;
/**
* 弹幕内容
*/
String content;
/**
* 房间id
*/
String roomId;
public BilibiliMessage(JSONObject json) throws Exception { public BilibiliMessage(JSONObject json) throws Exception {
constructJson(json); constructJson(json);
...@@ -28,80 +49,15 @@ public class BilibiliMessage { ...@@ -28,80 +49,15 @@ public class BilibiliMessage {
private void constructJson(JSONObject json) throws Exception{ private void constructJson(JSONObject json) throws Exception{
try { try {
System.out.println(json);
JSONArray jsonArray = json.getJSONArray("info"); JSONArray jsonArray = json.getJSONArray("info");
messageType = json.getString("cmd"); messageType = json.getString("cmd");
user_id = jsonArray.getJSONArray(2).getString(0); userId = jsonArray.getJSONArray(2).getString(0);
time = new Date(); time = new Date();
nickName = jsonArray.getJSONArray(2).getString(1); nickName = jsonArray.getJSONArray(2).getString(1);
content = jsonArray.getString(1); content = jsonArray.getString(1);
} catch (Exception e) { } catch (Exception e) {
throw new Exception(); throw new Exception();
} }
} }
@Override
public String toString() {
return "new BilibiliMessage["
+ " user_id = " + user_id
+ ", nickName = " + nickName
+ ", messageType = " + messageType
+ ", time = " + time
+ ", content = " + content
+ ", room_id = " + room_id
+ "]";
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getMessageType() {
return messageType;
}
public String getUser_id() {
return user_id;
}
public String getNickName() {
return nickName;
}
public String getContent() {
return content;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public void setContent(String content) {
this.content = content;
}
public String getRoom_id() {
return room_id;
}
public void setRoom_id(String room_id) {
this.room_id = room_id;
}
} }
package com.zhiwei.live.danmu.bilibili; package com.zhiwei.live.danmu.bilibili;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.io.ByteArrayInputStream;
import java.util.regex.Matcher; import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.DataFormatException;
import org.apache.commons.lang3.StringUtils; import java.util.zip.Inflater;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import com.zhiwei.live.danmu.util.DataCallBack; import com.zhiwei.live.danmu.util.DataCallBack;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
...@@ -17,9 +20,9 @@ import io.netty.channel.ChannelHandlerContext; ...@@ -17,9 +20,9 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
@Log4j2
public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
private static final byte[] FIRST_REQ = new byte[] { 0x00, 0x00, 0x00 }; private static final byte[] FIRST_REQ = new byte[] { 0x00, 0x00, 0x00 };
private static final byte[] BILI_IN = new byte[] { 0x00, 0x10, 0x00 , 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,0x01}; private static final byte[] BILI_IN = new byte[] { 0x00, 0x10, 0x00 , 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,0x01};
...@@ -31,7 +34,7 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{ ...@@ -31,7 +34,7 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
/** /**
* Constructor * Constructor
* *
* @param roomId * @param roomid
* 房间号 * 房间号
*/ */
public BilibiliMessageHandler(DataCallBack dataCallBack,String roomid) { public BilibiliMessageHandler(DataCallBack dataCallBack,String roomid) {
...@@ -52,7 +55,7 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{ ...@@ -52,7 +55,7 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
.getBytes(); .getBytes();
ByteBuf loginMsg = Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(FIRST_REQ) ByteBuf loginMsg = Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(FIRST_REQ)
,Unpooled.buffer(1).writeByte(body.length+16),Unpooled.wrappedBuffer(BILI_IN), Unpooled.wrappedBuffer(body)); ,Unpooled.buffer(1).writeByte(body.length+16),Unpooled.wrappedBuffer(BILI_IN), Unpooled.wrappedBuffer(body));
System.out.println("发送登录消息: \n" + ByteBufUtil.prettyHexDump(loginMsg)); log.info("发送登录消息: {}" , ByteBufUtil.prettyHexDump(loginMsg));
ctx.writeAndFlush(loginMsg); ctx.writeAndFlush(loginMsg);
} }
...@@ -66,21 +69,12 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{ ...@@ -66,21 +69,12 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
if (msg instanceof ByteBuf) { if (msg instanceof ByteBuf) {
try { try {
ByteBuf buf = (ByteBuf) msg; ByteBuf buf = (ByteBuf) msg;
System.out.println("收到消息: \n" + ByteBufUtil.prettyHexDump(buf)); byte[] data = new byte[buf.writerIndex() - buf.readerIndex()];
byte[] bt = new byte[buf.writerIndex() - buf.readerIndex() - 16]; buf.readBytes(data);
byte[] byd = new byte[16]; if(data.length > 16){
buf.readBytes(byd, 0, 16); analyzeData(data);
// System.out.println("bt == " + new String(bt)); }else{
System.out.println("byd === " + byd[0] + " == " + byd[4] + " == " + byd[6] + " == " + byd[8] + " == " + byd[12]); System.out.println("11111111111111111111111");
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);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -89,6 +83,152 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{ ...@@ -89,6 +83,152 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
ReferenceCountUtil.release(msg); ReferenceCountUtil.release(msg);
} }
/**
* 解析b站弹幕数据
* @param data
* @throws Exception
*/
private void analyzeData(byte[] data) throws IOException, DataFormatException {
int dataLength = data.length;
if (dataLength < 16) {
log.info("wrong data");
} else {
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));
int msgLength = inputStream.readInt();
if (msgLength < 16) {
log.info("maybe need expand size of cache");
} else if (msgLength > 16 && msgLength == dataLength) {
short headerLength = inputStream.readShort();
short version = inputStream.readShort();
int action = inputStream.readInt() - 1;
// 直播间在线用户数目
if (action == 2) {
inputStream.readInt();
int userCount = inputStream.readInt();
System.out.println(userCount);
} else if (action == 4) {
int param = inputStream.readInt();
int msgBodyLength = dataLength - 16;
byte[] msgBody = new byte[msgBodyLength];
inputStream.read(msgBody, 0, msgBodyLength);
if (version != 2) {
String jsonStr = new String(msgBody, StandardCharsets.UTF_8);
JSONObject dataJson = JSONObject.parseObject(jsonStr);
System.out.println(dataJson);
} else {
Inflater inflater = new Inflater();
inflater.setInput(msgBody);
while (!inflater.finished()) {
byte[] header = new byte[16];
inflater.inflate(header, 0, 16);
DataInputStream headerStream = new DataInputStream(new ByteArrayInputStream(header));
int innerMsgLen = headerStream.readInt();
short innerHeaderLength = headerStream.readShort();
short innerVersion = headerStream.readShort();
int innerAction = headerStream.readInt() - 1;
int innerParam = headerStream.readInt();
byte[] innerData = new byte[innerMsgLen - 16];
inflater.inflate(innerData, 0, innerData.length);
if (innerAction == 4) {
String jsonStr = new String(innerData, StandardCharsets.UTF_8);
JSONObject dataJson = JSONObject.parseObject(jsonStr);
System.out.println(dataJson);
} else if (innerAction == 2) {
// pass
}
}
}
}
} else if (msgLength > 16 && msgLength < dataLength) {
byte[] singleData = new byte[msgLength];
System.arraycopy(data, 0, singleData, 0, msgLength);
analyzeData(singleData);
int remainLen = dataLength - msgLength;
byte[] remainDate = new byte[remainLen];
System.arraycopy(data, msgLength, remainDate, 0, remainLen);
analyzeData(remainDate);
}
}
}
// private void analyzeData(byte[] data) throws Exception{
// int dataLength = data.length;
// DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));
// int msgLength = inputStream.readInt();
// System.out.println(dataLength+"=============="+msgLength);
// if (msgLength > 16 && msgLength == dataLength) {
// short version = inputStream.readShort();
// int action = inputStream.readInt() - 1;
// System.out.println("action============"+action);
// // 直播间在线用户数目
// if (action == 2) {
// int userCount = inputStream.readInt();
//// log.info("直播间: {}, 人气 : {}", roomId, userCount);
// System.out.println("直播间人气====" + userCount);
// } else if (action == 4) {
// System.out.println("222222222222222222");
// int param = inputStream.readInt();
// int msgBodyLength = dataLength - 16;
// byte[] msgBody = new byte[msgBodyLength];
// inputStream.read(msgBody, 0, msgBodyLength);
// if (version != 2) {
// String jsonStr = new String(msgBody, StandardCharsets.UTF_8);
// System.out.println(jsonStr);
// JSONObject dataJson = JSONObject.parseObject(jsonStr);
// System.out.println("dataJson======"+ dataJson);
// BilibiliMessage bilibiliMessage = new BilibiliMessage(dataJson);
// bilibiliMessage.setRoomId(roomId);
// dataCallBack.onData(bilibiliMessage);
// } else {
// Inflater inflater = new Inflater();
// inflater.setInput(msgBody);
// while (!inflater.finished()) {
// byte[] header = new byte[16];
// inflater.inflate(header, 0, 16);
// DataInputStream headerStream = new DataInputStream(new ByteArrayInputStream(header));
// int innerMsgLen = headerStream.readInt();
// short innerHeaderLength = headerStream.readShort();
// short innerVersion = headerStream.readShort();
// int innerAction = headerStream.readInt() - 1;
// int innerParam = headerStream.readInt();
// byte[] innerData = new byte[innerMsgLen - 16];
// inflater.inflate(innerData, 0, innerData.length);
// if (innerAction == 4) {
// String jsonStr = new String(innerData, StandardCharsets.UTF_8);
// System.out.println(jsonStr);
// JSONObject dataJson = JSONObject.parseObject(jsonStr);
// System.out.println("dataJson======"+ dataJson);
// BilibiliMessage bilibiliMessage = new BilibiliMessage(dataJson);
// bilibiliMessage.setRoomId(roomId);
// dataCallBack.onData(bilibiliMessage);
// }
// }
// }
// }
// } else if (msgLength > 16 && msgLength < dataLength) {
// System.out.println("3333333333333333");
// byte[] singleData = new byte[msgLength];
// System.arraycopy(data, 0, singleData, 0, msgLength);
// analyzeData(singleData);
// int remainLen = dataLength - msgLength;
// byte[] remainDate = new byte[remainLen];
// System.arraycopy(data, msgLength, remainDate, 0, remainLen);
// analyzeData(remainDate);
// }else{
// log.info("maybe need expand size of cache");
// System.out.println("maybe need expand size of cache");
// }
// }
/* /*
* @see * @see
* io.netty.channel.ChannelInboundHandlerAdapter#userEventTriggered(io.netty. * io.netty.channel.ChannelInboundHandlerAdapter#userEventTriggered(io.netty.
......
...@@ -22,8 +22,8 @@ import io.netty.handler.timeout.IdleStateHandler; ...@@ -22,8 +22,8 @@ import io.netty.handler.timeout.IdleStateHandler;
public class DouyuClient { public class DouyuClient {
private static Logger logger = LogManager.getLogger(DouyuClient.class); private static Logger logger = LogManager.getLogger(DouyuClient.class);
private static final int PORT = 8601; private static final int PORT = 8504;
private static final String HOST = "openbarrage.douyutv.com"; private static final String HOST = "danmuproxy.douyu.com";
/** /**
* 根据房间号获取弹幕信息 * 根据房间号获取弹幕信息
......
...@@ -70,7 +70,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter { ...@@ -70,7 +70,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter {
if (msg instanceof ByteBuf) { if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg; ByteBuf buf = (ByteBuf) msg;
String source = buf.toString(CharsetUtil.UTF_8); String source = buf.toString(CharsetUtil.UTF_8);
// System.out.println("source========="+source); System.out.println("source========="+source);
if(source.contains("chatmsg")) { if(source.contains("chatmsg")) {
Map<String,Object> messageMap = DouYuUtil.toMap(source); Map<String,Object> messageMap = DouYuUtil.toMap(source);
String data = JSONObject.toJSONString(messageMap); String data = JSONObject.toJSONString(messageMap);
...@@ -84,6 +84,10 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter { ...@@ -84,6 +84,10 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter {
ByteBuf groupMsg = buildMsg(StringUtils.join("type@=joingroup/rid@=",roomId,"/gid@=-9999/")); ByteBuf groupMsg = buildMsg(StringUtils.join("type@=joingroup/rid@=",roomId,"/gid@=-9999/"));
System.out.println("发送入组消息: \n" + ByteBufUtil.prettyHexDump(groupMsg)); System.out.println("发送入组消息: \n" + ByteBufUtil.prettyHexDump(groupMsg));
ctx.writeAndFlush(groupMsg); ctx.writeAndFlush(groupMsg);
// ByteBuf giftMsg = buildMsg(StringUtils.join("type@=dmfbdreq/dfl@=sn@AA=105@ASss@AA=0@AS@Ssn@AA=106@ASss@AA=0@AS@Ssn@AA=107@ASss@AA=0@AS@Ssn@AA=108@ASss@AA=0@AS@Ssn@AA=110@ASss@AA=0@AS@S/"));
// System.out.println("发送礼物消息: \n" + ByteBufUtil.prettyHexDump(giftMsg));
// ctx.writeAndFlush(giftMsg);
finish = true; finish = true;
} }
ctx.fireChannelRead(msg); ctx.fireChannelRead(msg);
...@@ -97,7 +101,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter { ...@@ -97,7 +101,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter {
if (evt instanceof IdleStateEvent) { if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt; IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.WRITER_IDLE) { if (event.state() == IdleState.WRITER_IDLE) {
ByteBuf pingMsg = buildMsg(StringUtils.join("type@=keeplive/tick@=", System.currentTimeMillis() / 1000, "/")); ByteBuf pingMsg = buildMsg(StringUtils.join("type@=mrkl/"));
System.out.println("发送心跳消息: \n" + ByteBufUtil.prettyHexDump(pingMsg)); System.out.println("发送心跳消息: \n" + ByteBufUtil.prettyHexDump(pingMsg));
ctx.writeAndFlush(pingMsg); ctx.writeAndFlush(pingMsg);
} else { } else {
...@@ -109,7 +113,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter { ...@@ -109,7 +113,7 @@ public class DouyuMessageHandler extends ChannelInboundHandlerAdapter {
} }
private ByteBuf buildMsg(String content) { private ByteBuf buildMsg(String content) {
int fixedLen = 4 + 4 + 1; int fixedLen = 4 + 4 + 1 + content.length();
byte[] body = content.getBytes(CharsetUtil.UTF_8); byte[] body = content.getBytes(CharsetUtil.UTF_8);
int length = fixedLen + body.length; int length = fixedLen + body.length;
return Unpooled.buffer(length).writeIntLE(length).writeIntLE(length).writeShortLE(689) return Unpooled.buffer(length).writeIntLE(length).writeIntLE(length).writeShortLE(689)
......
...@@ -5,7 +5,7 @@ import java.util.Objects; ...@@ -5,7 +5,7 @@ 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.utils.RequestUtils;
import com.zhiwei.live.bean.RoomInfo; 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;
...@@ -23,7 +23,7 @@ import okhttp3.Response; ...@@ -23,7 +23,7 @@ import okhttp3.Response;
* @date Jan 25, 2019 3:24:33 PM * @date Jan 25, 2019 3:24:33 PM
*/ */
public class PandamClient { public class PandamClient {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
/** /**
......
...@@ -3,6 +3,7 @@ package com.zhiwei.live.danmu.pandam; ...@@ -3,6 +3,7 @@ package com.zhiwei.live.danmu.pandam;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
...@@ -24,6 +25,7 @@ import io.netty.util.ReferenceCountUtil; ...@@ -24,6 +25,7 @@ import io.netty.util.ReferenceCountUtil;
* @author 0xff * @author 0xff
* @date Jan 25, 2019 11:03:20 AM * @date Jan 25, 2019 11:03:20 AM
*/ */
@Log4j2
public class PandamMessageHandler extends ChannelInboundHandlerAdapter { public class PandamMessageHandler extends ChannelInboundHandlerAdapter {
private static final byte[] FIRST_REQ = new byte[] { 0x00, 0x06, 0x00, 0x02, 0x00 }; private static final byte[] FIRST_REQ = new byte[] { 0x00, 0x06, 0x00, 0x02, 0x00 };
private static final byte[] FIRST_RES = new byte[] { 0x00, 0x06, 0x00, 0x06 }; private static final byte[] FIRST_RES = new byte[] { 0x00, 0x06, 0x00, 0x06 };
......
package com.zhiwei.live.roominfo; package com.zhiwei.live.roominfo;
import com.zhiwei.crawler.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
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.proxy.ProxyHolder; import com.zhiwei.crawler.proxy.ProxyHolder;
import com.zhiwei.live.bean.RoomInfo; import com.zhiwei.live.bean.RoomInfo;
import com.zhiwei.tools.tools.ZhiWeiTools; import com.zhiwei.tools.tools.ZhiWeiTools;
...@@ -18,7 +18,7 @@ import com.zhiwei.tools.tools.ZhiWeiTools; ...@@ -18,7 +18,7 @@ import com.zhiwei.tools.tools.ZhiWeiTools;
*/ */
public class BilibiliRoomInfoCrawler { public class BilibiliRoomInfoCrawler {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
private static Logger logger = LogManager.getLogger(BilibiliRoomInfoCrawler.class); private static Logger logger = LogManager.getLogger(BilibiliRoomInfoCrawler.class);
private static final String PT = "B站"; private static final String PT = "B站";
...@@ -69,7 +69,7 @@ public class BilibiliRoomInfoCrawler { ...@@ -69,7 +69,7 @@ public class BilibiliRoomInfoCrawler {
public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{ public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_PROXY).body().string(); String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_HEAVY_PROXY).body().string();
if(!StringUtils.isBlank(htmlBody)) { if(!StringUtils.isBlank(htmlBody)) {
//判断页面中是否包含房间信息 //判断页面中是否包含房间信息
if(htmlBody.contains("window.__NEPTUNE_IS_MY_WAIFU__=")) { if(htmlBody.contains("window.__NEPTUNE_IS_MY_WAIFU__=")) {
...@@ -90,7 +90,7 @@ public class BilibiliRoomInfoCrawler { ...@@ -90,7 +90,7 @@ public class BilibiliRoomInfoCrawler {
String username = null; String username = null;
//通过房间id获取用户信息 //通过房间id获取用户信息
roomUrl = "https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid="+room_id; roomUrl = "https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid="+room_id;
String roomBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_PROXY).body().string(); String roomBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_HEAVY_PROXY).body().string();
if(!StringUtils.isBlank(roomBody)) { if(!StringUtils.isBlank(roomBody)) {
JSONObject roomData = JSONObject.parseObject(roomBody).getJSONObject("data"); JSONObject roomData = JSONObject.parseObject(roomBody).getJSONObject("data");
username = roomData.getJSONObject("info").getString("uname"); username = roomData.getJSONObject("info").getString("uname");
......
package com.zhiwei.live.roominfo; package com.zhiwei.live.roominfo;
import com.zhiwei.crawler.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
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.proxy.ProxyHolder; import com.zhiwei.crawler.proxy.ProxyHolder;
import com.zhiwei.live.bean.RoomInfo; import com.zhiwei.live.bean.RoomInfo;
...@@ -17,13 +17,13 @@ import com.zhiwei.live.bean.RoomInfo; ...@@ -17,13 +17,13 @@ import com.zhiwei.live.bean.RoomInfo;
*/ */
public class DouYuRoomInfoCrawler { public class DouYuRoomInfoCrawler {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
private static Logger logger = LogManager.getLogger(DouYuRoomInfoCrawler.class); private static Logger logger = LogManager.getLogger(DouYuRoomInfoCrawler.class);
private static final String PT = "斗鱼"; private static final String PT = "斗鱼";
/** /**
* 根据房间id获取房间信息 * 根据房间id获取房间信息
* @param roomId * @param roomUrl
* @return * @return
* @throws Exception * @throws Exception
*/ */
...@@ -64,16 +64,16 @@ public class DouYuRoomInfoCrawler { ...@@ -64,16 +64,16 @@ public class DouYuRoomInfoCrawler {
/** /**
* 根据房间id获取房间信息 * 根据房间id获取房间信息
* @param roomId * @param roomUrl
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{ public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{
String roomBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_PROXY).body().string(); String roomBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_HEAVY_PROXY).body().string();
if(!StringUtils.isBlank(roomBody) && roomBody.contains("ROOM.room_id =")) { if(!StringUtils.isBlank(roomBody) && roomBody.contains("ROOM.room_id =")) {
String roomId = roomBody.split("ROOM\\.room_id = ")[1].split("; ")[0].trim(); String roomId = roomBody.split("ROOM\\.room_id = ")[1].split("; ")[0].trim();
String url = "http://open.douyucdn.cn/api/RoomApi/room/" + roomId; String url = "http://open.douyucdn.cn/api/RoomApi/room/" + roomId;
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url), ProxyHolder.NAT_PROXY).body().string(); String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url), ProxyHolder.NAT_HEAVY_PROXY).body().string();
if(!StringUtils.isBlank(htmlBody)) { if(!StringUtils.isBlank(htmlBody)) {
JSONObject data = JSONObject.parseObject(htmlBody).getJSONObject("data"); JSONObject data = JSONObject.parseObject(htmlBody).getJSONObject("data");
String room_name = data.getString("room_name"); String room_name = data.getString("room_name");
......
package com.zhiwei.live.roominfo; package com.zhiwei.live.roominfo;
import com.zhiwei.crawler.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -7,13 +8,12 @@ import org.jsoup.Jsoup; ...@@ -7,13 +8,12 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import com.zhiwei.crawler.core.HttpBoot; import com.zhiwei.crawler.core.HttpBoot;
import com.zhiwei.crawler.core.RequestUtils;
import com.zhiwei.crawler.proxy.ProxyHolder; import com.zhiwei.crawler.proxy.ProxyHolder;
import com.zhiwei.live.bean.RoomInfo; import com.zhiwei.live.bean.RoomInfo;
public class HuYaRoomInfoCrawler { public class HuYaRoomInfoCrawler {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
private static Logger logger = LogManager.getLogger(HuYaRoomInfoCrawler.class); private static Logger logger = LogManager.getLogger(HuYaRoomInfoCrawler.class);
private static final String PT = "虎牙"; private static final String PT = "虎牙";
...@@ -56,7 +56,7 @@ public class HuYaRoomInfoCrawler { ...@@ -56,7 +56,7 @@ public class HuYaRoomInfoCrawler {
* @throws Exception * @throws Exception
*/ */
public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception { public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception {
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_PROXY).body().string(); String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl), ProxyHolder.NAT_HEAVY_PROXY).body().string();
if (!StringUtils.isBlank(htmlBody)) { if (!StringUtils.isBlank(htmlBody)) {
Document document = Jsoup.parse(htmlBody); Document document = Jsoup.parse(htmlBody);
String roomName = document.select("h1#J_roomTitle").text(); String roomName = document.select("h1#J_roomTitle").text();
......
package com.zhiwei.live.roominfo; package com.zhiwei.live.roominfo;
import com.zhiwei.crawler.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
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.proxy.ProxyHolder; import com.zhiwei.crawler.proxy.ProxyHolder;
import com.zhiwei.live.bean.RoomInfo; import com.zhiwei.live.bean.RoomInfo;
import com.zhiwei.tools.tools.ZhiWeiTools; import com.zhiwei.tools.tools.ZhiWeiTools;
...@@ -17,7 +17,7 @@ import com.zhiwei.tools.tools.ZhiWeiTools; ...@@ -17,7 +17,7 @@ import com.zhiwei.tools.tools.ZhiWeiTools;
* *
*/ */
public class PandamTVRoomInfoCrawler { public class PandamTVRoomInfoCrawler {
private static HttpBoot httpBoot = new HttpBoot(); private static HttpBoot httpBoot = new HttpBoot.Builder().build();
private static Logger logger = LogManager.getLogger(PandamTVRoomInfoCrawler.class); private static Logger logger = LogManager.getLogger(PandamTVRoomInfoCrawler.class);
private static final String PT = "熊猫TV"; private static final String PT = "熊猫TV";
...@@ -85,7 +85,7 @@ public class PandamTVRoomInfoCrawler { ...@@ -85,7 +85,7 @@ public class PandamTVRoomInfoCrawler {
* @throws Exception * @throws Exception
*/ */
public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{ public static RoomInfo getRoomInfoProxyByRoomUrl(String roomUrl) throws Exception{
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl),ProxyHolder.NAT_PROXY).body().string(); String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(roomUrl),ProxyHolder.NAT_HEAVY_PROXY).body().string();
if(!StringUtils.isBlank(htmlBody)) { if(!StringUtils.isBlank(htmlBody)) {
//判断页面中是否包含房间信息,此为pc端直播 //判断页面中是否包含房间信息,此为pc端直播
if(htmlBody.contains("window._config_roominfo = ")) { if(htmlBody.contains("window._config_roominfo = ")) {
......
...@@ -7,7 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack; ...@@ -7,7 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack;
public class BilibiliDanMuTest { 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/529"; String roomUrl = "https://live.bilibili.com/387";
try { try {
BilibiliClient.getDanmu(new DataCallBack() { BilibiliClient.getDanmu(new DataCallBack() {
@Override @Override
......
...@@ -7,7 +7,7 @@ import com.zhiwei.live.danmu.util.DataCallBack; ...@@ -7,7 +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://www.douyu.com/71017"; String roomUrl = "https://www.douyu.com/topic/NarutoMatch?rid=535534";
try { try {
DouyuClient.getDanmu(new DataCallBack() { DouyuClient.getDanmu(new DataCallBack() {
@Override @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