Skip to main content

bpi_rs/article/
info.rs

1//! 专栏基本信息
2//!
3//! [查看 API 文档](https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/article/info.md)
4
5use crate::article::models::ArticleStats;
6use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
7use serde::{ Deserialize, Serialize };
8
9/// 专栏基本信息数据
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ArticleInfoData {
12    /// 是否点赞 0:未点赞 1:已点赞 需要登录(Cookie) 未登录为0
13    pub like: i32,
14    /// 是否关注文章作者 false:未关注 true:已关注 需要登录(Cookie) 未登录为false
15    pub attention: bool,
16    /// 是否收藏 false:未收藏 true:已收藏 需要登录(Cookie) 未登录为false
17    pub favorite: bool,
18    /// 为文章投币数
19    pub coin: i32,
20    /// 状态数信息
21    pub stats: ArticleStats,
22    /// 文章标题
23    pub title: String,
24    /// 文章头图url
25    pub banner_url: String,
26    /// 文章作者mid
27    pub mid: i64,
28    /// 文章作者昵称
29    pub author_name: String,
30    /// true 作用尚不明确
31    pub is_author: bool,
32    /// 动态封面
33    pub image_urls: Vec<String>,
34    /// 封面图片
35    pub origin_image_urls: Vec<String>,
36    /// true 作用尚不明确
37    pub shareable: bool,
38    /// true 作用尚不明确
39    pub show_later_watch: bool,
40    /// true 作用尚不明确
41    pub show_small_window: bool,
42    /// 是否收于文集 false:否 true:是
43    pub in_list: bool,
44    /// 上一篇文章cvid 无为0
45    pub pre: i64,
46    /// 下一篇文章cvid 无为0
47    pub next: i64,
48    /// 分享方式列表
49    pub share_channels: Vec<ShareChannel>,
50    /// 文章类别 0:文章 2:笔记
51    pub r#type: i32,
52    /// 视频URL
53    #[serde(default)]
54    pub video_url: String,
55    /// 位置信息
56    #[serde(default)]
57    pub location: String,
58    /// 是否禁用分享
59    #[serde(default)]
60    pub disable_share: bool,
61}
62
63/// 分享方式
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct ShareChannel {
66    /// 分享名称
67    pub name: String,
68    /// 分享图片url
69    pub picture: String,
70    /// 分享代号
71    pub share_channel: String,
72}
73
74impl BpiClient {
75    /// 获取专栏文章基本信息
76    ///
77    /// # 参数
78    /// | 名称 | 类型 | 说明              |
79    /// | ---- | ---- | ----------------- |
80    /// | `id` | i64  | 专栏 cvid (必要)  |
81    ///
82    /// # 文档
83    /// [获取专栏文章基本信息](https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/article/info.md#获取专栏文章基本信息)
84    pub async fn article_info(&self, id: i64) -> Result<BpiResponse<ArticleInfoData>, BpiError> {
85        self
86            .get("https://api.bilibili.com/x/article/viewinfo")
87            .query(&[("id", id.to_string())])
88            .send_bpi("获取专栏文章基本信息").await
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    const TEST_CVID: i64 = 2;
97
98    #[tokio::test]
99    async fn test_article_info() -> Result<(), Box<BpiError>> {
100        let bpi = BpiClient::new();
101
102        let cvid = TEST_CVID;
103
104        let result = bpi.article_info(cvid).await?;
105
106        let data = result.data.unwrap();
107        assert!(!data.title.is_empty());
108        assert!(!data.author_name.is_empty());
109        assert!(data.mid > 0);
110
111        Ok(())
112    }
113
114    #[tokio::test]
115    async fn test_article_stats() -> Result<(), Box<BpiError>> {
116        let bpi = BpiClient::new();
117
118        let result = bpi.article_info(TEST_CVID).await?;
119
120        let data = result.data.unwrap();
121        let stats = &data.stats;
122        assert!(stats.view >= 0);
123        assert!(stats.favorite >= 0);
124        assert!(stats.like >= 0);
125        assert!(stats.reply >= 0);
126
127        Ok(())
128    }
129}