From 96a44485af42f07cb4ab7927f80f5053cfe93184 Mon Sep 17 00:00:00 2001 From: Nemo2011 Date: Tue, 30 Jan 2024 12:00:55 +0800 Subject: [PATCH] chore: article & note --- bilibili_api/article.py | 11 +++++------ bilibili_api/data/api/dynamic.json | 10 ++++++++++ bilibili_api/dynamic.py | 11 +++++++++++ bilibili_api/note.py | 11 +++++++++-- docs/modules/dynamic.md | 2 ++ docs/modules/note.md | 10 +++++++++- 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/bilibili_api/article.py b/bilibili_api/article.py index c491990a..c0e0f3cb 100644 --- a/bilibili_api/article.py +++ b/bilibili_api/article.py @@ -18,14 +18,14 @@ from yarl import URL from bs4 import BeautifulSoup, element -from bilibili_api.utils.initial_state import get_initial_state +from .utils.initial_state import get_initial_state -from .note import Note, NoteType from .utils.utils import get_api from .utils.credential import Credential from .utils.network import Api, get_session from .exceptions.NetworkException import ApiException, NetworkException from .video import get_cid_info_sync +from . import note API = get_api("article") @@ -204,10 +204,10 @@ def is_note(self) -> bool: """ return self.__type == ArticleType.NOTE - def turn_to_note(self) -> Note: + def turn_to_note(self) -> "note.Note": assert self.__type == ArticleType.NOTE - return Note( - cvid=self.__cvid, note_type=NoteType.PUBLIC, credential=self.credential + return note.Note( + cvid=self.__cvid, note_type=note.NoteType.PUBLIC, credential=self.credential ) def markdown(self) -> str: @@ -883,7 +883,6 @@ def __init__(self, text: str): def markdown(self): txt = self.text - txt = txt.lstrip() special_chars = ["\\", "*", "$", "<", ">", "|"] for c in special_chars: txt = txt.replace(c, "\\" + c) diff --git a/bilibili_api/data/api/dynamic.json b/bilibili_api/data/api/dynamic.json index d9c858b6..3ea59285 100644 --- a/bilibili_api/data/api/dynamic.json +++ b/bilibili_api/data/api/dynamic.json @@ -199,6 +199,16 @@ }, "comment": "动态详细信息" }, + "detail_new": { + "url": "https://api.bilibili.com/x/polymer/web-dynamic/v1/detail", + "method": "GET", + "verify": false, + "params": { + "timezone_offset": "int: 时区偏移量", + "id": "int: 动态 ID" + }, + "comment": "动态详细信息" + }, "dynamic_page_UPs_info": { "url": "https://api.bilibili.com/x/polymer/web-dynamic/v1/portal", "method": "GET", diff --git a/bilibili_api/dynamic.py b/bilibili_api/dynamic.py index 54411bbc..b3132d4a 100644 --- a/bilibili_api/dynamic.py +++ b/bilibili_api/dynamic.py @@ -742,6 +742,8 @@ def get_dynamic_id(self) -> int: async def get_info(self, features: str = "itemOpusStyle") -> dict: """ + (不建议使用此旧版 API,请转到新版 get_info_opus) + 获取动态信息 Args: @@ -762,6 +764,15 @@ async def get_info(self, features: str = "itemOpusStyle") -> dict: ) return data + async def get_info_opus(self) -> dict: + """ + 新版获取动态信息 + + Returns: + dict: 调用 API 返回的结果 + """ + pass + async def get_reposts(self, offset: str = "0") -> dict: """ 获取动态转发列表 diff --git a/bilibili_api/note.py b/bilibili_api/note.py index e87d50fd..e4542bc7 100644 --- a/bilibili_api/note.py +++ b/bilibili_api/note.py @@ -14,7 +14,7 @@ import httpx from yarl import URL -from bilibili_api.utils.initial_state import get_initial_state +from .utils.initial_state import get_initial_state from .utils.utils import get_api from .utils.picture import Picture @@ -22,6 +22,7 @@ from .exceptions import ApiException, ArgsException from .utils.network import Api, get_session from .video import get_cid_info_sync +from . import article API = get_api("note") API_ARTICLE = get_api("article") @@ -98,6 +99,10 @@ def get_aid(self) -> int: def get_note_id(self) -> int: return self.__note_id + def turn_to_article(self) -> "article.Article": + assert self.__type == NoteType.PUBLIC + return article.Article(cvid=self.get_cvid(), credential=self.credential) + async def get_info(self) -> dict: """ 获取笔记信息 @@ -277,7 +282,9 @@ def parse_note(data: List[dict]): if not isinstance(field["insert"], str): if "tag" in field["insert"].keys(): node = VideoCardNode() - node.aid = get_cid_info_sync(field["insert"]["tag"]["cid"])["cid"] + node.aid = get_cid_info_sync(field["insert"]["tag"]["cid"])[ + "cid" + ] self.__children.append(node) elif "imageUpload" in field["insert"].keys(): node = ImageNode() diff --git a/docs/modules/dynamic.md b/docs/modules/dynamic.md index 04249c6c..b360c0ba 100644 --- a/docs/modules/dynamic.md +++ b/docs/modules/dynamic.md @@ -191,6 +191,8 @@ from bilibili_api import dynamic | -------- | ------------- | ------------------- | | features | str, optional | 默认 itemOpusStyle. | +**(不建议使用此旧版 API,请转到新版 get_info_opus)** + 获取动态信息 **Returns:** dict: 调用 API 返回的结果 diff --git a/docs/modules/note.md b/docs/modules/note.md index adde7ea5..7d3c203f 100644 --- a/docs/modules/note.md +++ b/docs/modules/note.md @@ -13,7 +13,7 @@ from bilibili_api import note 笔记类型枚举 -## class Video +## class Note 笔记类,各种对笔记的操作都在里面 @@ -96,6 +96,14 @@ from bilibili_api import note --- +#### def turn_to_article() + +将笔记类转为专栏类。需要保证笔记是公开笔记。 + +**Returns:** Note: 专栏类 + +--- + #### async def get_info() 获取笔记信息。