Skip to main content

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    /// # 文档
60    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/opus)
61    ///
62    /// # 参数
63    ///
64    /// | 名称 | 类型 | 说明 |
65    /// | ---- | ---- | ---- |
66    /// | `mid` | u64 | 用户 UID |
67    /// | `page` | `Option<u32>` | 页码,默认 0 |
68    /// | `offset` | `Option<&str>` | 下一页偏移量 |
69    /// | `typ` | `Option<&str>` | 类型:`all`/`article`/`dynamic`,默认 `all` |
70    pub async fn opus_space_feed(
71        &self,
72        mid: u64,
73        page: Option<u32>,
74        offset: Option<&str>,
75        typ: Option<&str> // all/article/dynamic
76    ) -> Result<BpiResponse<SpaceData>, BpiError> {
77        let query = vec![
78            ("host_mid", mid.to_string()),
79            ("page", page.unwrap_or(0).to_string()),
80            ("offset", offset.unwrap_or("").to_string()),
81            ("type", typ.unwrap_or("all").to_string()),
82            ("web_location", "333.1387".to_string())
83        ];
84
85        self
86            .get("https://api.bilibili.com/x/polymer/web-dynamic/v1/opus/feed/space")
87            .query(&query)
88            .send_bpi("获取用户空间图文").await
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use tracing::info;
96
97    #[tokio::test]
98    async fn test_opus_space_feed() {
99        let bpi = BpiClient::new();
100        let resp = bpi.opus_space_feed(4279370, Some(1), None, None).await;
101        assert!(resp.is_ok());
102        if let Ok(r) = resp {
103            info!("空间图文返回: {:?}", r);
104        }
105    }
106}