bpi_rs/opus/
space.rs

1//! 空间图文
2//!
3//! [空间图文](https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/opus/space.md#空间图文)
4
5use crate::{BilibiliRequest, BpiClient, BpiError, BpiResponse};
6use serde::{Deserialize, Serialize};
7
8/// 空间图文封面信息
9#[derive(Debug, Clone, Deserialize, Serialize)]
10pub struct SpaceCover {
11    /// 封面高度
12    pub height: u32,
13    /// 图片 URL
14    pub url: String,
15    /// 封面宽度
16    pub width: u32,
17}
18
19/// 空间图文统计信息
20#[derive(Debug, Clone, Deserialize, Serialize)]
21pub struct SpaceStat {
22    /// 点赞数(字符串)
23    pub like: String,
24    /// 浏览数(字符串,仅自己可见)
25    pub view: Option<String>,
26}
27
28/// 空间图文单条信息
29#[derive(Debug, Clone, Deserialize, Serialize)]
30pub struct SpaceItem {
31    /// 文本内容
32    pub content: String,
33    /// 封面信息,可选
34    pub cover: Option<SpaceCover>,
35    /// 跳转 URL
36    pub jump_url: String,
37    /// opus id
38    pub opus_id: String,
39    /// 统计信息
40    pub stat: SpaceStat,
41}
42
43/// 空间图文响应数据
44#[derive(Debug, Clone, Deserialize, Serialize)]
45pub struct SpaceData {
46    /// 是否还有更多
47    pub has_more: bool,
48    /// 图文列表
49    pub items: Vec<SpaceItem>,
50    /// 下一页 offset
51    pub offset: String,
52    /// 更新数
53    pub update_num: u32,
54}
55
56impl BpiClient {
57    /// 获取用户空间图文
58    ///
59    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/opus
60    ///
61    /// 参数
62    ///
63    /// | 名称 | 类型 | 说明 |
64    /// | ---- | ---- | ---- |
65    /// | `mid` | u64 | 用户 UID |
66    /// | `page` | Option<u32> | 页码,默认 0 |
67    /// | `offset` | Option<&str> | 下一页偏移量 |
68    /// | `typ` | Option<&str> | 类型:`all`/`article`/`dynamic`,默认 `all` |
69    pub async fn opus_space_feed(
70        &self,
71        mid: u64,
72        page: Option<u32>,
73        offset: Option<&str>,
74        typ: Option<&str>, // all/article/dynamic
75    ) -> Result<BpiResponse<SpaceData>, BpiError> {
76        let query = vec![
77            ("host_mid", mid.to_string()),
78            ("page", page.unwrap_or(0).to_string()),
79            ("offset", offset.unwrap_or("").to_string()),
80            ("type", typ.unwrap_or("all").to_string()),
81            ("web_location", "333.1387".to_string()),
82        ];
83
84        self.get("https://api.bilibili.com/x/polymer/web-dynamic/v1/opus/feed/space")
85            .query(&query)
86            .send_bpi("获取用户空间图文")
87            .await
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use tracing::info;
95
96    #[tokio::test]
97    async fn test_opus_space_feed() {
98        let bpi = BpiClient::new();
99        let resp = bpi.opus_space_feed(4279370, Some(1), None, None).await;
100        assert!(resp.is_ok());
101        if let Ok(r) = resp {
102            info!("空间图文返回: {:?}", r);
103        }
104    }
105}