关于
一直在想办法获取哔哩哔哩的相关对象,对媒体运营数据进行自动管理,于是写了一个BiliClient的JAR包,自己使用,也分享出来,希望可以帮助大家,目前该项目已经上传GITHUB和Maven中央仓库,可以直接使用依赖引入。
引入Maven依赖包
在IDEA中新建Maven项目,并在POM文件中加入如下依赖
<!-- https://mvnrepository.com/artifact/cn.hll520.linclient/BilibiliClient -->
<dependency>
<groupId>cn.hll520.linclient</groupId>
<artifactId>BilibiliClient</artifactId>
<version>1.0.2</version>
</dependency>
1.配置连接(可选)
调用BaseClientConfig
的静态方法对相关属进行配置,目前支持如下配置,不进行配置也可以,将使用默认配置
BaseClientConfig baseClientConfig=new BaseClientConfig();
// 协议
baseClientConfig.setProtocol(String protocol);
// 服务host
baseClientConfig.setHost(String host);
// 服务host Vc
baseClientConfig.setHostVc(String hostVc);
// 配置端口
baseClientConfig.setPort(Integer port);
// 配置请求
baseClientConfig.setRequestConfig(RequestConfig config);
若进行配置将会修改全局配置,对配置之后的全部请求生效
2. 获取BiliClient对象
目前仅支持使用工厂方法获取默认对象
BiliClient client = BiliClientFactor.getClient();
3. 使用
通过实例对象.类型.条件.方法()
获取对应的哔哩哔哩对象,有如下案例,以下均免登录获取,涉及用户的均以皮皮团
为例
3.1 获取用户信息
使用UID获取指定用户的信息。
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
User user = client.user().withUID(20165629L).get();
System.out.println(user);
System.out.println("姓名"+user.getName());
System.out.println("签名"+user.getSign());
System.out.println("生日"+user.getBirthday());
System.out.println("头像"+user.getFace());
System.out.println("认证信息"+user.getOfficial());
System.out.println("直播间信息"+user.getLive_room());
System.out.println("会员信息"+user.getVip());
}
}
结果如下
User(mid=20165629, name=共青团中央, sex=保密, face=http://i1.hdslb.com/bfs/face/31a4594ba9e39c985e19d37594a794df752bf4b4.jpg, sign=新年快乐!这里皮皮团,等你很久啦!, rank=10000, level=6, silence=0, birthday=05-05, coins=0, fans_badge=true, official=Official(role=4, title=共青团中央 官方账号, desc=, type=1), vip=Vip(type=2, status=1, theme_type=0, label=Vip.Label(path=, text=年度大会员, label_theme=annual_vip), avatar_subscript=1, nickname_color=#FB7299), pendant=Pendant(pid=0, name=, image=, expire=0, image_enhance=, image_enhance_frame=), nameplate=Nameplate(nid=39, name=直播车神, image=http://i2.hdslb.com/bfs/face/a1bf58db4a48f4ec394627d96af7399456812bbb.png, image_small=http://i1.hdslb.com/bfs/face/9d1ab43e5064834e0081c46990c7f4d1228ac69c.png, level=稀有勋章, condition=直播主播等级>=30级), is_followed=false, top_photo=http://i1.hdslb.com/bfs/space/cb1c3ef50e22b6096fde67febe863494caefebad.png, theme={}, sys_notice=SysNotice(id=null, content=null, url=null, notice_type=null, icon=null, text_color=null, bg_color=null), live_room=LiveRoom(roomStatus=1, liveStatus=0, url=https://live.bilibili.com/910884, title=共青团中央的投稿视频, cover=http://i0.hdslb.com/bfs/live/new_room_cover/3df1d45603474f67465a8dc9f0ad871c0597ce6d.jpg, online=10577, roomid=910884, roundStatus=1, broadcast_type=0))
姓名共青团中央
签名新年快乐!这里皮皮团,等你很久啦!
生日05-05
头像http://i1.hdslb.com/bfs/face/31a4594ba9e39c985e19d37594a794df752bf4b4.jpg
认证信息Official(role=4, title=共青团中央 官方账号, desc=, type=1)
直播间信息LiveRoom(roomStatus=1, liveStatus=0, url=https://live.bilibili.com/910884, title=共青团中央的投稿视频, cover=http://i0.hdslb.com/bfs/live/new_room_cover/3df1d45603474f67465a8dc9f0ad871c0597ce6d.jpg, online=10577, roomid=910884, roundStatus=1, broadcast_type=0)
会员信息Vip(type=2, status=1, theme_type=0, label=Vip.Label(path=, text=年度大会员, label_theme=annual_vip), avatar_subscript=1, nickname_color=#FB7299)
3.2 用户粉丝数据获取
使用UID获取指定用户的粉丝信息
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
Relation list = client.relation().toFollowers(20165629L).list();
System.out.println(list);
System.out.println("粉丝总数 --> "+list.getTotal());
System.out.println("最新粉丝 --> "+list.getItems().get(0));
System.out.println("最新粉丝ID --> "+list.getItems().get(0).getMid());
System.out.println("最新粉丝名称 --> "+list.getItems().get(0).getUname());
System.out.println("最新粉丝签名 --> "+list.getItems().get(0).getSign());
System.out.println("粉丝列表 -->"+list.getItems());
// 分页获取
System.out.println(client.relation().toFollowers(20165629L).list(2L, 5L));
}
}
结果如下
Relation(total=7746656, re_version=2668395765, items=[Follower(mtime=1613215557, uname=合法收集雨水), Follower(mtime=1613215551, uname=兲孖灬彳亍), Follower(mtime=1613215533, uname=bili_81165375252), Follower(mtime=1613215521, uname=bili_地狱阎王), Follower(mtime=1613215500, uname=天蓝色天空的你), Follower(mtime=1613215494, uname=在下别亦楠), Follower(mtime=1613215484, uname=bili_84916673050), Follower(mtime=1613215469, uname=徐龙芮), Follower(mtime=1613215462, uname=阿qia威), Follower(mtime=1613215438, uname=俞白同学), Follower(mtime=1613215394, uname=饭黎爱吃椰子), Follower(mtime=1613215351, uname=无住的心), Follower(mtime=1613215350, uname=Study_Committee), Follower(mtime=1613215347, uname=快活的阿柴汪), Follower(mtime=1613215337, uname=往事如烟流年), Follower(mtime=1613215332, uname=尼龙瞎), Follower(mtime=1613215330, uname=二哈哈哈叫), Follower(mtime=1613215320, uname=bili_95463935507), Follower(mtime=1613215281, uname=罗水水要加油), Follower(mtime=1613215266, uname=bili_43079631053), Follower(mtime=1613215255, uname=德意志111米卡), Follower(mtime=1613215248, uname=DK逍白衣), Follower(mtime=1613215227, uname=柚子哥哥家的兔叽), Follower(mtime=1613215226, uname=派大星爱吹泡泡), Follower(mtime=1613215218, uname=viola47), Follower(mtime=1613215204, uname=WHY笔巴卜), Follower(mtime=1613215198, uname=bili_46704847334), Follower(mtime=1613215197, uname=bili_42344310679), Follower(mtime=1613215182, uname=bili_24315745446), Follower(mtime=1613215180, uname=普通奶茶), Follower(mtime=1613215153, uname=怎样K), Follower(mtime=1613215131, uname=缃罗_), Follower(mtime=1613215119, uname=晴破长空), Follower(mtime=1613215071, uname=bili_18668806817), Follower(mtime=1613215059, uname=-张三的父亲_), Follower(mtime=1613215053, uname=Yachtsy), Follower(mtime=1613215048, uname=厨力准备-安应景), Follower(mtime=1613215045, uname=k_清以), Follower(mtime=1613215035, uname=梵雅清音), Follower(mtime=1613215028, uname=西北笙z), Follower(mtime=1613214985, uname=Performance艺术家), Follower(mtime=1613214956, uname=bili_84055320353), Follower(mtime=1613214946, uname=璇一kinal), Follower(mtime=1613214935, uname=Kirwtr), Follower(mtime=1613214933, uname=bili_58334347829), Follower(mtime=1613214913, uname=吉良吉影de奇妙冒险), Follower(mtime=1613214904, uname=脑子开过光的小查), Follower(mtime=1613214892, uname=66722378207_bili), Follower(mtime=1613214886, uname=asdnaruto), Follower(mtime=1613214883, uname=人生_就图一个成果)])
粉丝总数 --> 7746656
最新粉丝 --> Follower(mtime=1613215557, uname=合法收集雨水)
最新粉丝ID --> 439154941
最新粉丝名称 --> 合法收集雨水
最新粉丝签名 --> 行万里路,读万卷书。
粉丝列表 -->[Follower(mtime=1613215557, uname=合法收集雨水), Follower(mtime=1613215551, uname=兲孖灬彳亍), Follower(mtime=1613215533, uname=bili_81165375252), Follower(mtime=1613215521, uname=bili_地狱阎王), Follower(mtime=1613215500, uname=天蓝色天空的你), Follower(mtime=1613215494, uname=在下别亦楠), Follower(mtime=1613215484, uname=bili_84916673050), Follower(mtime=1613215469, uname=徐龙芮), Follower(mtime=1613215462, uname=阿qia威), Follower(mtime=1613215438, uname=俞白同学), Follower(mtime=1613215394, uname=饭黎爱吃椰子), Follower(mtime=1613215351, uname=无住的心), Follower(mtime=1613215350, uname=Study_Committee), Follower(mtime=1613215347, uname=快活的阿柴汪), Follower(mtime=1613215337, uname=往事如烟流年), Follower(mtime=1613215332, uname=尼龙瞎), Follower(mtime=1613215330, uname=二哈哈哈叫), Follower(mtime=1613215320, uname=bili_95463935507), Follower(mtime=1613215281, uname=罗水水要加油), Follower(mtime=1613215266, uname=bili_43079631053), Follower(mtime=1613215255, uname=德意志111米卡), Follower(mtime=1613215248, uname=DK逍白衣), Follower(mtime=1613215227, uname=柚子哥哥家的兔叽), Follower(mtime=1613215226, uname=派大星爱吹泡泡), Follower(mtime=1613215218, uname=viola47), Follower(mtime=1613215204, uname=WHY笔巴卜), Follower(mtime=1613215198, uname=bili_46704847334), Follower(mtime=1613215197, uname=bili_42344310679), Follower(mtime=1613215182, uname=bili_24315745446), Follower(mtime=1613215180, uname=普通奶茶), Follower(mtime=1613215153, uname=怎样K), Follower(mtime=1613215131, uname=缃罗_), Follower(mtime=1613215119, uname=晴破长空), Follower(mtime=1613215071, uname=bili_18668806817), Follower(mtime=1613215059, uname=-张三的父亲_), Follower(mtime=1613215053, uname=Yachtsy), Follower(mtime=1613215048, uname=厨力准备-安应景), Follower(mtime=1613215045, uname=k_清以), Follower(mtime=1613215035, uname=梵雅清音), Follower(mtime=1613215028, uname=西北笙z), Follower(mtime=1613214985, uname=Performance艺术家), Follower(mtime=1613214956, uname=bili_84055320353), Follower(mtime=1613214946, uname=璇一kinal), Follower(mtime=1613214935, uname=Kirwtr), Follower(mtime=1613214933, uname=bili_58334347829), Follower(mtime=1613214913, uname=吉良吉影de奇妙冒险), Follower(mtime=1613214904, uname=脑子开过光的小查), Follower(mtime=1613214892, uname=66722378207_bili), Follower(mtime=1613214886, uname=asdnaruto), Follower(mtime=1613214883, uname=人生_就图一个成果)]
Relation(total=7746656, re_version=4149428170, items=[Follower(mtime=1613215533, uname=bili_81165375252), Follower(mtime=1613215521, uname=bili_地狱阎王)])
3.3 获取用户动态列表
目前支持单次获取和分页获取,使用案例代码如下,支持动态类型包括普通内容,视频\小视频,转发动态
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
// 获取前6条
DynamicItems list = client.dynamic().withHostUid(20165629L).list();
System.out.println(list);
System.out.println("是还有动态--> " + (list.getHasMore() == 1));
// 动态集合
List<Dynamic> items = list.getItems();
System.out.println(items);
// 最新一条
Dynamic dynamic = items.get(0);
System.out.println("动态类型-->" + dynamic.getType());
// 动态数据
DynamicData data = dynamic.getData();
System.out.println(data);
System.out.println("动态阅读-->" + data.getView());
System.out.println("动态点赞-->" + data.getLike());
System.out.println("动态转发-->" + data.getRepost());
// 动态详情(普通类型)
DynamicDetail detail = dynamic.getDetail();
System.out.println(detail); // 若非普通类型为null
if (detail != null) {
// 动态包含的图片
List<Picture> pictures = detail.getPictures();
System.out.println(pictures);
}
// 动态视频(视频类型)
Video video = dynamic.getVideo();
System.out.println("动态内容" + video.getDynamic());
System.out.println("视频说明" + video.getDesc());
// 获取之后六条---- 以此类推
DynamicItems list2 = client.dynamic().withHostUid(20165629L).list(list.getNextOffset());
System.out.println(list2);
}
}
结果如下
因较长仅放出截图
3.4 动态分析工具
该工具可以获取用户全部动态,并对动态进行批量统计和分类,耗时较长
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
// 动态分类信息
DynamicSortInfo dynamicSortInfo = DynamicTools.queryData(client, 20165629L);
// DynamicTools.queryAll(client, 20165629L); // 获取全部动态 但不分析
System.out.println(dynamicSortInfo);
List<Dynamic> dynamics = dynamicSortInfo.getDynamics();// 获取该用户的全部动态
System.out.println(dynamics);
System.out.println("用户总动态数量--> " + dynamicSortInfo.getCount());
System.out.println("用户总动态阅读--> " + dynamicSortInfo.getViewAll());
System.out.println("用户总动态点赞--> " + dynamicSortInfo.getLikeAll());
System.out.println("用户总动态评论--> " + dynamicSortInfo.getCommentAll());
System.out.println("用户总动态转发--> " + dynamicSortInfo.getRepostAll());
System.out.println("用户总动态硬币--> " + dynamicSortInfo.getCoinAll());
// 动态分类绘制
Map<Dynamic.DType, DynamicInfo> sortInfo = dynamicSortInfo.getSortInfo();
sortInfo.forEach((type, dynamicInfo) -> {
System.out.println(type + "类型总硬币--> " + dynamicInfo.getCoinAll());
System.out.println(type + "类型总点赞--> " + dynamicInfo.getLikeAll());
System.out.println(type + "类型总转发--> " + dynamicInfo.getRepostAll());
System.out.println(type + "类型总评论--> " + dynamicInfo.getCommentAll());
System.out.println(type + "类型总阅读--> " + dynamicInfo.getViewAll());
System.out.println(type + "类型总数量--> " + dynamicInfo.getCount());
// List<Dynamic> dynamics = dynamicInfo.getDynamics();// 获取该用户全部当前类型的动态
});
}
}
结果如下
3.5 获取指定动态
使用动态ID(网页动态详情地址或使用上述3.3或3.4中获取的MID)来获取某一特定动态详情
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
// 获取指定动态
Dynamic dynamic = client.dynamic().withDynamicId(490734987491008394L).get();
// 动态类型和作者
System.out.println("类型为--> "+ dynamic.getType());
System.out.println("作者为--> "+ dynamic.getName());
System.out.println("作者ID为--> "+ dynamic.getUid());
// 指定动态数据
DynamicData data = dynamic.getData();
System.out.println(data);
System.out.println("动态阅读--> "+data.getView());
System.out.println("动态点赞--> "+data.getLike());
System.out.println("动态转发--> "+data.getRepost());
// 指定动态内容
DynamicDetail detail = dynamic.getDetail();
System.out.println("动态类容--> "+detail.getDescription());
System.out.println("动态发布时间--> "+detail.getUpload_time());
// 获取动态图片
List<Picture> pictures = detail.getPictures();
System.out.println(pictures);
}
}
结果如下
3.6 获取指定视频信息
public class Main {
public static void main(String[] args) {
BiliClient client = BiliClientFactor.getClient();
// 工具BV
Video video = client.video().withBvid("BV1hz4y1k7jf").get();
// Video video = client.video().withBvid(585465570L).get(); // 工具AV获取
System.out.println(video);
System.out.println("视频AV号--> " + video.getAid());
System.out.println("视频BV号--> " + video.getBvid());
System.out.println("视频封面--> " + video.getPic());
System.out.println("视频标题--> " + video.getTitle());
System.out.println("视频时长(秒)--> " + video.getCtime());
System.out.println("视频合作者信息--> " + video.getStaff());
// 弹幕 播放等
System.out.println("视频播放数据--> " + video.getStat());
System.out.println("视频发布时动态信息--> " + video.getDynamic());
System.out.println("视频详情信息--> " + video.getDesc());
}
}
运行结果
4. 异常分析
4.1 SLF4J错误
异常发生于运行时候,异常信息如下
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
该异常的原因是,项目中部分错误和信息使用Lombok的日志注解,并进行日志打印,在使用时候未引入日志框架导致,若无需日志可以无视该问题,不影响使用,若需要解决,可以加入以下依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
4.2 Maven无法加载依赖
错误为添加依赖后,刷新Maven加载,报红并提示找不到此依赖,原因是无法访问中央仓库或代理仓库中还为镜像此依赖,可以在Maven配置文件settings.xml
加入如下Maven中央仓库站点
<mirrors>
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
<mirrors>
结语
以上仅仅包含该包的部分功能,以及部分功能的部分使用情景。
该项目源码已经上传GitHub开源,点击跳转
该项目已经上传Maven中央仓库,点击跳转