Skip to content

Commit

Permalink
Merge pull request #436 from Nemo2011/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
z0z0r4 authored Aug 16, 2023
2 parents 8984a9f + 123019c commit dd606c4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOGS/v15.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 15.5.4 2023/8/16
- fix: [BREADKING CHANGE] 更新 user.get_dynamics 接口 https://github.com/Nemo2011/bilibili-api/pull/432
- fix: 无法实时获取弹幕 by @Drelf2018 in https://github.com/Nemo2011/bilibili-api/pull/430 and @whille in https://github.com/Nemo2011/bilibili-api/pull/429
- fix: 修复 bangumi.Episode 初始化信息问题 by @z0z0r4 https://github.com/Nemo2011/bilibili-api/issues/433

# 15.5.3 2023/7/25
- fix: 强行修复 Api 的相关 bug https://github.com/Nemo2011/bilibili-api/issues/405

Expand Down
2 changes: 1 addition & 1 deletion bilibili_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
interactive_video,
)

BILIBILI_API_VERSION = "15.5.3"
BILIBILI_API_VERSION = "15.5.4"

# 如果系统为 Windows,则修改默认策略,以解决代理报错问题
if "windows" in platform.system().lower():
Expand Down
23 changes: 15 additions & 8 deletions bilibili_api/bangumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .exceptions.ApiException import ApiException
from .utils.network_httpx import request, Api, get_session
from .exceptions.ResponseException import ResponseException
from .utils.initial_state import get_initial_state, get_initial_state_sync
from .utils.initial_state import get_initial_state, get_next_data

API = get_api("bangumi")

Expand Down Expand Up @@ -1008,8 +1008,10 @@ def __init__(
url=api["url"], params=params, cookies=self.credential.get_cookies()
)
meta.raise_for_status()
# print(meta.json())
self.__ssid = meta.json()["result"]["media"]["season_id"]
if meta.json()["code"] == 0:
self.__ssid = meta.json()["result"]["media"]["season_id"]
else:
raise ApiException(msg=meta.json()["message"])
params["media_id"] = media_id
# 处理正常情况
if self.__ssid != -1:
Expand Down Expand Up @@ -1292,16 +1294,21 @@ def __init__(self, epid: int, credential: Union[Credential, None] = None):
self.__epid = epid

if not epid in episode_data_cache.keys():
content = get_initial_state_sync(
content = get_next_data(
url=f"https://www.bilibili.com/bangumi/play/ep{self.__epid}",
credential=self.credential,
)
credential=self.credential
)["props"]["pageProps"]["dehydratedState"]["queries"][0]["state"]["data"]["mediaInfo"] # 变成 __NEXT_DATA__ 了,又臭又长
else:
content = episode_data_cache[epid]["bangumi_meta"]

bvid = content["epInfo"]["bvid"]
for ep_info in content["episodes"]:
if ep_info["ep_id"] == epid:
bvid = ep_info["bvid"]
break
# else:
# raise ValueError("未找到对应的 bvid")
if not epid in episode_data_cache.keys():
self.bangumi = Bangumi(ssid=content["mediaInfo"]["season_id"])
self.bangumi = Bangumi(ssid=content["season_id"])
else:
self.bangumi = episode_data_cache[epid]["bangumi_class"]

Expand Down
7 changes: 4 additions & 3 deletions bilibili_api/data/api/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@
"comment": "专栏文集"
},
"dynamic": {
"url": "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history",
"url": "https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space",
"method": "GET",
"verify": false,
"params": {
"host_uid": "int: uid",
"offset_dynamic_id": "int: 动态偏移用,第一页为 0",
"need_top": "int bool: 是否显示置顶动态"
"offset": "int: 动态偏移用,第一页为 0",
"timezone_offset": "int: -400",
"features": "str: itemOpusStyle"
},
"comment": "用户动态信息"
},
Expand Down
9 changes: 4 additions & 5 deletions bilibili_api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ async def get_article_list(
params = {"mid": self.__uid, "sort": order.value}
return await Api(**api, credential=self.credential).update_params(**params).result

async def get_dynamics(self, offset: int = 0, need_top: bool = False) -> dict:
async def get_dynamics(self, offset: int = 0) -> dict:
"""
获取用户动态。
Expand All @@ -486,16 +486,15 @@ async def get_dynamics(self, offset: int = 0, need_top: bool = False) -> dict:
0 为从头开始。
Defaults to 0.
need_top (bool, optional): 显示置顶动态. Defaults to False.
Returns:
dict: 调用接口返回的内容。
"""
api = API["info"]["dynamic"]
params = {
"host_uid": self.__uid,
"offset_dynamic_id": offset,
"need_top": 1 if need_top else 0,
"offset": offset,
"features": "itemOpusStyle",
"timezone_offset": -480
}
data = await Api(**api, credential=self.credential).update_params(**params).result
# card 字段自动转换成 JSON。
Expand Down
31 changes: 31 additions & 0 deletions bilibili_api/utils/initial_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,35 @@ def get_initial_state_sync(url: str, credential: Credential = Credential()) -> d
except json.JSONDecodeError:
raise ApiException("信息解析错误")

return content

def get_next_data(url: str, credential: Credential = Credential()) -> dict:
'''
同步获取剧集的数据
Args:
url (str): 链接
credential (Credential, optional): 用户凭证. Defaults to Credential().
'''
try:
resp = httpx.get(
url,
cookies=credential.get_cookies(),
headers={"User-Agent": "Mozilla/5.0"},
follow_redirects=True,
)
except Exception as e:
raise e
else:
content = resp.text
pattern = re.compile(pattern = r'<script id="__NEXT_DATA__" type="application/json">\s*(.*?)\s*</script>')
match = re.search(pattern, content)
if match is None:
raise ApiException("未找到相关信息")
try:
content = json.loads(match.group(1))
except json.JSONDecodeError:
raise ApiException("信息解析错误")

return content
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="bilibili-api-python",
version="15.5.3",
version="15.5.4",
license="GPLv3+",
author="Nemo2011",
author_email="[email protected]",
Expand Down

0 comments on commit dd606c4

Please sign in to comment.