Skip to content

Commit

Permalink
feat: Article <-> Dynamic & style: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Dec 17, 2024
1 parent c1cc94e commit f1442aa
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
29 changes: 25 additions & 4 deletions bilibili_api/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from .exceptions.NetworkException import ApiException, NetworkException
from .video import get_cid_info_sync

from . import dynamic

API = get_api("article")

# 文章颜色表
Expand Down Expand Up @@ -159,6 +161,21 @@ def __init__(self, cvid: int, credential: Union[Credential, None] = None):
self.__meta = None
self.__cvid = cvid
self.__has_parsed: bool = False
self.__get_all_data: dict = None

async def turn_to_dynamic(self) -> "dynamic.Dynamic":
"""
将专栏转为对应动态(评论、点赞等数据专栏/动态/图文共享)
转换后可查看“赞和转发”列表。
Returns:
Dynamic: 动态实例
"""
return dynamic.Dynamic(
dynamic_id=(await self.get_all())["readInfo"]["dyn_id_str"],
credential=self.credential,
)

def get_cvid(self) -> int:
"""
Expand Down Expand Up @@ -485,7 +502,7 @@ async def parse(el: BeautifulSoup):
node_list.append(node)

elif e.name == "div":
node_list += (await parse(e))
node_list += await parse(e)

return node_list

Expand Down Expand Up @@ -517,9 +534,13 @@ async def get_all(self) -> dict:
Returns:
dict: 调用 API 返回的结果
"""
return (
await get_initial_state(f"https://www.bilibili.com/read/cv{self.__cvid}/?jump_opus=1")
)[0]
if not self.__get_all_data:
self.__get_all_data = (
await get_initial_state(
f"https://www.bilibili.com/read/cv{self.__cvid}/?jump_opus=1"
)
)[0]
return self.__get_all_data

async def set_like(self, status: bool = True) -> dict:
"""
Expand Down
1 change: 1 addition & 0 deletions bilibili_api/channel_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(

async def __fetch_meta(self) -> None:
from .user import User

if self.is_new:
api = API_USER["channel_series"]["season_info"]
params = {"season_id": self.id_}
Expand Down
6 changes: 2 additions & 4 deletions bilibili_api/cheese.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ def __init__(
self.__season_id = season_id
self.__ep_id = ep_id
self.credential: Credential = credential if credential else Credential()

async def __fetch_season_id(self) -> None:
# self.season_id = str(sync(self.get_meta())["season_id"])
api = API["info"]["meta"]
params = {"ep_id": self.__ep_id}
meta = await (
Api(**api, credential=self.credential)
.update_params(**params)
.result
Api(**api, credential=self.credential).update_params(**params).result
)
self.__season_id = int(meta["season_id"])

Expand Down
66 changes: 43 additions & 23 deletions bilibili_api/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .utils.credential import Credential
from .utils.network import Api
from .exceptions.DynamicExceedImagesException import DynamicExceedImagesException
from .article import Article

API = utils.get_api("dynamic")
API_opus = utils.get_api("opus")
Expand Down Expand Up @@ -776,6 +777,7 @@ def __init__(
credential (Credential | None, optional): 凭据类. Defaults to None.
"""
self.__dynamic_id = dynamic_id
self.__detail = None
self.credential: Credential = (
credential if credential is not None else Credential()
)
Expand All @@ -789,37 +791,55 @@ def get_dynamic_id(self) -> None:
"""
return self.__dynamic_id

async def get_info(
self,
features: str = "itemOpusStyle,opusBigCover,onlyfansVote,endFooterHidden,decorationCard,onlyfansAssetsV2,ugcDelete",
) -> dict:
async def get_info(self) -> dict:
"""
(对 Opus 动态,获取动态内容建议使用 Opus.get_detail())
获取动态信息
Args:
features (str, optional): 默认 itemOpusStyle,opusBigCover,onlyfansVote,endFooterHidden,decorationCard,onlyfansAssetsV2,ugcDelete.
Returns:
dict: 调用 API 返回的结果
"""
if not self.__detail:
api = API["info"]["detail"]
params = {
"id": self.__dynamic_id,
"timezone_offset": -480,
"platform": "web",
"gaia_source": "main_web",
"features": "itemOpusStyle,opusBigCover,onlyfansVote,endFooterHidden,decorationCard,onlyfansAssetsV2,ugcDelete",
"web_location": "333.1368",
"x-bili-device-req-json": '{"platform":"web","device":"pc"}',
"x-bili-web-req-json": '{"spm_id":"333.1368"}',
}
self.__detail = (
await Api(**api, credential=self.credential)
.update_params(**params)
.result
)
return self.__detail

async def is_article(self) -> bool:
"""
判断动态是否为专栏发布动态(评论、点赞等数据专栏/动态/图文共享)
api = API["info"]["detail"]
params = {
"id": self.__dynamic_id,
"timezone_offset": -480,
"platform": "web",
"gaia_source": "main_web",
"features": features,
"web_location": "333.1368",
"x-bili-device-req-json": '{"platform":"web","device":"pc"}',
"x-bili-web-req-json": '{"spm_id":"333.1368"}',
}
data = (
await Api(**api, credential=self.credential).update_params(**params).result
Returns:
bool: 是否为专栏
"""
return (await self.get_info())["item"]["basic"]["comment_type"] == 12

async def turn_to_article(self) -> "Article":
"""
将专栏发布动态转为对应专栏(评论、点赞等数据专栏/动态/图文共享)
转换后可投币。
Returns:
Article: 专栏实例
"""
raise_for_statement(await self.is_article(), "此动态无对应专栏。")
return Article(
cvid=(await self.get_info())["item"]["basic"]["rid_str"],
credential=self.credential,
)
return data

async def get_reaction(self, offset: str = "") -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion bilibili_api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class OrderType(Enum):
asc = "asc"


async def name2uid_sync(names: Union[str, List[str]]):
def name2uid_sync(names: Union[str, List[str]]):
"""
将用户名转为 uid
Expand Down

0 comments on commit f1442aa

Please sign in to comment.