Skip to content

Commit b1d3c36

Browse files
committed
增强鲁棒性
1 parent 4827e81 commit b1d3c36

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

CrawlDouyuDanmu/src/main/java/Crawl.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ public class Crawl extends Thread {
1616
String gid;
1717
//与弹幕服务器交互的控制器
1818
MessageHandler messageHandler;
19+
//登录名
20+
String username;
1921

2022
public Crawl() throws IOException {
2123
rid = Utils.getRoomId();
2224
}
2325

2426
/**
2527
*
26-
* 初始化弹幕服务器地址数据和弹幕分组信息
28+
* 初始化"弹幕服务器地址数据"和"弹幕分组信息"和"登录用户名"
2729
*/
2830
public void init() throws IOException {
2931
String ip = Utils.getServerIP();
@@ -56,6 +58,8 @@ public void init() throws IOException {
5658
}
5759
} else if (msg.startsWith("type@=setmsggroup")) {
5860
gid = msg.split("gid@=")[1].split("/")[0];
61+
} else if (msg.startsWith("type@=loginres")) {
62+
username = msg.split("username@=")[1].split("/")[0];
5963
}
6064
}
6165
socket.close();
@@ -66,43 +70,59 @@ public void login() throws IOException {
6670
System.out.println("连接弹幕服务器(danmu.douyutv.com:" + ports.get(0) + ")");
6771
messageHandler = new MessageHandler(socket);
6872

69-
String loginreq = "type@=loginreq/username@=visitor503535/password@=1234567890123456/roomid@=" + rid + "/";
73+
String loginreq = "type@=loginreq/username@=" + username + "/password@=1234567890123456/roomid@=" + rid + "/";
7074
messageHandler.send(loginreq);
71-
String joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=" + gid + "/";
75+
System.out.println("登录名:" + username);
76+
77+
String joinGroup;
78+
if (Utils.isSeaMode()) {
79+
joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=-9999/";
80+
System.out.println("海量弹幕模式:开启");
81+
} else {
82+
joinGroup = "type@=joingroup/rid@=" + rid + "/gid@=" + gid + "/";
83+
System.out.println("海量弹幕模式:关闭");
84+
System.out.println("进入" + gid + "号弹幕分组");
85+
}
86+
7287
messageHandler.send(joinGroup);
73-
System.out.println("进入" + gid + "号弹幕分组");
7488
}
7589

7690
@Override
7791
public void run() {
7892
try {
7993
System.out.println("房间名:" + Utils.getRoomName());
8094
System.out.println("主播:" + Utils.getOwnerName());
81-
if (!Utils.roomIsAlive()) {
95+
/*if (!Utils.roomIsAlive()) {
8296
System.out.println("房间未开播,程序结束.");
8397
return;
8498
} else {
8599
System.out.println("状态:正在直播");
86-
}
100+
}*/
101+
} catch (IOException e) {
102+
e.printStackTrace();
103+
}
87104

105+
try {
88106
init();
89107
login();
108+
login();
109+
System.out.println("--------------------------");
90110

91111
long start = System.currentTimeMillis();
92112
while (true) {
93113
byte[] bytes = messageHandler.read();
94114
String msg = new String(Arrays.copyOfRange(bytes,8,bytes.length));
95115

96116
if (msg.startsWith("type@=chatmessage")) {
97-
String nickname = msg.split("@Snick@A=")[1].split("@Srg@A")[0];
98-
String content = msg.split("content@=")[1].split("/snick@=")[0];
117+
String nickname = msg.split("Snick@A=",2)[1].split("@",2)[0];
118+
String content = msg.split("content@=",2)[1].split("/",2)[0];
99119
System.out.println("[" + nickname + "]:" + content);
100-
// System.out.println(msg);
101120
}
102121

103122
long end = System.currentTimeMillis();
104123
if (end - start > 30000) {
105124
messageHandler.send("type=mrkl/");
125+
start = System.currentTimeMillis();
106126
}
107127

108128
Thread.sleep(1);

CrawlDouyuDanmu/src/main/java/MessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public byte[] read() throws IOException {
3636
byte[] bytes = new byte[contentLen];
3737
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
3838
while ((len = inputStream.read(bytes,0,contentLen - readLen)) != -1) {
39-
byteArray.write(bytes);
39+
byteArray.write(bytes,0,len);
4040
readLen += len;
4141
if (readLen == contentLen) {
4242
break;

CrawlDouyuDanmu/src/main/java/Utils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public static String getRoomUrl() throws IOException {
6969
}
7070
}
7171

72+
/**
73+
*
74+
* @return 返回值代表是否开启海量弹幕模式
75+
*/
76+
public static boolean isSeaMode() {
77+
return config.getProperty("seaMode").equals("true");
78+
}
79+
7280
public static String getServerIP() throws IOException {
7381
return config.getProperty("serverIP");
7482
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#要抓取的斗鱼房间网址
2-
url = http://www.douyutv.com/caomei
2+
url = http://www.douyutv.com/wt55kai
33

44
#一开始通讯的服务器,不是弹幕服务器,弹幕服务器地址是与这个服务器通讯后返回的(抓包得到这个地址)
55
serverIP = 119.90.49.93
6-
serverPort = 8062
6+
serverPort = 8062
7+
8+
#海量弹幕模式 (true || false)
9+
#在人气少的主播房间好像是不管用的,看人气少的主播弹幕设置为false
10+
seaMode = true

0 commit comments

Comments
 (0)