bpi-rs 0.2.0

Bilibili API client library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 评论区相关操作 API
//
// [参考文档](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/comment/action)

use crate::BilibiliRequest;
use crate::BpiError;
use crate::comment::CommentClient;
use crate::response::BpiResult;
use serde::{Deserialize, Serialize};

const ADD_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/add";
const LIKE_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/action";
const DISLIKE_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/hate";
const DELETE_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/del";
const TOP_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/top";
const REPORT_ENDPOINT: &str = "https://api.bilibili.com/x/v2/reply/report";

/// 评论区类型枚举(部分示例,需按需求扩展)

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CommentType {
    Video = 1,    // 视频
    Article = 12, // 专栏
    Dynamic = 17, // 动态
    Unknown = 0,
}

/// 举报原因枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ReportReason {
    Other = 0,
    Ad = 1,
    Porn = 2,
    Spam = 3,
    Flame = 4,
    Spoiler = 5,
    Politics = 6,
    Abuse = 7,
    Irrelevant = 8,
    Illegal = 9,
    Vulgar = 10,
    Phishing = 11,
    Scam = 12,
    Rumor = 13,
    Incitement = 14,
    Privacy = 15,
    FloorSnatching = 16,
    HarmfulToYouth = 17,
}

/// 评论成功返回数据
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CommentData {
    pub rpid: u64,
    pub rpid_str: String,
    pub root: u64,
    pub root_str: String,
    pub parent: u64,
    pub parent_str: String,
    pub dialog: u64,
    pub dialog_str: String,
    pub success_toast: Option<String>,
}

/// Parameters for publishing a comment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommentAddParams {
    r#type: CommentType,
    oid: u64,
    message: String,
    root: Option<u64>,
    parent: Option<u64>,
    plat: u8,
}

impl CommentAddParams {
    pub fn new(r#type: CommentType, oid: u64, message: impl Into<String>) -> BpiResult<Self> {
        validate_comment_type(r#type)?;

        Ok(Self {
            r#type,
            oid: validate_nonzero_u64("oid", oid)?,
            message: normalize_non_blank("message", message.into())?,
            root: None,
            parent: None,
            plat: 1,
        })
    }

    pub fn root(mut self, root: u64) -> BpiResult<Self> {
        self.root = Some(validate_nonzero_u64("root", root)?);
        Ok(self)
    }

    pub fn parent(mut self, parent: u64) -> BpiResult<Self> {
        self.parent = Some(validate_nonzero_u64("parent", parent)?);
        Ok(self)
    }

    pub(crate) fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
        let mut pairs = vec![
            ("type", comment_type_value(self.r#type).to_string()),
            ("oid", self.oid.to_string()),
            ("message", self.message.clone()),
            ("plat", self.plat.to_string()),
            ("csrf", csrf.to_string()),
        ];

        if let Some(root) = self.root {
            pairs.push(("root", root.to_string()));
        }
        if let Some(parent) = self.parent {
            pairs.push(("parent", parent.to_string()));
        }

        pairs
    }
}

/// Parameters for binary comment actions such as like, dislike, and top.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommentActionParams {
    r#type: CommentType,
    oid: u64,
    rpid: u64,
    action: u8,
}

impl CommentActionParams {
    pub fn new(r#type: CommentType, oid: u64, rpid: u64, action: u8) -> BpiResult<Self> {
        validate_comment_type(r#type)?;

        Ok(Self {
            r#type,
            oid: validate_nonzero_u64("oid", oid)?,
            rpid: validate_nonzero_u64("rpid", rpid)?,
            action: validate_binary_action(action)?,
        })
    }

    pub(crate) fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
        vec![
            ("type", comment_type_value(self.r#type).to_string()),
            ("oid", self.oid.to_string()),
            ("rpid", self.rpid.to_string()),
            ("action", self.action.to_string()),
            ("csrf", csrf.to_string()),
        ]
    }
}

/// Parameters for deleting a comment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommentDeleteParams {
    r#type: CommentType,
    oid: u64,
    rpid: u64,
}

impl CommentDeleteParams {
    pub fn new(r#type: CommentType, oid: u64, rpid: u64) -> BpiResult<Self> {
        validate_comment_type(r#type)?;

        Ok(Self {
            r#type,
            oid: validate_nonzero_u64("oid", oid)?,
            rpid: validate_nonzero_u64("rpid", rpid)?,
        })
    }

    pub(crate) fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
        vec![
            ("type", comment_type_value(self.r#type).to_string()),
            ("oid", self.oid.to_string()),
            ("rpid", self.rpid.to_string()),
            ("csrf", csrf.to_string()),
        ]
    }
}

/// Parameters for reporting a comment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommentReportParams {
    r#type: CommentType,
    oid: u64,
    rpid: u64,
    reason: ReportReason,
    content: Option<String>,
}

impl CommentReportParams {
    pub fn new(r#type: CommentType, oid: u64, rpid: u64, reason: ReportReason) -> BpiResult<Self> {
        validate_comment_type(r#type)?;

        Ok(Self {
            r#type,
            oid: validate_nonzero_u64("oid", oid)?,
            rpid: validate_nonzero_u64("rpid", rpid)?,
            reason,
            content: None,
        })
    }

    pub fn content(mut self, content: impl Into<String>) -> BpiResult<Self> {
        self.content = Some(normalize_non_blank("content", content.into())?);
        Ok(self)
    }

    pub(crate) fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
        let mut pairs = vec![
            ("type", comment_type_value(self.r#type).to_string()),
            ("oid", self.oid.to_string()),
            ("rpid", self.rpid.to_string()),
            ("reason", report_reason_value(self.reason).to_string()),
            ("csrf", csrf.to_string()),
        ];

        if let Some(content) = &self.content {
            pairs.push(("content", content.clone()));
        }

        pairs
    }
}

impl<'a> CommentClient<'a> {
    /// Publishes a comment and returns the canonical payload result.
    pub async fn add(&self, params: CommentAddParams) -> BpiResult<CommentData> {
        let csrf = self.client.csrf()?;
        self.client
            .post(ADD_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_payload("comment.action.add")
            .await
    }

    /// Likes or unlikes a comment and returns the canonical payload result.
    pub async fn like(&self, params: CommentActionParams) -> BpiResult<Option<serde_json::Value>> {
        let csrf = self.client.csrf()?;
        self.client
            .post(LIKE_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_optional_payload("comment.action.like")
            .await
    }

    /// Dislikes or undislikes a comment and returns the canonical payload result.
    pub async fn dislike(
        &self,
        params: CommentActionParams,
    ) -> BpiResult<Option<serde_json::Value>> {
        let csrf = self.client.csrf()?;
        self.client
            .post(DISLIKE_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_optional_payload("comment.action.dislike")
            .await
    }

    /// Deletes a comment and returns the canonical payload result.
    pub async fn delete(
        &self,
        params: CommentDeleteParams,
    ) -> BpiResult<Option<serde_json::Value>> {
        let csrf = self.client.csrf()?;
        self.client
            .post(DELETE_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_optional_payload("comment.action.delete")
            .await
    }

    /// Tops or untops a comment and returns the canonical payload result.
    pub async fn top(&self, params: CommentActionParams) -> BpiResult<Option<serde_json::Value>> {
        let csrf = self.client.csrf()?;
        self.client
            .post(TOP_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_optional_payload("comment.action.top")
            .await
    }

    /// Reports a comment and returns the canonical payload result.
    pub async fn report(
        &self,
        params: CommentReportParams,
    ) -> BpiResult<Option<serde_json::Value>> {
        let csrf = self.client.csrf()?;
        self.client
            .post(REPORT_ENDPOINT)
            .form(&params.form_pairs(&csrf))
            .send_bpi_optional_payload("comment.action.report")
            .await
    }
}

fn validate_comment_type(value: CommentType) -> BpiResult<()> {
    if value == CommentType::Unknown {
        return Err(BpiError::invalid_parameter(
            "type",
            "comment type must be known",
        ));
    }

    Ok(())
}

fn comment_type_value(value: CommentType) -> u32 {
    value as u32
}

fn report_reason_value(value: ReportReason) -> u32 {
    value as u32
}

fn validate_nonzero_u64(field: &'static str, value: u64) -> BpiResult<u64> {
    if value == 0 {
        return Err(BpiError::invalid_parameter(field, "value must be non-zero"));
    }

    Ok(value)
}

fn validate_binary_action(value: u8) -> BpiResult<u8> {
    if matches!(value, 0 | 1) {
        return Ok(value);
    }

    Err(BpiError::invalid_parameter(
        "action",
        "value must be 0 or 1",
    ))
}

fn normalize_non_blank(field: &'static str, value: String) -> BpiResult<String> {
    let value = value.trim().to_string();
    if value.is_empty() {
        return Err(BpiError::invalid_parameter(field, "value cannot be blank"));
    }

    Ok(value)
}

#[cfg(test)]
mod tests {
    use crate::BpiError;

    use super::{CommentActionParams, CommentAddParams, CommentReportParams, CommentType};

    #[test]
    fn comment_add_params_rejects_blank_message() {
        let err = CommentAddParams::new(CommentType::Video, 23199, "  ").unwrap_err();

        assert!(matches!(
            err,
            BpiError::InvalidParameter {
                field: "message",
                ..
            }
        ));
    }

    #[test]
    fn comment_add_params_serializes_reply_fields() -> Result<(), BpiError> {
        let params = CommentAddParams::new(CommentType::Video, 23199, "hello")?
            .root(2554491176)?
            .parent(2554491177)?;

        assert_eq!(
            params.form_pairs("csrf-token"),
            vec![
                ("type", "1".to_string()),
                ("oid", "23199".to_string()),
                ("message", "hello".to_string()),
                ("plat", "1".to_string()),
                ("csrf", "csrf-token".to_string()),
                ("root", "2554491176".to_string()),
                ("parent", "2554491177".to_string()),
            ]
        );
        Ok(())
    }

    #[test]
    fn comment_action_params_rejects_invalid_action() {
        let err = CommentActionParams::new(CommentType::Video, 23199, 2554491176, 2).unwrap_err();

        assert!(matches!(
            err,
            BpiError::InvalidParameter {
                field: "action",
                ..
            }
        ));
    }

    #[test]
    fn comment_report_params_rejects_blank_content() -> Result<(), BpiError> {
        let err = CommentReportParams::new(
            CommentType::Video,
            23199,
            2554491176,
            super::ReportReason::Other,
        )?
        .content(" ")
        .unwrap_err();

        assert!(matches!(
            err,
            BpiError::InvalidParameter {
                field: "content",
                ..
            }
        ));
        Ok(())
    }
}