Skip to main content

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    /// # 文档
96    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
97    ///
98    /// - aid: 稿件 avid
99    pub async fn note_is_forbid(
100        &self,
101        aid: u64
102    ) -> Result<BpiResponse<NoteIsForbidData>, BpiError> {
103        self
104            .get("https://api.bilibili.com/x/note/is_forbid")
105            .query(&[("aid", aid)])
106            .send_bpi("查询稿件是否禁止笔记").await
107    }
108
109    /// 查询私有笔记内容
110    ///
111    /// # 文档
112    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
113    ///
114    /// # 参数
115    ///
116    /// | 名称 | 类型 | 说明 |
117    /// | ---- | ---- | ---- |
118    /// | `oid` | u64 | 稿件 avid |
119    /// | `note_id` | u64 | 笔记 ID |
120    pub async fn note_get_private_info(
121        &self,
122        oid: u64,
123        note_id: u64
124    ) -> Result<BpiResponse<PrivateNoteInfoData>, BpiError> {
125        self
126            .get("https://api.bilibili.com/x/note/info")
127            .query(
128                &[
129                    ("oid", oid),
130                    ("oid_type", 0),
131                    ("note_id", note_id),
132                ]
133            )
134            .send_bpi("查询私有笔记内容").await
135    }
136
137    /// 查询公开笔记内容
138    ///
139    /// # 文档
140    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
141    ///
142    /// - cvid: 公开笔记 cvid
143    pub async fn note_get_public_info(
144        &self,
145        cvid: u64
146    ) -> Result<BpiResponse<PublicNoteInfoData>, BpiError> {
147        self
148            .get("https://api.bilibili.com/x/note/publish/info")
149            .query(&[("cvid", cvid)])
150            .send_bpi("查询公开笔记内容").await
151    }
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157    use tracing::info;
158
159    #[tokio::test]
160    async fn test_note_is_forbid() {
161        let bpi = BpiClient::new();
162        // 替换为一个有效的avid
163        let aid = 338677252;
164        let resp = bpi.note_is_forbid(aid).await;
165
166        info!("{:?}", resp);
167        assert!(resp.is_ok());
168
169        let resp_data = resp.unwrap();
170        info!("code: {}", resp_data.code);
171        if let Some(data) = resp_data.data {
172            info!("forbid_note_entrance: {}", data.forbid_note_entrance);
173        }
174    }
175
176    #[tokio::test]
177    async fn test_note_get_private_info() {
178        let bpi = BpiClient::new();
179        let oid = 676931260;
180        let note_id = 83577722856540160;
181        let resp = bpi.note_get_private_info(oid, note_id).await;
182
183        info!("{:?}", resp);
184        assert!(resp.is_ok());
185
186        let resp_data = resp.unwrap();
187        info!("code: {}", resp_data.code);
188        if let Some(data) = resp_data.data {
189            info!("note title: {}", data.title);
190            info!("note content: {}", data.content);
191        }
192    }
193
194    #[tokio::test]
195    async fn test_note_get_public_info() {
196        let bpi = BpiClient::new();
197        let cvid = 15160286;
198        let resp = bpi.note_get_public_info(cvid).await;
199
200        info!("{:?}", resp);
201        assert!(resp.is_ok());
202
203        let resp_data = resp.unwrap();
204        info!("code: {}", resp_data.code);
205        if let Some(data) = resp_data.data {
206            info!("note title: {}", data.title);
207            info!("note content: {}", data.content);
208            info!("author name: {}", data.author.name);
209        }
210    }
211}