From 282ad45c5ea7049d4ce9935ae1ff81e491fe591c Mon Sep 17 00:00:00 2001 From: HFrost0 Date: Sat, 27 Jan 2024 12:12:35 +0800 Subject: [PATCH] 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):