<div align="center">
# StarBotCore
[![PyPI](https://img.shields.io/pypi/v/starbot-bilibili-core)](https://pypi.org/project/starbot-bilibili-core)
[![Python](https://img.shields.io/badge/python-3.10%20|%203.11-blue)](https://www.python.org)
[![License](https://img.shields.io/github/license/Starlwr/StarBotCore)](https://github.com/Starlwr/StarBotCore/blob/master/LICENSE)
[![STARS](https://img.shields.io/github/stars/Starlwr/StarBotCore?color=yellow&label=Stars)](https://github.com/Starlwr/StarBotCore/stargazers)
**一个优雅的异步 bilibili 直播间监听库**
</div>
## 特性
* 极简代码调用,可快速实现高级功能
* 细化原始事件,自动区分文字弹幕与表情弹幕,免费礼物、付费礼物与盲盒礼物等
* 基于分级消息主题的订阅发布模式,可灵活监听单个直播间或多个直播间
* 自动解析原始数据并包装为事件发布,无需手动使用代码处理原始数据
## 快速开始
### 安装
```shell
pip install starbot-bilibili-core
```
### 代码框架
在开始编写代码前,需要引入异步任务执行器 executor,并执行初始化操作
```python
from starbot_executor import executor
async def main():
# 业务逻辑
pass
loop = executor.init()
loop.run_until_complete(main())
loop.run_forever()
```
### 监听直播间
引入直播间事件监听器 listener 后,仅须一行代码即可添加监听直播间,并自动持续发布该直播间的相关事件
```python
await listener.add(room_id=22889484)
```
### 已支持的事件列表
- 连接成功事件(ConnectedEvent)
- 断开连接事件(DisconnectedEvent)
- 心跳响应超时事件(TimeoutEvent)
- 开播事件(LiveOnEvent)
- 下播事件(LiveOffEvent)
- 文字弹幕事件(DanmuEvent)
- 大表情弹幕事件(EmojiEvent)
- 免费礼物事件(SilverGiftEvent)
- 付费礼物事件(GoldGiftEvent)
- 盲盒礼物事件(BlindGiftEvent)
- 醒目留言事件(SuperChatEvent)
- 开通舰长事件(CaptainEvent)
- 开通提督事件(CommanderEvent)
- 开通总督事件(GovernorEvent)
- 进房事件(EnterRoomEvent)
- 关注事件(FollowEvent)
- 观看过人数更新事件(WatchedUpdateEvent)
- 点赞事件(LikeClickEvent)
- 点赞数更新事件(LikeUpdateEvent)
### 订阅-发布机制
消息主题为层级结构,较高层级的消息主题可同时监听到较低层级的事件,举例如下:
弹幕事件对应消息主题为三层,一级主题为:EventType.LiveEvent,二级主题为:LiveEvent.DanmuEvent,三级主题为:房间号
使用 **@executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent, 房间号)** 装饰的方法,仅可监听到**相应房间号**的**弹幕**事件
使用 **@executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent)** 装饰的方法,可监听到**所有房间**的**弹幕**事件
使用 **@executor.on(EventType.LiveEvent)** 或 **@executor.on()** 装饰的方法,可监听到**所有房间**的**所有**事件
### 完整示例
```python
from loguru import logger
from starbot_executor import executor
from starbot_core import listener, EventType, LiveEvent, ConnectedEvent, DisconnectedEvent, DanmuEvent, BlindGiftEvent
from starbot_core.exception.LoginException import LoginException
from starbot_core.utils.network import credential
async def main():
# 登录 B 站账号,不登录账号无法抓取完整数据,Cookie 获取方式:https://bot.starlwr.com/depoly/document
try:
await credential.login(
sessdata="在此填入 SESSDATA 字段",
bili_jct="在此填入 bili_jct 字段",
buvid3="在此填入 buvid3 字段"
)
except LoginException as e:
logger.error(e.msg, e)
return
# 例 1: 监听所有直播间的所有事件
@executor.on()
async def on_all(subjects, event):
logger.info(f"{subjects}: {event}")
# 例 2: 监听所有直播间的连接成功事件
@executor.on(EventType.LiveEvent, LiveEvent.ConnectedEvent)
async def on_connected(event: ConnectedEvent):
logger.success(f"已成功连接到 {event.source.uname} 的直播间 {event.source.room_id}")
# 例 3: 监听所有直播间的断开连接事件
@executor.on(EventType.LiveEvent, LiveEvent.DisconnectedEvent)
async def on_disconnected(event: DisconnectedEvent):
logger.opt(colors=True).info(f"已断开连接 <cyan>{event.source.uname}</> 的直播间 <cyan>{event.source.room_id}</>")
# 例 4: 监听 22889484 直播间的弹幕事件
@executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent, 22889484)
async def on_danmu(event: DanmuEvent):
if event.fans_medal is None:
logger.info(f"{event.sender.uname} (无粉丝勋章): {event.content}")
else:
logger.info(
f"{event.sender.uname} (Lv {event.fans_medal.level} - {event.fans_medal.name}): {event.content}")
# 例 5: 监听所有直播间的盲盒礼物事件
@executor.on(EventType.LiveEvent, LiveEvent.BlindGiftEvent)
async def on_blind_gift(event: BlindGiftEvent):
if event.profit >= 0:
logger.info(f"{event.sender.uname} 通过 {event.blind_name} 开出了 {event.count} 个 {event.gift_name}, 赚了 {abs(event.profit)} 元")
else:
logger.info(f"{event.sender.uname} 通过 {event.blind_name} 开出了 {event.count} 个 {event.gift_name}, 亏了 {abs(event.profit)} 元")
# 循环添加监听直播间
room_ids = (22889484, 55634, 21013446)
for room_id in room_ids:
await listener.add(room_id=room_id)
listener.auto_complete = True # 设置为 True 可以获得更完整的信息(部分事件中的头像、昵称等),但会因网络请求导致一定的延迟,请酌情使用
loop = executor.init()
loop.run_until_complete(main())
loop.run_forever()
```
## 相关项目
* [StarBotExecutor](https://github.com/Starlwr/StarBotExecutor): 一个基于订阅发布模式的异步执行器
## 鸣谢
* [bilibili-api](https://github.com/MoyuScript/bilibili-api): 哔哩哔哩的 API 调用模块
* [bilibili-API-collect](https://github.com/SocialSisterYi/bilibili-API-collect): 哔哩哔哩 API 收集整理
Raw data
{
"_id": null,
"home_page": "https://github.com/Starlwr/StarBotCore",
"name": "starbot-bilibili-core",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "starbot, bilibili, bot",
"author": "Starlwr",
"author_email": "lwr1104@qq.com",
"download_url": "https://files.pythonhosted.org/packages/b6/62/c81224281236b69c36a01d8c44ac0a8812ef5b0cae5e14862f1f74d993c0/starbot_bilibili_core-1.3.0.tar.gz",
"platform": null,
"description": "<div align=\"center\">\n\n# StarBotCore\n\n[![PyPI](https://img.shields.io/pypi/v/starbot-bilibili-core)](https://pypi.org/project/starbot-bilibili-core)\n[![Python](https://img.shields.io/badge/python-3.10%20|%203.11-blue)](https://www.python.org)\n[![License](https://img.shields.io/github/license/Starlwr/StarBotCore)](https://github.com/Starlwr/StarBotCore/blob/master/LICENSE)\n[![STARS](https://img.shields.io/github/stars/Starlwr/StarBotCore?color=yellow&label=Stars)](https://github.com/Starlwr/StarBotCore/stargazers)\n\n**\u4e00\u4e2a\u4f18\u96c5\u7684\u5f02\u6b65 bilibili \u76f4\u64ad\u95f4\u76d1\u542c\u5e93**\n</div>\n\n## \u7279\u6027\n\n* \u6781\u7b80\u4ee3\u7801\u8c03\u7528\uff0c\u53ef\u5feb\u901f\u5b9e\u73b0\u9ad8\u7ea7\u529f\u80fd\n* \u7ec6\u5316\u539f\u59cb\u4e8b\u4ef6\uff0c\u81ea\u52a8\u533a\u5206\u6587\u5b57\u5f39\u5e55\u4e0e\u8868\u60c5\u5f39\u5e55\uff0c\u514d\u8d39\u793c\u7269\u3001\u4ed8\u8d39\u793c\u7269\u4e0e\u76f2\u76d2\u793c\u7269\u7b49\n* \u57fa\u4e8e\u5206\u7ea7\u6d88\u606f\u4e3b\u9898\u7684\u8ba2\u9605\u53d1\u5e03\u6a21\u5f0f\uff0c\u53ef\u7075\u6d3b\u76d1\u542c\u5355\u4e2a\u76f4\u64ad\u95f4\u6216\u591a\u4e2a\u76f4\u64ad\u95f4\n* \u81ea\u52a8\u89e3\u6790\u539f\u59cb\u6570\u636e\u5e76\u5305\u88c5\u4e3a\u4e8b\u4ef6\u53d1\u5e03\uff0c\u65e0\u9700\u624b\u52a8\u4f7f\u7528\u4ee3\u7801\u5904\u7406\u539f\u59cb\u6570\u636e\n\n## \u5feb\u901f\u5f00\u59cb\n### \u5b89\u88c5\n\n```shell\npip install starbot-bilibili-core\n```\n\n### \u4ee3\u7801\u6846\u67b6\n\n\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u524d\uff0c\u9700\u8981\u5f15\u5165\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5668 executor\uff0c\u5e76\u6267\u884c\u521d\u59cb\u5316\u64cd\u4f5c\n\n```python\nfrom starbot_executor import executor\n\nasync def main():\n # \u4e1a\u52a1\u903b\u8f91\n pass\n\nloop = executor.init()\nloop.run_until_complete(main())\nloop.run_forever()\n```\n\n### \u76d1\u542c\u76f4\u64ad\u95f4\n\n\u5f15\u5165\u76f4\u64ad\u95f4\u4e8b\u4ef6\u76d1\u542c\u5668 listener \u540e\uff0c\u4ec5\u987b\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u6dfb\u52a0\u76d1\u542c\u76f4\u64ad\u95f4\uff0c\u5e76\u81ea\u52a8\u6301\u7eed\u53d1\u5e03\u8be5\u76f4\u64ad\u95f4\u7684\u76f8\u5173\u4e8b\u4ef6\n\n```python\nawait listener.add(room_id=22889484)\n```\n\n### \u5df2\u652f\u6301\u7684\u4e8b\u4ef6\u5217\u8868\n\n- \u8fde\u63a5\u6210\u529f\u4e8b\u4ef6\uff08ConnectedEvent\uff09\n- \u65ad\u5f00\u8fde\u63a5\u4e8b\u4ef6\uff08DisconnectedEvent\uff09\n- \u5fc3\u8df3\u54cd\u5e94\u8d85\u65f6\u4e8b\u4ef6\uff08TimeoutEvent\uff09\n- \u5f00\u64ad\u4e8b\u4ef6\uff08LiveOnEvent\uff09\n- \u4e0b\u64ad\u4e8b\u4ef6\uff08LiveOffEvent\uff09\n- \u6587\u5b57\u5f39\u5e55\u4e8b\u4ef6\uff08DanmuEvent\uff09\n- \u5927\u8868\u60c5\u5f39\u5e55\u4e8b\u4ef6\uff08EmojiEvent\uff09\n- \u514d\u8d39\u793c\u7269\u4e8b\u4ef6\uff08SilverGiftEvent\uff09\n- \u4ed8\u8d39\u793c\u7269\u4e8b\u4ef6\uff08GoldGiftEvent\uff09\n- \u76f2\u76d2\u793c\u7269\u4e8b\u4ef6\uff08BlindGiftEvent\uff09\n- \u9192\u76ee\u7559\u8a00\u4e8b\u4ef6\uff08SuperChatEvent\uff09\n- \u5f00\u901a\u8230\u957f\u4e8b\u4ef6\uff08CaptainEvent\uff09\n- \u5f00\u901a\u63d0\u7763\u4e8b\u4ef6\uff08CommanderEvent\uff09\n- \u5f00\u901a\u603b\u7763\u4e8b\u4ef6\uff08GovernorEvent\uff09\n- \u8fdb\u623f\u4e8b\u4ef6\uff08EnterRoomEvent\uff09\n- \u5173\u6ce8\u4e8b\u4ef6\uff08FollowEvent\uff09\n- \u89c2\u770b\u8fc7\u4eba\u6570\u66f4\u65b0\u4e8b\u4ef6\uff08WatchedUpdateEvent\uff09\n- \u70b9\u8d5e\u4e8b\u4ef6\uff08LikeClickEvent\uff09\n- \u70b9\u8d5e\u6570\u66f4\u65b0\u4e8b\u4ef6\uff08LikeUpdateEvent\uff09\n\n### \u8ba2\u9605-\u53d1\u5e03\u673a\u5236\n\n\u6d88\u606f\u4e3b\u9898\u4e3a\u5c42\u7ea7\u7ed3\u6784\uff0c\u8f83\u9ad8\u5c42\u7ea7\u7684\u6d88\u606f\u4e3b\u9898\u53ef\u540c\u65f6\u76d1\u542c\u5230\u8f83\u4f4e\u5c42\u7ea7\u7684\u4e8b\u4ef6\uff0c\u4e3e\u4f8b\u5982\u4e0b\uff1a \n\n\u5f39\u5e55\u4e8b\u4ef6\u5bf9\u5e94\u6d88\u606f\u4e3b\u9898\u4e3a\u4e09\u5c42\uff0c\u4e00\u7ea7\u4e3b\u9898\u4e3a\uff1aEventType.LiveEvent\uff0c\u4e8c\u7ea7\u4e3b\u9898\u4e3a\uff1aLiveEvent.DanmuEvent\uff0c\u4e09\u7ea7\u4e3b\u9898\u4e3a\uff1a\u623f\u95f4\u53f7 \n\u4f7f\u7528 **@executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent, \u623f\u95f4\u53f7)** \u88c5\u9970\u7684\u65b9\u6cd5\uff0c\u4ec5\u53ef\u76d1\u542c\u5230**\u76f8\u5e94\u623f\u95f4\u53f7**\u7684**\u5f39\u5e55**\u4e8b\u4ef6 \n\u4f7f\u7528 **@executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent)** \u88c5\u9970\u7684\u65b9\u6cd5\uff0c\u53ef\u76d1\u542c\u5230**\u6240\u6709\u623f\u95f4**\u7684**\u5f39\u5e55**\u4e8b\u4ef6 \n\u4f7f\u7528 **@executor.on(EventType.LiveEvent)** \u6216 **@executor.on()** \u88c5\u9970\u7684\u65b9\u6cd5\uff0c\u53ef\u76d1\u542c\u5230**\u6240\u6709\u623f\u95f4**\u7684**\u6240\u6709**\u4e8b\u4ef6\n\n### \u5b8c\u6574\u793a\u4f8b\n\n```python\nfrom loguru import logger\nfrom starbot_executor import executor\n\nfrom starbot_core import listener, EventType, LiveEvent, ConnectedEvent, DisconnectedEvent, DanmuEvent, BlindGiftEvent\nfrom starbot_core.exception.LoginException import LoginException\nfrom starbot_core.utils.network import credential\n\n\nasync def main():\n # \u767b\u5f55 B \u7ad9\u8d26\u53f7\uff0c\u4e0d\u767b\u5f55\u8d26\u53f7\u65e0\u6cd5\u6293\u53d6\u5b8c\u6574\u6570\u636e\uff0cCookie \u83b7\u53d6\u65b9\u5f0f\uff1ahttps://bot.starlwr.com/depoly/document\n try:\n await credential.login(\n sessdata=\"\u5728\u6b64\u586b\u5165 SESSDATA \u5b57\u6bb5\",\n bili_jct=\"\u5728\u6b64\u586b\u5165 bili_jct \u5b57\u6bb5\",\n buvid3=\"\u5728\u6b64\u586b\u5165 buvid3 \u5b57\u6bb5\"\n )\n except LoginException as e:\n logger.error(e.msg, e)\n return\n\n # \u4f8b 1: \u76d1\u542c\u6240\u6709\u76f4\u64ad\u95f4\u7684\u6240\u6709\u4e8b\u4ef6\n @executor.on()\n async def on_all(subjects, event):\n logger.info(f\"{subjects}: {event}\")\n\n # \u4f8b 2: \u76d1\u542c\u6240\u6709\u76f4\u64ad\u95f4\u7684\u8fde\u63a5\u6210\u529f\u4e8b\u4ef6\n @executor.on(EventType.LiveEvent, LiveEvent.ConnectedEvent)\n async def on_connected(event: ConnectedEvent):\n logger.success(f\"\u5df2\u6210\u529f\u8fde\u63a5\u5230 {event.source.uname} \u7684\u76f4\u64ad\u95f4 {event.source.room_id}\")\n\n # \u4f8b 3: \u76d1\u542c\u6240\u6709\u76f4\u64ad\u95f4\u7684\u65ad\u5f00\u8fde\u63a5\u4e8b\u4ef6\n @executor.on(EventType.LiveEvent, LiveEvent.DisconnectedEvent)\n async def on_disconnected(event: DisconnectedEvent):\n logger.opt(colors=True).info(f\"\u5df2\u65ad\u5f00\u8fde\u63a5 <cyan>{event.source.uname}</> \u7684\u76f4\u64ad\u95f4 <cyan>{event.source.room_id}</>\")\n\n # \u4f8b 4: \u76d1\u542c 22889484 \u76f4\u64ad\u95f4\u7684\u5f39\u5e55\u4e8b\u4ef6\n @executor.on(EventType.LiveEvent, LiveEvent.DanmuEvent, 22889484)\n async def on_danmu(event: DanmuEvent):\n if event.fans_medal is None:\n logger.info(f\"{event.sender.uname} (\u65e0\u7c89\u4e1d\u52cb\u7ae0): {event.content}\")\n else:\n logger.info(\n f\"{event.sender.uname} (Lv {event.fans_medal.level} - {event.fans_medal.name}): {event.content}\")\n\n # \u4f8b 5: \u76d1\u542c\u6240\u6709\u76f4\u64ad\u95f4\u7684\u76f2\u76d2\u793c\u7269\u4e8b\u4ef6\n @executor.on(EventType.LiveEvent, LiveEvent.BlindGiftEvent)\n async def on_blind_gift(event: BlindGiftEvent):\n if event.profit >= 0:\n logger.info(f\"{event.sender.uname} \u901a\u8fc7 {event.blind_name} \u5f00\u51fa\u4e86 {event.count} \u4e2a {event.gift_name}, \u8d5a\u4e86 {abs(event.profit)} \u5143\")\n else:\n logger.info(f\"{event.sender.uname} \u901a\u8fc7 {event.blind_name} \u5f00\u51fa\u4e86 {event.count} \u4e2a {event.gift_name}, \u4e8f\u4e86 {abs(event.profit)} \u5143\")\n\n # \u5faa\u73af\u6dfb\u52a0\u76d1\u542c\u76f4\u64ad\u95f4\n room_ids = (22889484, 55634, 21013446)\n for room_id in room_ids:\n await listener.add(room_id=room_id)\n\n\nlistener.auto_complete = True # \u8bbe\u7f6e\u4e3a True \u53ef\u4ee5\u83b7\u5f97\u66f4\u5b8c\u6574\u7684\u4fe1\u606f\uff08\u90e8\u5206\u4e8b\u4ef6\u4e2d\u7684\u5934\u50cf\u3001\u6635\u79f0\u7b49\uff09\uff0c\u4f46\u4f1a\u56e0\u7f51\u7edc\u8bf7\u6c42\u5bfc\u81f4\u4e00\u5b9a\u7684\u5ef6\u8fdf\uff0c\u8bf7\u914c\u60c5\u4f7f\u7528\nloop = executor.init()\nloop.run_until_complete(main())\nloop.run_forever()\n```\n\n## \u76f8\u5173\u9879\u76ee\n\n* [StarBotExecutor](https://github.com/Starlwr/StarBotExecutor): \u4e00\u4e2a\u57fa\u4e8e\u8ba2\u9605\u53d1\u5e03\u6a21\u5f0f\u7684\u5f02\u6b65\u6267\u884c\u5668\n\n## \u9e23\u8c22\n\n* [bilibili-api](https://github.com/MoyuScript/bilibili-api): \u54d4\u54e9\u54d4\u54e9\u7684 API \u8c03\u7528\u6a21\u5757\n* [bilibili-API-collect](https://github.com/SocialSisterYi/bilibili-API-collect): \u54d4\u54e9\u54d4\u54e9 API \u6536\u96c6\u6574\u7406\n",
"bugtrack_url": null,
"license": "AGPL-3.0-only",
"summary": "\u4e00\u4e2a\u4f18\u96c5\u7684\u5f02\u6b65 bilibili \u76f4\u64ad\u95f4\u76d1\u542c\u5e93",
"version": "1.3.0",
"project_urls": {
"Homepage": "https://github.com/Starlwr/StarBotCore",
"Repository": "https://github.com/Starlwr/StarBotCore"
},
"split_keywords": [
"starbot",
" bilibili",
" bot"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "43e7da51601ce76061976a85a8de6b4df26eb2d87f89267810aa12058b5303aa",
"md5": "af13921b8ec0e69170ef3eb7a4d7e70c",
"sha256": "a2cf9074e267081b30c6579af5ba751e52dc46a03e948cd0313632593f4b797c"
},
"downloads": -1,
"filename": "starbot_bilibili_core-1.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "af13921b8ec0e69170ef3eb7a4d7e70c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 31507,
"upload_time": "2024-04-04T14:31:50",
"upload_time_iso_8601": "2024-04-04T14:31:50.676862Z",
"url": "https://files.pythonhosted.org/packages/43/e7/da51601ce76061976a85a8de6b4df26eb2d87f89267810aa12058b5303aa/starbot_bilibili_core-1.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b662c81224281236b69c36a01d8c44ac0a8812ef5b0cae5e14862f1f74d993c0",
"md5": "48732e2250dcaa0d139693b4d77e02ba",
"sha256": "a83e1b3ec4c423b2081a30324da30b35c2e2866e84b6996f36948e3c7e6867fe"
},
"downloads": -1,
"filename": "starbot_bilibili_core-1.3.0.tar.gz",
"has_sig": false,
"md5_digest": "48732e2250dcaa0d139693b4d77e02ba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 47461,
"upload_time": "2024-04-04T14:31:52",
"upload_time_iso_8601": "2024-04-04T14:31:52.872992Z",
"url": "https://files.pythonhosted.org/packages/b6/62/c81224281236b69c36a01d8c44ac0a8812ef5b0cae5e14862f1f74d993c0/starbot_bilibili_core-1.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-04 14:31:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Starlwr",
"github_project": "StarBotCore",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "starbot-bilibili-core"
}