Skip to main content

bpi_rs/note/
list.rs

1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
2use serde::{ Deserialize, Serialize };
3
4/// 稿件私有笔记列表数据
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct NoteListArchiveData {
7    /// 笔记ID列表
8    #[serde(rename = "noteIds")]
9    pub note_ids: Option<Vec<String>>,
10}
11
12// --- 查询用户私有笔记 ---
13
14/// 用户私有笔记的视频信息
15#[derive(Debug, Clone, Deserialize, Serialize)]
16pub struct PrivateNoteArc {
17    pub oid: u64,
18    pub status: u8,
19    pub oid_type: u8,
20    pub aid: u64,
21
22    // 老笔记没有以下内容
23    pub bvid: Option<String>,
24    pub pic: Option<String>,
25    pub desc: Option<String>,
26}
27
28/// 用户私有笔记列表项
29#[derive(Debug, Clone, Deserialize, Serialize)]
30pub struct PrivateNoteItem {
31    pub title: String,
32    pub summary: String,
33    pub mtime: String,
34    pub arc: PrivateNoteArc,
35    pub note_id: u64,
36    pub audit_status: u8,
37    pub web_url: String,
38    pub note_id_str: String,
39    pub message: String,
40    pub forbid_note_entrance: Option<bool>,
41    pub likes: u64,
42    pub has_like: bool,
43}
44
45/// 用户私有笔记列表数据
46#[derive(Debug, Clone, Deserialize, Serialize)]
47pub struct PrivateNoteListData {
48    pub list: Option<Vec<PrivateNoteItem>>,
49    pub page: Option<NotePage>,
50}
51
52// --- 查询稿件公开笔记 ---
53
54/// 稿件公开笔记列表项的作者信息
55#[derive(Debug, Clone, Deserialize, Serialize)]
56pub struct PublicNoteAuthor {
57    pub mid: u64,
58    pub name: String,
59    pub face: String,
60    pub level: u8,
61    pub vip_info: serde_json::Value,
62    pub pendant: serde_json::Value,
63}
64
65/// 稿件公开笔记列表项
66#[derive(Debug, Clone, Deserialize, Serialize)]
67pub struct PublicNoteItem {
68    pub cvid: u64,
69    pub title: String,
70    pub summary: String,
71    pub pubtime: String,
72    pub web_url: String,
73    pub message: String,
74    pub author: PublicNoteAuthor,
75    pub likes: u64,
76    pub has_like: bool,
77}
78
79/// 稿件公开笔记分页信息
80#[derive(Debug, Clone, Deserialize, Serialize)]
81pub struct NotePage {
82    pub total: u32,
83    pub size: u32,
84    pub num: u32,
85}
86
87/// 稿件公开笔记列表数据
88#[derive(Debug, Clone, Deserialize, Serialize)]
89pub struct PublicNoteListArchiveData {
90    pub list: Option<Vec<PublicNoteItem>>,
91    pub page: Option<NotePage>,
92    pub show_public_note: bool,
93    pub message: String,
94}
95
96// --- 查询用户公开笔记 ---
97
98/// 用户公开笔记列表数据
99#[derive(Debug, Clone, Deserialize, Serialize)]
100pub struct PublicNoteListUserData {
101    pub list: Option<Vec<PublicNoteItem>>,
102    pub page: Option<NotePage>,
103}
104
105impl BpiClient {
106    /// 查询稿件私有笔记
107    ///
108    /// # 文档
109    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
110    ///
111    /// # 参数
112    /// - oid: 稿件 avid
113    pub async fn note_list_archive(
114        &self,
115        oid: u64
116    ) -> Result<BpiResponse<NoteListArchiveData>, BpiError> {
117        self
118            .get("https://api.bilibili.com/x/note/list/archive")
119            .query(
120                &[
121                    ("oid", oid),
122                    ("oid_type", 0),
123                ]
124            )
125            .send_bpi("查询稿件私有笔记").await
126    }
127
128    /// 查询用户私有笔记
129    ///
130    /// # 文档
131    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
132    ///
133    /// # 参数
134    ///
135    /// | 名称 | 类型 | 说明 |
136    /// | ---- | ---- | ---- |
137    /// | `pn` | u32 | 页码 |
138    /// | `ps` | u32 | 每页数量 |
139    pub async fn note_list_user_private(
140        &self,
141        pn: u32,
142        ps: u32
143    ) -> Result<BpiResponse<PrivateNoteListData>, BpiError> {
144        self
145            .get("https://api.bilibili.com/x/note/list")
146            .query(
147                &[
148                    ("pn", pn),
149                    ("ps", ps),
150                ]
151            )
152            .send_bpi("查询用户私有笔记").await
153    }
154
155    /// 查询稿件公开笔记
156    ///
157    /// # 文档
158    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
159    ///
160    /// # 参数
161    ///
162    /// | 名称 | 类型 | 说明 |
163    /// | ---- | ---- | ---- |
164    /// | `oid` | u64 | 稿件 avid |
165    /// | `pn` | u32 | 页码 |
166    /// | `ps` | u32 | 每页数量 |
167    pub async fn note_list_public_archive(
168        &self,
169        oid: u64,
170        pn: u32,
171        ps: u32
172    ) -> Result<BpiResponse<PublicNoteListArchiveData>, BpiError> {
173        self
174            .get("https://api.bilibili.com/x/note/publish/list/archive")
175            .query(
176                &[
177                    ("oid", oid.to_string()),
178                    ("oid_type", (0).to_string()),
179                    ("pn", pn.to_string()),
180                    ("ps", ps.to_string()),
181                ]
182            )
183            .send_bpi("查询稿件公开笔记").await
184    }
185
186    /// 查询用户公开笔记
187    ///
188    /// # 文档
189    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/note)
190    ///
191    /// # 参数
192    ///
193    /// | 名称 | 类型 | 说明 |
194    /// | ---- | ---- | ---- |
195    /// | `pn` | u32 | 页码 |
196    /// | `ps` | u32 | 每页数量 |
197    pub async fn note_list_public_user(
198        &self,
199        pn: u32,
200        ps: u32
201    ) -> Result<BpiResponse<PublicNoteListUserData>, BpiError> {
202        self
203            .get("https://api.bilibili.com/x/note/publish/list/user")
204            .query(
205                &[
206                    ("pn", pn),
207                    ("ps", ps),
208                ]
209            )
210            .send_bpi("查询用户公开笔记").await
211    }
212}
213
214#[cfg(test)]
215mod tests {
216    use super::*;
217    use tracing::info;
218
219    #[tokio::test]
220    async fn test_note_list_archive() {
221        let bpi = BpiClient::new();
222        // 替换为一个有效的avid
223        let oid = 676931260;
224        let resp = bpi.note_list_archive(oid).await;
225
226        info!("{:?}", resp);
227        assert!(resp.is_ok());
228
229        let resp_data = resp.unwrap();
230        info!("code: {}", resp_data.code);
231        if let Some(data) = resp_data.data {
232            info!("note ids: {:?}", data.note_ids);
233        }
234    }
235
236    #[tokio::test]
237    async fn test_note_list_user_private() {
238        let bpi = BpiClient::new();
239        let resp = bpi.note_list_user_private(1, 10).await;
240
241        info!("{:?}", resp);
242        assert!(resp.is_ok());
243
244        let resp_data = resp.unwrap();
245        resp_data.data
246            .as_ref()
247            .and_then(|data| data.list.as_ref())
248            .and_then(|list| {
249                info!("first note item: {:?}", list.first());
250                Some(())
251            });
252    }
253
254    #[tokio::test]
255    async fn test_note_list_public_archive() {
256        let bpi = BpiClient::new();
257        // 替换为一个有效的avid
258        let oid = 338677252;
259        let resp = bpi.note_list_public_archive(oid, 1, 10).await;
260
261        info!("{:?}", resp);
262        assert!(resp.is_ok());
263
264        let resp_data = resp.unwrap();
265        info!("code: {}", resp_data.code);
266        if let Some(data) = resp_data.data {
267            info!("show_public_note: {}", data.show_public_note);
268        }
269    }
270
271    #[tokio::test]
272    async fn test_note_list_public_user() {
273        let bpi = BpiClient::new();
274        let resp = bpi.note_list_public_user(1, 10).await;
275
276        info!("{:?}", resp);
277        assert!(resp.is_ok());
278
279        let resp_data = resp.unwrap();
280        info!("code: {}", resp_data.code);
281        if let Some(data) = resp_data.data {
282            info!("total public notes: {}", data.page.as_ref().unwrap().total);
283        }
284    }
285}