Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
live-crawler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhiwei
live-crawler
Commits
fe6e0d5b
Commit
fe6e0d5b
authored
Jun 02, 2020
by
cwy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
b站弹幕采集修改
parent
2a8c7a4d
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
9 deletions
+17
-9
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliClient.java
+5
-6
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
+11
-2
src/test/java/com/zhiwei/live/test/danmu/BilibiliDanMuTest.java
+1
-1
No files found.
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliClient.java
View file @
fe6e0d5b
...
@@ -5,9 +5,8 @@ import java.util.concurrent.TimeUnit;
...
@@ -5,9 +5,8 @@ import java.util.concurrent.TimeUnit;
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
org.jsoup.Jsoup
;
import
org.jsoup.nodes.Document
;
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.bean.RoomInfo
;
...
@@ -36,13 +35,13 @@ public class BilibiliClient {
...
@@ -36,13 +35,13 @@ public class BilibiliClient {
RoomInfo
roomInfo
=
BilibiliRoomInfoCrawler
.
getRoomInfoByRoomUrl
(
roomUrl
);
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://
api.live.bilibili.com/room/v1/Danmu/getConf?room_id=
"
+
roomInfo
.
getRoomId
();
Request
request
=
RequestUtils
.
wrapGet
(
url
);
Request
request
=
RequestUtils
.
wrapGet
(
url
);
String
host
=
null
;
String
host
=
null
;
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
Document
doc
=
Jsoup
.
parse
(
response
.
body
().
string
()
);
String
result
=
response
.
body
().
string
(
);
String
[]
address
=
doc
.
select
(
"dm_server_list"
).
text
().
split
(
","
);
JSONObject
json
=
JSONObject
.
parseObject
(
result
);
host
=
address
[
0
]
;
host
=
json
.
getJSONObject
(
"data"
).
getJSONArray
(
"host_server_list"
).
getJSONObject
(
0
).
getString
(
"host"
)
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
IllegalArgumentException
(
"获取聊天服务器地址失败"
,
e
);
throw
new
IllegalArgumentException
(
"获取聊天服务器地址失败"
,
e
);
}
}
...
...
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
View file @
fe6e0d5b
...
@@ -64,17 +64,26 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
...
@@ -64,17 +64,26 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
@Override
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
if
(
msg
instanceof
ByteBuf
)
{
if
(
msg
instanceof
ByteBuf
)
{
try
{
ByteBuf
buf
=
(
ByteBuf
)
msg
;
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
);
String
source
=
buf
.
toString
(
CharsetUtil
.
UTF_8
);
Matcher
matcher
=
pattern
.
matcher
(
source
);
Matcher
matcher
=
pattern
.
matcher
(
source
);
// System.out.println(source);
while
(
matcher
.
find
())
{
while
(
matcher
.
find
())
{
JSONObject
dataJson
=
JSONObject
.
parseObject
(
matcher
.
group
());
JSONObject
dataJson
=
JSONObject
.
parseObject
(
matcher
.
group
());
BilibiliMessage
bilibiliMessage
=
new
BilibiliMessage
(
dataJson
);
BilibiliMessage
bilibiliMessage
=
new
BilibiliMessage
(
dataJson
);
bilibiliMessage
.
setRoom_id
(
roomId
);
bilibiliMessage
.
setRoom_id
(
roomId
);
dataCallBack
.
onData
(
bilibiliMessage
);
dataCallBack
.
onData
(
bilibiliMessage
);
// System.out.println(bilibiliMessage);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
ReferenceCountUtil
.
release
(
msg
);
ReferenceCountUtil
.
release
(
msg
);
...
...
src/test/java/com/zhiwei/live/test/danmu/BilibiliDanMuTest.java
View file @
fe6e0d5b
...
@@ -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/
13
9"
;
String
roomUrl
=
"https://live.bilibili.com/
52
9"
;
try
{
try
{
BilibiliClient
.
getDanmu
(
new
DataCallBack
()
{
BilibiliClient
.
getDanmu
(
new
DataCallBack
()
{
@Override
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment