diff --git a/src/bilifm/command.py b/src/bilifm/command.py index 5b08f7d..c6f3159 100644 --- a/src/bilifm/command.py +++ b/src/bilifm/command.py @@ -79,8 +79,7 @@ def season( sea = Season(uid, sid) audio_generator = sea.get_videos() if not audio_generator: - typer.Exit(1) - return + raise typer.Exit(1) if not os.path.isdir(sea.name): os.makedirs(sea.name) @@ -108,8 +107,7 @@ def series( ser = Series(uid, sid) audio_generator = ser.get_videos() if not audio_generator: - typer.Exit(1) - return + raise typer.Exit(1) for audios in audio_generator: for id in audios: diff --git a/src/bilifm/user.py b/src/bilifm/user.py index 887c66f..4d5e071 100644 --- a/src/bilifm/user.py +++ b/src/bilifm/user.py @@ -1,14 +1,27 @@ import typer -from .util import request +from .util import get_w_webid, request class User: uidUrl: str = "https://api.bilibili.com/x/space/wbi/arc/search" + uid: str = "" videos: list = [] def __init__(self, uid: str): - params = {"mid": uid, "ps": 1, "tid": 0, "pn": 1, "order": "pubdate"} + self.uid = uid + + w_webid = get_w_webid(uid) + + params = { + "mid": uid, + "ps": 1, + "tid": 0, + "pn": 1, + "order": "pubdate", + "web_location": "333.999", + "w_webid": w_webid, + } response = request( method="get", url=self.uidUrl, params=params, wbi=True, dm=True @@ -18,7 +31,7 @@ def __init__(self, uid: str): if code != 0: typer.echo(f"Error: uid {uid} not found") - return + raise typer.Exit(1) total = response.json()["data"]["page"]["count"] @@ -29,7 +42,15 @@ def __init__(self, uid: str): for i in range(1, max_pn + 2): ps = 50 if i != max_pn + 1 else surpus - params = {"mid": uid, "ps": ps, "tid": 0, "pn": i, "order": "pubdate"} + params = { + "mid": uid, + "ps": ps, + "tid": 0, + "pn": i, + "order": "pubdate", + "web_location": "333.999", + "w_webid": w_webid, + } response = request( method="get", url=self.uidUrl, params=params, wbi=True, dm=True diff --git a/src/bilifm/util.py b/src/bilifm/util.py index 59c8b40..4cb0576 100644 --- a/src/bilifm/util.py +++ b/src/bilifm/util.py @@ -1,5 +1,7 @@ +import json import os import random +import re import time import urllib.parse from enum import Enum @@ -176,6 +178,44 @@ def gen_dm_args(params: dict): return params +def get_w_webid(uid: str): + """reference: https://github.com/SocialSisterYi/bilibili-API-collect/issues/1107#issuecomment-2424269364""" + + dynamic_url = f"https://space.bilibili.com/{uid}/dynamic" + + # TODO: 简化所需的 headers 设置 + headers = { + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", + "accept-language": "zh-CN,zh;q=0.9", + "cache-control": "no-cache", + "dnt": "1", + "pragma": "no-cache", + "priority": "u=0, i", + "sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"macOS"', + "sec-fetch-dest": "document", + "sec-fetch-mode": "navigate", + "sec-fetch-site": "none", + "sec-fetch-user": "?1", + "upgrade-insecure-requests": "1", + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36", + } + + text = requests.get(dynamic_url, headers=headers).text + + # + __RENDER_DATA__ = re.search( + r"", + text, + re.S, + ).group(1) + + access_id = json.loads(urllib.parse.unquote(__RENDER_DATA__))["access_id"] + + return access_id + + def request( method: str, url: str,