File size: 8,284 Bytes
0aee47a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
"""
bilibili_api.channel_series
用户合集与列表相关
"""
import json
from enum import Enum
from typing import List, Union, Optional
import httpx
from .utils.utils import get_api, raise_for_statement
from .utils.credential import Credential
from .utils.network import Api, HEADERS
API_USER = get_api("user")
API = get_api("channel-series")
channel_meta_cache = {}
class ChannelOrder(Enum):
"""
合集视频排序顺序。
+ DEFAULT: 默认排序
+ CHANGE : 升序排序
"""
DEFAULT = "false"
CHANGE = "true"
class ChannelSeriesType(Enum):
"""
合集与列表类型
+ SERIES: 相同视频分类
+ SEASON: 新概念多 P
**SEASON 类合集与列表名字为`合集·XXX`,请注意区别**
"""
SERIES = 0
SEASON = 1
class ChannelSeries:
"""
合集与列表类
Attributes:
credential (Credential): 凭据类. Defaults to None.
"""
def __init__(
self,
uid: int = -1,
type_: ChannelSeriesType = ChannelSeriesType.SERIES,
id_: int = -1,
credential: Union[Credential, None] = None,
):
"""
Args:
uid(int) : 用户 uid. Defaults to -1.
type_(ChannelSeriesType): 合集与列表类型. Defaults to ChannelSeriesType.SERIES.
id_(int) : season_id 或 series_id. Defaults to -1.
credential(Credential) : 凭证. Defaults to None.
"""
global channel_meta_cache
raise_for_statement(id_ != -1)
raise_for_statement(type_ != None)
from .user import User
self.__uid = uid
self.is_new = type_.value
self.id_ = id_
self.owner = User(self.__uid, credential=credential)
self.credential = credential
self.meta = None
if not f"{type_.value}-{id_}" in channel_meta_cache.keys():
if self.is_new:
api = API_USER["channel_series"]["season_info"]
params = {"season_id": self.id_}
else:
api = API_USER["channel_series"]["info"]
params = {"series_id": self.id_}
resp = Api(**api).update_params(**params).result_sync
if self.is_new:
self.meta = resp["info"]
self.meta["mid"] = resp["info"]["upper"]["mid"]
self.__uid = self.meta["mid"]
self.owner = User(self.__uid, credential=credential)
else:
self.meta = resp["meta"]
self.__uid = self.meta["mid"]
self.owner = User(self.__uid, credential=credential)
else:
self.meta = channel_meta_cache[f"{type_.value}-{id_}"]
def get_meta(self) -> dict:
"""
获取元数据
Returns:
调用 API 返回的结果
"""
return self.meta # type: ignore
async def get_videos(
self, sort: ChannelOrder = ChannelOrder.DEFAULT, pn: int = 1, ps: int = 100
) -> dict:
"""
获取合集视频
Args:
sort(ChannelOrder): 排序方式
pn(int) : 页数,默认为 1
ps(int) : 每一页显示的视频数量
Returns:
调用 API 返回的结果
"""
if self.is_new:
return await self.owner.get_channel_videos_season(self.id_, sort, pn, ps)
else:
return await self.owner.get_channel_videos_series(self.id_, sort, pn, ps)
async def create_channel_series(
name: str,
aids: List[int] = [],
keywords: List[str] = [],
description: str = "",
credential: Union[Credential, None] = None,
) -> dict:
"""
新建一个视频列表 (旧版合集)
Args:
name (str): 列表名称。
aids (List[int]): 要加入列表的视频的 aid 列表。
keywords (List[str]): 列表的关键词。
description (str): 列表的描述。
credential (Credential | None): 凭据类。
Returns:
dict: 调用 API 返回的结果
"""
from .user import get_self_info
credential = credential if credential else Credential()
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
api = API_USER["channel_series"]["create"]
info = await get_self_info(credential)
data = {
"mid": info["mid"],
"aids": ",".join(map(lambda x: str(x), aids)),
"name": name,
"keywords": ",".join(keywords),
"description": description,
}
return await Api(**api, credential=credential).update_data(**data).result
async def del_channel_series(series_id: int, credential: Credential) -> dict:
"""
删除视频列表(旧版合集)
Args:
series_id (int) : 旧版合集 id。
credential (Credential): 凭据类。
Returns:
dict: 调用 API 返回的结果
"""
from .user import User, get_self_info
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
series_total = ChannelSeries(
type_=ChannelSeriesType.SERIES, id_=series_id, credential=credential
).get_meta()["total"]
self_uid = (await get_self_info(credential))["mid"]
aids = []
pages = series_total // 20 + (1 if (series_total % 20 != 0) else 0)
for page in range(1, pages + 1, 1):
page_info = await User(self_uid, credential).get_channel_videos_series(
series_id, pn=page, ps=20
)
for aid in page_info["aids"]:
aids.append(aid)
api = API_USER["channel_series"]["del_channel_series"]
data = {
"mid": self_uid,
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
return await Api(**api, credential=credential).update_data(**data).result
async def add_aids_to_series(
series_id: int, aids: List[int], credential: Credential
) -> dict:
"""
添加视频至视频列表(旧版合集)
Args:
series_id (int) : 旧版合集 id。
aids (List[int]) : 视频 aid 列表。
credential (Credential): 凭据类。
Returns:
dict: 调用 API 返回的结果
"""
from .user import get_self_info
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
self_info = await get_self_info(credential)
api = API_USER["channel_series"]["add_channel_aids_series"]
data = {
"mid": self_info["mid"],
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
return await Api(**api, credential=credential).update_data(**data).result
async def del_aids_from_series(
series_id: int, aids: List[int], credential: Credential
) -> dict:
"""
从视频列表(旧版合集)删除视频
Args:
series_id (int) : 旧版合集 id。
aids (List[int]) : 视频 aid 列表。
credential (Credential): 凭据类。
Returns:
dict: 调用 API 返回的结果
"""
from .user import get_self_info
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
self_info = await get_self_info(credential)
api = API_USER["channel_series"]["del_channel_aids_series"]
data = {
"mid": self_info["mid"],
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
return await Api(**api, credential=credential).update_data(**data).result
async def set_follow_channel_season(
season_id: int, status: bool = True, credential: Optional[Credential] = None
) -> dict:
"""
设置是否订阅合集(新版)
Args:
season_id (int) : 合集 id
status (bool): 是否订阅状态. Defaults to True.
"""
api = API["operate"]["fav"] if status else API["operate"]["unfav"]
data = {"season_id": season_id}
return await Api(**api, credential=credential).update_data(**data).result
|