Skip to content

Commit

Permalink
chore: Merge pull request #866 from xianrenzhou/xianrenzhou
Browse files Browse the repository at this point in the history
search.search_by_type 增加功能:可以按照时间段查询 & 新增 to_timestamps 函数
  • Loading branch information
Nemo2011 authored Dec 7, 2024
2 parents 2617a06 + 0196376 commit 1c0a4a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
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' 格式,并且起始时间早于结束时间。")
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

0 comments on commit 1c0a4a7

Please sign in to comment.