bpi_rs/note/
info.rs

1use crate::{BilibiliRequest, BpiClient, BpiError, BpiResponse};
2use serde::{Deserialize, Serialize};
3
4// --- 查询该稿件是否禁止笔记 ---
5
6/// 稿件是否禁止笔记的响应数据
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct NoteIsForbidData {
9    /// 是否禁止笔记
10    pub forbid_note_entrance: bool,
11}
12
13// --- 查询私有笔记内容 ---
14
15/// 私有笔记的视频稿件信息
16#[derive(Debug, Clone, Deserialize, Serialize)]
17pub struct PrivateNoteArc {
18    pub oid: u64,
19    pub oid_type: u8,
20    pub title: String,
21    pub pic: String,
22    pub status: u32,
23    pub desc: String,
24}
25
26/// 私有笔记的标签
27#[derive(Debug, Clone, Deserialize, Serialize)]
28pub struct PrivateNoteTag {
29    pub cid: u64,
30    pub status: u8,
31    pub index: u32,
32    pub seconds: u32,
33    pub pos: u32,
34}
35
36/// 私有笔记的响应数据
37#[derive(Debug, Clone, Deserialize, Serialize)]
38pub struct PrivateNoteInfoData {
39    pub arc: PrivateNoteArc,
40    pub audit_status: u8,
41    pub cid_count: u32,
42    pub content: String,
43    pub forbid_note_entrance: bool,
44    pub pub_reason: Option<String>,
45    pub pub_status: u8,
46    pub pub_version: u32,
47    pub summary: String,
48    pub tags: Vec<PrivateNoteTag>,
49    pub title: String,
50}
51
52// --- 查询公开笔记内容 ---
53
54/// 公开笔记的稿件信息
55#[derive(Debug, Clone, Deserialize, Serialize)]
56pub struct PublicNoteArc {
57    pub oid: u64,
58    pub oid_type: u8,
59    pub title: String,
60    pub status: u32,
61    pub pic: String,
62    pub desc: String,
63}
64
65/// 公开笔记的作者信息
66#[derive(Debug, Clone, Deserialize, Serialize)]
67pub struct PublicNoteAuthor {
68    pub mid: u64,
69    pub name: String,
70    pub face: String,
71    pub level: u8,
72    pub vip_info: serde_json::Value,
73    pub pendant: serde_json::Value,
74}
75
76/// 公开笔记的响应数据
77#[derive(Debug, Clone, Deserialize, Serialize)]
78pub struct PublicNoteInfoData {
79    pub cvid: u64,
80    pub note_id: u64,
81    pub title: String,
82    pub summary: String,
83    pub content: String,
84    pub cid_count: u32,
85    pub pub_status: u8,
86    pub tags: Vec<PrivateNoteTag>,
87    pub arc: PublicNoteArc,
88    pub author: PublicNoteAuthor,
89    pub forbid_note_entrance: bool,
90}
91
92impl BpiClient {
93    /// 查询该稿件是否禁止笔记
94    ///
95    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note
96    ///
97    /// - aid: 稿件 avid
98    pub async fn note_is_forbid(
99        &self,
100        aid: u64,
101    ) -> Result<BpiResponse<NoteIsForbidData>, BpiError> {
102        self.get("https://api.bilibili.com/x/note/is_forbid")
103            .query(&[("aid", aid)])
104            .send_bpi("查询稿件是否禁止笔记")
105            .await
106    }
107
108    /// 查询私有笔记内容
109    ///
110    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note
111    ///
112    /// 参数
113    ///
114    /// | 名称 | 类型 | 说明 |
115    /// | ---- | ---- | ---- |
116    /// | `oid` | u64 | 稿件 avid |
117    /// | `note_id` | u64 | 笔记 ID |
118    pub async fn note_get_private_info(
119        &self,
120        oid: u64,
121        note_id: u64,
122    ) -> Result<BpiResponse<PrivateNoteInfoData>, BpiError> {
123        self.get("https://api.bilibili.com/x/note/info")
124            .query(&[("oid", oid), ("oid_type", 0), ("note_id", note_id)])
125            .send_bpi("查询私有笔记内容")
126            .await
127    }
128
129    /// 查询公开笔记内容
130    ///
131    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note
132    ///
133    /// - cvid: 公开笔记 cvid
134    pub async fn note_get_public_info(
135        &self,
136        cvid: u64,
137    ) -> Result<BpiResponse<PublicNoteInfoData>, BpiError> {
138        self.get("https://api.bilibili.com/x/note/publish/info")
139            .query(&[("cvid", cvid)])
140            .send_bpi("查询公开笔记内容")
141            .await
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148    use tracing::info;
149
150    #[tokio::test]
151    async fn test_note_is_forbid() {
152        let bpi = BpiClient::new();
153        // 替换为一个有效的avid
154        let aid = 338677252;
155        let resp = bpi.note_is_forbid(aid).await;
156
157        info!("{:?}", resp);
158        assert!(resp.is_ok());
159
160        let resp_data = resp.unwrap();
161        info!("code: {}", resp_data.code);
162        if let Some(data) = resp_data.data {
163            info!("forbid_note_entrance: {}", data.forbid_note_entrance);
164        }
165    }
166
167    #[tokio::test]
168    async fn test_note_get_private_info() {
169        let bpi = BpiClient::new();
170        let oid = 676931260;
171        let note_id = 83577722856540160;
172        let resp = bpi.note_get_private_info(oid, note_id).await;
173
174        info!("{:?}", resp);
175        assert!(resp.is_ok());
176
177        let resp_data = resp.unwrap();
178        info!("code: {}", resp_data.code);
179        if let Some(data) = resp_data.data {
180            info!("note title: {}", data.title);
181            info!("note content: {}", data.content);
182        }
183    }
184
185    #[tokio::test]
186    async fn test_note_get_public_info() {
187        let bpi = BpiClient::new();
188        let cvid = 15160286;
189        let resp = bpi.note_get_public_info(cvid).await;
190
191        info!("{:?}", resp);
192        assert!(resp.is_ok());
193
194        let resp_data = resp.unwrap();
195        info!("code: {}", resp_data.code);
196        if let Some(data) = resp_data.data {
197            info!("note title: {}", data.title);
198            info!("note content: {}", data.content);
199            info!("author name: {}", data.author.name);
200        }
201    }
202}