From 6eb676513359ee7195326915e956bd377d5e8a47 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Sun, 21 Jan 2024 18:34:06 -0500 Subject: [PATCH 1/3] meta: specify py3.12 support Signed-off-by: Rui Chen --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 022faa62..8893dc46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ classifiers = [ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", ] dependencies = [ "aiofiles>=0.8.0", From c4c2e6d9e8c9383acdb4c1f3f72a7c4d3e251664 Mon Sep 17 00:00:00 2001 From: HFrost0 Date: Tue, 23 Jan 2024 21:09:15 +0800 Subject: [PATCH 2/3] fix: optional for pydantic validation --- bilix/sites/bilibili/api.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bilix/sites/bilibili/api.py b/bilix/sites/bilibili/api.py index 118dc475..21a8f3fd 100644 --- a/bilix/sites/bilibili/api.py +++ b/bilix/sites/bilibili/api.py @@ -195,14 +195,14 @@ async def get_up_info(client: httpx.AsyncClient, url_or_mid: str, pn=1, ps=30, o class Media(BaseModel): base_url: str - backup_url: List[str] = None - size: int = None - width: int = None - height: int = None - suffix: str = None - quality: str = None - codec: str = None - segment_base: dict = None + backup_url: Optional[List[str]] = None + size: Optional[int] = None + width: Optional[int] = None + height: Optional[int] = None + suffix: Optional[str] = None + quality: Optional[str] = None + codec: Optional[str] = None + segment_base: Optional[dict] = None @property def urls(self): @@ -299,7 +299,7 @@ class Status(BaseModel): reply: int = Field(description="回复数") favorite: int = Field(description="收藏数") share: int = Field(description="分享数") - follow: int = Field(default=None, description="追剧数/追番数") + follow: Optional[int] = Field(default=None, description="追剧数/追番数") @field_validator('view', mode="before") @classmethod @@ -321,7 +321,7 @@ class VideoInfo(BaseModel): pages: List[Page] # [[p_name, p_url], ...] img_url: str status: Status - bvid: str = None + bvid: Optional[str] = None dash: Optional[Dash] = None other: Optional[List[Media]] = None # durl resource: flv, mp4. From 282ad45c5ea7049d4ce9935ae1ff81e491fe591c Mon Sep 17 00:00:00 2001 From: HFrost0 Date: Sat, 27 Jan 2024 12:12:35 +0800 Subject: [PATCH 3/3] fix(api): bilibili list --- bilix/sites/bilibili/api.py | 28 +++++++++++++++++++++++----- bilix/sites/bilibili/api_test.py | 4 ++-- bilix/sites/bilibili/downloader.py | 4 ++-- bilix/sites/bilibili/informer.py | 2 +- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/bilix/sites/bilibili/api.py b/bilix/sites/bilibili/api.py index 21a8f3fd..e60dc929 100644 --- a/bilix/sites/bilibili/api.py +++ b/bilix/sites/bilibili/api.py @@ -58,11 +58,13 @@ async def get_list_info(client: httpx.AsyncClient, url_or_sid: str, ): meta = json.loads(res.text) mid = meta['data']['meta']['mid'] params = {'mid': mid, 'series_id': sid, 'ps': meta['data']['meta']['total']} - list_res, up_res = await asyncio.gather( + list_res, up_info = await asyncio.gather( req_retry(client, 'https://api.bilibili.com/x/series/archives', params=params), - req_retry(client, f'https://api.bilibili.com/x/space/acc/info?mid={mid}')) - list_info, up_info = json.loads(list_res.text), json.loads(up_res.text) - list_name, up_name = meta['data']['meta']['name'], up_info['data']['name'] + get_up_info(client, str(mid)), + ) + list_info = json.loads(list_res.text) + list_name = meta['data']['meta']['name'] + up_name = up_info.get('name', '') bvids = [i['bvid'] for i in list_info['data']['archives']] return list_name, up_name, bvids @@ -164,8 +166,12 @@ async def _add_sign(client: httpx.AsyncClient, params: dict): return params +def _find_mid(space_url: str): + return re.search(r'^https://space.bilibili.com/(\d+)/?', space_url).group(1) + + @raise_api_error -async def get_up_info(client: httpx.AsyncClient, url_or_mid: str, pn=1, ps=30, order="pubdate", keyword=""): +async def get_up_video_info(client: httpx.AsyncClient, url_or_mid: str, pn=1, ps=30, order="pubdate", keyword=""): """ 获取up主信息 @@ -193,6 +199,18 @@ async def get_up_info(client: httpx.AsyncClient, url_or_mid: str, pn=1, ps=30, o return up_name, total_size, bv_ids +async def get_up_info(client: httpx.AsyncClient, url_or_mid: str): + if url_or_mid.startswith("http"): + mid = _find_mid(url_or_mid) + else: + mid = url_or_mid + params = {"mid": mid} + await _add_sign(client, params) + res = await req_retry(client, "https://api.bilibili.com/x/space/wbi/acc/info", params=params) + data = json.loads(res.text)['data'] + return data + + class Media(BaseModel): base_url: str backup_url: Optional[List[str]] = None diff --git a/bilix/sites/bilibili/api_test.py b/bilix/sites/bilibili/api_test.py index d438a235..9634473a 100644 --- a/bilix/sites/bilibili/api_test.py +++ b/bilix/sites/bilibili/api_test.py @@ -61,8 +61,8 @@ async def test_get_cate_page_info(): @pytest.mark.asyncio -async def test_get_up_info(): - up_name, total_size, bvids = await api.get_up_info(client, "316568752", keyword="什么") +async def test_get_up_video_info(): + up_name, total_size, bvids = await api.get_up_video_info(client, "316568752", keyword="什么") assert len(bvids) > 0 and bvids[0].startswith('BV') diff --git a/bilix/sites/bilibili/downloader.py b/bilix/sites/bilibili/downloader.py index af00985d..8e266ddb 100644 --- a/bilix/sites/bilibili/downloader.py +++ b/bilix/sites/bilibili/downloader.py @@ -248,7 +248,7 @@ async def get_up( :return: """ ps = 30 - up_name, total_size, bv_ids = await api.get_up_info(self.client, url_or_mid, 1, ps, order, keyword) + up_name, total_size, bv_ids = await api.get_up_video_info(self.client, url_or_mid, 1, ps, order, keyword) if self.hierarchy: path /= legal_title(f"【up】{up_name}") path.mkdir(parents=True, exist_ok=True) @@ -269,7 +269,7 @@ async def _get_up_by_page(self, url_or_mid, path: Path, pn=1, num=30, order='pub series=True, image=False, subtitle=False, dm=False, only_audio=False, codec='', ): ps = 30 num = min(ps, num) - _, _, bvids = await api.get_up_info(self.client, url_or_mid, pn, ps, order, keyword) + _, _, bvids = await api.get_up_video_info(self.client, url_or_mid, pn, ps, order, keyword) bvids = bvids[:num] func = self.get_series if series else self.get_video # noinspection PyArgumentList diff --git a/bilix/sites/bilibili/informer.py b/bilix/sites/bilibili/informer.py index f725f8c2..19107d5f 100644 --- a/bilix/sites/bilibili/informer.py +++ b/bilix/sites/bilibili/informer.py @@ -23,7 +23,7 @@ async def info_key(self, key): await self.parse_url(key)(self, key) async def info_up(self, url: str): - up_name, total_size, bvids = await api.get_up_info(self.client, url) + up_name, total_size, bvids = await api.get_up_video_info(self.client, url) rprint(up_name) async def info_favour(self, url: str):