Skip to content

Commit

Permalink
fix(api): bilibili list
Browse files Browse the repository at this point in the history
  • Loading branch information
HFrost0 committed Jan 27, 2024
1 parent c4c2e6d commit 282ad45
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
28 changes: 23 additions & 5 deletions bilix/sites/bilibili/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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主信息
Expand Down Expand 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
Expand Down
4 changes: 2 additions & 2 deletions bilix/sites/bilibili/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
4 changes: 2 additions & 2 deletions bilix/sites/bilibili/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bilix/sites/bilibili/informer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 282ad45

Please sign in to comment.