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
01161ad1
Commit
01161ad1
authored
Jan 29, 2019
by
yangchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
b站数据返回修改
parent
65064349
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
21 deletions
+32
-21
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliClient.java
+11
-18
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
+6
-3
src/main/java/com/zhiwei/live/danmu/bilibili/DataCallBack.java
+15
-0
No files found.
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliClient.java
View file @
01161ad1
package
com
.
zhiwei
.
live
.
danmu
.
bilibili
;
package
com
.
zhiwei
.
live
.
danmu
.
bilibili
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Objects
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -9,14 +8,11 @@ import org.apache.logging.log4j.Logger;
...
@@ -9,14 +8,11 @@ import org.apache.logging.log4j.Logger;
import
org.jsoup.Jsoup
;
import
org.jsoup.Jsoup
;
import
org.jsoup.nodes.Document
;
import
org.jsoup.nodes.Document
;
import
com.zhiwei.common.config.GroupType
;
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.crawler.proxy.ProxyFactory
;
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.roominfo.BilibiliRoomInfoCrawler
;
import
com.zhiwei.live.roominfo.BilibiliRoomInfoCrawler
;
import
com.zhiwei.tools.tools.ZhiWeiTools
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.handler.timeout.IdleStateHandler
;
import
io.netty.handler.timeout.IdleStateHandler
;
...
@@ -30,20 +26,17 @@ public class BilibiliClient {
...
@@ -30,20 +26,17 @@ public class BilibiliClient {
private
static
final
int
PORT
=
2243
;
private
static
final
int
PORT
=
2243
;
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
String
roomUrl
=
"https://live.bilibili.com/
4
39"
;
String
roomUrl
=
"https://live.bilibili.com/
1
39"
;
String
roomId
=
roomUrl
.
replaceAll
(
"https://live.bilibili.com/"
,
""
);
String
roomId
=
roomUrl
.
replaceAll
(
"https://live.bilibili.com/"
,
""
);
try
{
try
{
getDanmu
(
roomId
);
getDanmu
(
new
DataCallBack
()
{
List
<
BilibiliMessage
>
messagesList
=
BilibiliMessageListener
.
messages
;
while
(
true
)
{
@Override
try
{
public
void
onData
(
BilibiliMessage
data
)
{
BilibiliMessage
messages
=
messagesList
.
remove
(
0
);
System
.
out
.
println
(
"-------------"
+
data
.
toString
());
System
.
out
.
println
(
messages
);
}
catch
(
Exception
e
)
{
}
break
;
},
roomId
);
}
ZhiWeiTools
.
sleep
(
50
);
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
...
@@ -58,7 +51,7 @@ public class BilibiliClient {
...
@@ -58,7 +51,7 @@ public class BilibiliClient {
* @param roomId
* @param roomId
* @throws Exception
* @throws Exception
*/
*/
public
static
void
getDanmu
(
String
roomId
)
throws
Exception
{
public
static
void
getDanmu
(
DataCallBack
dataCallBack
,
String
roomId
)
throws
Exception
{
//根据房间号获取真实房间号
//根据房间号获取真实房间号
RoomInfo
roomInfo
=
BilibiliRoomInfoCrawler
.
getRoomInfoByRoomId
(
roomId
);
RoomInfo
roomInfo
=
BilibiliRoomInfoCrawler
.
getRoomInfoByRoomId
(
roomId
);
if
(
Objects
.
nonNull
(
roomInfo
))
{
if
(
Objects
.
nonNull
(
roomInfo
))
{
...
@@ -75,7 +68,7 @@ public class BilibiliClient {
...
@@ -75,7 +68,7 @@ public class BilibiliClient {
}
}
//建立弹幕连接
//建立弹幕连接
Connector
.
asynchronizedTcpConnect
(
new
NioEventLoopGroup
(),
host
,
PORT
,
Connector
.
asynchronizedTcpConnect
(
new
NioEventLoopGroup
(),
host
,
PORT
,
new
IdleStateHandler
(
0
,
30
,
0
,
TimeUnit
.
SECONDS
),
new
BilibiliMessageHandler
(
roomInfo
.
getRoomId
())).
sync
();
new
IdleStateHandler
(
0
,
30
,
0
,
TimeUnit
.
SECONDS
),
new
BilibiliMessageHandler
(
dataCallBack
,
roomInfo
.
getRoomId
())).
sync
();
}
else
{
}
else
{
logger
.
info
(
"获取真实房间号出现问题,请及时检查程序"
);
logger
.
info
(
"获取真实房间号出现问题,请及时检查程序"
);
}
}
...
...
src/main/java/com/zhiwei/live/danmu/bilibili/BilibiliMessageHandler.java
View file @
01161ad1
...
@@ -23,14 +23,16 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
...
@@ -23,14 +23,16 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
private
static
final
byte
[]
PING
=
new
byte
[]
{
0x00
,
0x00
,
0x00
,
0x1F
,
0x00
,
0x10
,
0x00
,
0x01
,
0x00
,
0x00
,
0x00
,
0x02
,
0x00
,
0x00
,
0x00
,
0x01
,
0x5B
,
0x6F
,
0x62
,
0x6A
,
0x65
,
0x63
,
0x74
,
0x20
,
0x4F
,
0x62
,
0x6A
,
0x65
,
0x63
,
0x74
,
0x5D
};
private
static
final
byte
[]
PING
=
new
byte
[]
{
0x00
,
0x00
,
0x00
,
0x1F
,
0x00
,
0x10
,
0x00
,
0x01
,
0x00
,
0x00
,
0x00
,
0x02
,
0x00
,
0x00
,
0x00
,
0x01
,
0x5B
,
0x6F
,
0x62
,
0x6A
,
0x65
,
0x63
,
0x74
,
0x20
,
0x4F
,
0x62
,
0x6A
,
0x65
,
0x63
,
0x74
,
0x5D
};
private
static
Pattern
pattern
=
Pattern
.
compile
(
"\\{\"cmd\":\"DANMU_MSG.+?\\]\\}"
);
private
static
Pattern
pattern
=
Pattern
.
compile
(
"\\{\"cmd\":\"DANMU_MSG.+?\\]\\}"
);
private
String
roomid
;
private
String
roomid
;
private
DataCallBack
dataCallBack
;
/**
/**
* Constructor
* Constructor
*
*
* @param roomId
* @param roomId
* 房间号
* 房间号
*/
*/
public
BilibiliMessageHandler
(
String
roomid
)
{
public
BilibiliMessageHandler
(
DataCallBack
dataCallBack
,
String
roomid
)
{
this
.
dataCallBack
=
dataCallBack
;
this
.
roomid
=
roomid
;
this
.
roomid
=
roomid
;
}
}
...
@@ -66,7 +68,8 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
...
@@ -66,7 +68,8 @@ public class BilibiliMessageHandler extends ChannelInboundHandlerAdapter{
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
);
System
.
out
.
println
(
bilibiliMessage
);
dataCallBack
.
onData
(
bilibiliMessage
);
// System.out.println(bilibiliMessage);
}
}
}
}
ReferenceCountUtil
.
release
(
msg
);
ReferenceCountUtil
.
release
(
msg
);
...
...
src/main/java/com/zhiwei/live/danmu/bilibili/DataCallBack.java
0 → 100644
View file @
01161ad1
package
com
.
zhiwei
.
live
.
danmu
.
bilibili
;
public
interface
DataCallBack
{
/**
* 当有输入传入调度
*
* @param data
* @param attr
* @return void
*/
void
onData
(
BilibiliMessage
data
);
}
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