Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
Nemo2011 committed Dec 7, 2024
2 parents 41f356c + 15d855c commit 6897e13
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
28 changes: 28 additions & 0 deletions bilibili_api/data/api/live.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
},
"comment": "获取房间信息(真实房间号,封禁情况等)"
},
"emoticons": {
"url": "https://api.live.bilibili.com/xlive/web-ucenter/v2/emoticon/GetEmoticons",
"method": "GET",
"verify": true,
"params": {
"platform": "const str: pc",
"room_id": "int: 房间号"
},
"comment": "获取当前用户在本房间可用的表情包"
},
"danmu_info": {
"url": "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo",
"method": "GET",
Expand Down Expand Up @@ -301,6 +311,24 @@
"csrf,csrf_token": "str: 要给两个"
},
"comment": "发送直播间弹幕,有的参数不确定因为自己不搞这块没权限发一些样式的弹幕"
},
"send_emoticon": {
"url": "https://api.live.bilibili.com/msg/send",
"method": "POST",
"verify": true,
"params": {
"roomid": "int: 真实房间号",
"color": "int: 十进制颜色,有权限限制",
"fontsize": "int: 字体大小,默认 25",
"mode": "int: 弹幕模式,1 飞行 5 顶部 4 底部",
"msg": "str: 表情包代号",
"rnd": "int: 当前时间戳",
"bubble": "int: 默认 0,功能不知",
"dm_type": "int: 默认 1,功能不知",
"emoticonOptions": " 默认 [object Object] 功能不知",
"csrf,csrf_token": "str: 要给两个"
},
"comment": "发送直播间弹幕,有的参数不确定因为自己不搞这块没权限发一些样式的弹幕"
},
"send_popular_ticket": {
"url": "https://api.live.bilibili.com/xlive/general-interface/v1/rank/popularRankFreeScoreIncr",
Expand Down
49 changes: 48 additions & 1 deletion bilibili_api/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ async def get_room_play_info(self) -> dict:
self.__ruid = resp["uid"]
return resp

async def get_emoticons(self) -> dict:
"""
获取本房间可用表情包
Returns:
dict: 调用 API 返回的结果
"""
api = API["info"]["emoticons"]
params = {
"platform": "pc",
"room_id": self.room_display_id,
}
resp = (
await Api(**api, credential=self.credential).update_params(**params).result
)
return resp

async def get_room_id(self) -> int:
"""
获取直播间 id
Expand Down Expand Up @@ -470,7 +487,7 @@ async def get_room_play_info_v2(
await Api(**api, credential=self.credential).update_params(**params).result
)

async def ban_user(self, uid: int, hour: int) -> dict:
async def ban_user(self, uid: int, hour: int = -1) -> dict:
"""
封禁用户
Expand Down Expand Up @@ -508,6 +525,7 @@ async def unban_user(self, uid: int) -> dict:
data = {
"room_id": self.room_display_id,
"tuid": uid,
"visit_id": "",
}
return await Api(**api, credential=self.credential).update_data(**data).result

Expand Down Expand Up @@ -541,6 +559,35 @@ async def send_danmaku(self, danmaku: Danmaku, room_id: int = None, reply_mid: i
if reply_mid: data["reply_mid"] = reply_mid
return await Api(**api, credential=self.credential).update_data(**data).result

async def send_emoticon(self, emoticon: Danmaku, room_id: int = None) -> dict:
"""
直播间发送表情包
Args:
emoticon (Danmaku): text为表情包代号
Returns:
dict: 调用 API 返回的结果
"""
self.credential.raise_for_no_sessdata()

api = API["operate"]["send_emoticon"]
if not room_id:
room_id = (await self.get_room_play_info())["room_id"]

data = {
"mode": emoticon.mode,
"msg": emoticon.text,
"roomid": room_id,
"bubble": 0,
"dm_type": 1,
"rnd": int(time.time()),
"color": int(emoticon.color, 16),
"fontsize": emoticon.font_size,
"emoticonOptions": '[object Object]'
}
return await Api(**api, credential=self.credential).update_data(**data).result

async def sign_up_dahanghai(self, task_id: int = 1447) -> dict:
"""
大航海签到
Expand Down
11 changes: 10 additions & 1 deletion bilibili_api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from enum import Enum
from typing import List, Union, Callable

from .utils.utils import to_timestamps
from .utils.utils import get_api
from .video_zone import VideoZoneTypes
from .utils.network import Api, get_session
Expand Down Expand Up @@ -193,6 +193,10 @@ async def search_by_type(
category_id (CategoryTypeArticle | CategoryTypePhoto | int | None, optional) : 专栏/相簿分区筛选,指定分类,只在相册和专栏类型下生效
time_range (int, optional) : 指定时间,自动转换到指定区间,只在视频类型下生效 有四种:10分钟以下,10-30分钟,30-60分钟,60分钟以上
time_start (str, optional) : 指定开始时间,与结束时间搭配使用,格式为:"YYYY-MM-DD"
time_end (str, optional) : 指定结束时间,与开始时间搭配使用,格式为:"YYYY-MM-DD"
video_zone_type (int | ZoneTypes | None, optional) : 话题类型,指定 tid (可使用 channel 模块查询)
Expand Down Expand Up @@ -254,6 +258,11 @@ async def search_by_type(
params["order_sort"] = order_sort
if debug_param_func:
debug_param_func(params)
# time setting
if time_start and time_end:
time_stamp = to_timestamps(time_start,time_end)
params["pubtime_begin_s"] = time_stamp[0]
params["pubtime_end_s"] = time_stamp[1]
api = API["search"]["web_search_by_type"]
return await Api(**api, wbi=True).update_params(**params).result

Expand Down
24 changes: 23 additions & 1 deletion bilibili_api/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import random
from typing import List, TypeVar
from ..exceptions import StatementException

from datetime import datetime

def get_api(field: str, *args) -> dict:
"""
Expand Down Expand Up @@ -210,3 +210,25 @@ def get_deviceid(separator: str = "-", is_lowercase: bool = False) -> str:
def raise_for_statement(statement: bool, msg: str="未满足条件") -> None:
if not statement:
raise StatementException(msg=msg)

def to_timestamps(time_start, time_end):
"""
将两个日期字符串转换为整数时间戳 (int) 元组,并验证时间顺序。
Returns:
tuple: (int,int)
"""
try:
# 将输入字符串解析为 datetime 对象
start_dt = datetime.strptime(time_start, "%Y-%m-%d")
end_dt = datetime.strptime(time_end, "%Y-%m-%d")

# 验证起始时间是否早于结束时间
if start_dt >= end_dt:
raise ValueError("起始时间必须早于结束时间。")

# 转换为时间戳并返回
return int(start_dt.timestamp()), int(end_dt.timestamp())
except ValueError as e:
# 捕获日期格式错误或自定义错误消息
raise ValueError(f"输入错误: {e}. 请确保使用 'YYYY-MM-DD' 格式,并且起始时间早于结束时间。")
23 changes: 23 additions & 0 deletions docs/modules/live.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ Events:



### async def get_emoticons()

获取本房间可用表情包



**Returns:** dict: 调用 API 返回的结果




### async def get_room_play_info()

获取房间信息(真实房间号,封禁情况等)
Expand Down Expand Up @@ -434,6 +445,18 @@ Events:



### async def send_emoticon()

直播间发送表情包


| name | type | description |
| - | - | - |
| emoticon | Danmaku | 弹幕类, text为表情包代号 |

**Returns:** dict: 调用 API 返回的结果



### async def send_gift_from_bag()

Expand Down
2 changes: 2 additions & 0 deletions docs/modules/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Ps: Api 中 的 order_sort 字段决定顺序还是倒序
| order_sort | Union[int, None] | 用户粉丝数及等级排序顺序 默认为0 由高到低:0 由低到高:1 |
| category_id | Union[CategoryTypeArticle, None] | 专栏/相簿分区筛选,指定分类,只在相册和专栏类型下生效 |
| time_range | Union[int, None] | 指定时间,自动转换到指定区间,只在视频类型下生效 有四种:10分钟以下,10-30分钟,30-60分钟,60分钟以上 |
| time_start | Union[str, None] | 指定搜索开始日期,与结束日期搭配使用,格式为:"YYYY-MM-DD" |
| time_end | Union[str, None] | 指定搜索结束日期,与开始日期搭配使用,格式为:"YYYY-MM-DD" |
| video_zone_type | Union[int, None] | 话题类型,指定 tid (可使用 channel 模块查询) |
| order_type | Union[OrderUser, None] | 排序分类类型 |
| keyword | str | 搜索关键词 |
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
aiohttp~=3.11.2
aiohttp~=3.11.9
beautifulsoup4~=4.12.3
colorama~=0.4.6
lxml~=5.3.0
pyyaml~=6.0
brotli~=1.1.0
httpx~=0.27.2
httpx~=0.28.0
qrcode~=8.0
APScheduler~=3.10.4
APScheduler~=3.11.0
rsa~=4.9
pillow~=11.0.0
tqdm~=4.67.0
yarl~=1.17.2
tqdm~=4.67.1
yarl~=1.18.3
pycryptodomex~=3.21.0
qrcode_terminal~=0.8
PyJWT~=2.10.0
7 changes: 7 additions & 0 deletions tests/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,10 @@ async def test_zf_get_get_popular_ticket_num():
async def test_zg_popular_rank_free_score_incr():
return await l.send_popular_ticket()


async def test_zh_get_emoticons():
return await l.get_emoticons()


async def test_zi_send_emoticon():
return await l.send_danmaku(Danmaku("official_147"))

0 comments on commit 6897e13

Please sign in to comment.