bpi_rs/live/
recommend.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{BilibiliRequest, BpiClient, BpiError, BpiResponse};
4
5// ================= 数据结构 =================
6
7#[derive(Debug, Serialize, Clone, Deserialize)]
8pub struct WatchedShow {
9    /// 开关
10    pub switch: bool,
11    /// 看过人数
12    pub num: i32,
13    /// 小文本
14    pub text_small: String,
15    /// 大文本
16    pub text_large: String,
17    /// 图标URL
18    pub icon: String,
19    /// 图标位置
20    pub icon_location: i32,
21    /// Web端图标URL
22    pub icon_web: String,
23}
24
25#[derive(Debug, Serialize, Clone, Deserialize)]
26pub struct RecommendRoom {
27    /// 头像框
28    pub head_box: Option<serde_json::Value>,
29    /// 分区ID
30    pub area_v2_id: i32,
31    /// 父分区ID
32    pub area_v2_parent_id: i32,
33    /// 分区名称
34    pub area_v2_name: String,
35    /// 父分区名称
36    pub area_v2_parent_name: String,
37    /// 广播类型
38    pub broadcast_type: i32,
39    /// 封面URL
40    pub cover: String,
41    /// 直播间链接
42    pub link: String,
43    /// 观看人数
44    pub online: i32,
45    /// 挂件信息
46    #[serde(rename = "pendant_Info")]
47    pub pendant_info: serde_json::Value,
48    /// 直播间ID
49    pub roomid: i64,
50    /// 直播间标题
51    pub title: String,
52    /// 主播用户名
53    pub uname: String,
54    /// 主播头像URL
55    pub face: String,
56    /// 认证信息
57    pub verify: serde_json::Value,
58    /// 主播用户mid
59    pub uid: i64,
60    /// 关键帧URL
61    pub keyframe: String,
62    /// 是否自动播放
63    pub is_auto_play: i32,
64    /// 头像框类型
65    pub head_box_type: i32,
66    /// 标记
67    pub flag: i32,
68    /// 会话ID
69    pub session_id: String,
70    /// 展示回调URL
71    pub show_callback: String,
72    /// 点击回调URL
73    pub click_callback: String,
74    /// 特殊ID
75    pub special_id: i32,
76    /// 观看展示
77    pub watched_show: WatchedShow,
78    /// 是否为NFT头像
79    pub is_nft: i32,
80    /// NFT标记
81    pub nft_dmark: String,
82    /// 是否为广告
83    pub is_ad: bool,
84    /// 广告透明内容
85    pub ad_transparent_content: Option<serde_json::Value>,
86    /// 显示广告图标
87    pub show_ad_icon: bool,
88    /// 状态
89    pub status: bool,
90    /// 关注者数量
91    pub followers: i32,
92}
93
94#[derive(Debug, Serialize, Clone, Deserialize)]
95pub struct RecommendData {
96    /// 推荐房间列表
97    pub recommend_room_list: Vec<RecommendRoom>,
98    /// 置顶直播间号
99    pub top_room_id: i64,
100}
101
102impl BpiClient {
103    /// 主页获取直播推荐
104    ///
105
106    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/live
107    pub async fn live_recommend(&self) -> Result<BpiResponse<RecommendData>, BpiError> {
108        let params = [("platform", "web"), ("web_location", "333.1007")];
109
110        let resp = self
111            .get("https://api.live.bilibili.com/xlive/web-interface/v1/webMain/getMoreRecList")
112            .query(&params)
113            .send_bpi("主页获取直播推荐")
114            .await?;
115
116        Ok(resp)
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[tokio::test]
125    async fn test_get_live_recommend() -> Result<(), Box<BpiError>> {
126        let bpi = BpiClient::new();
127        let resp = bpi.live_recommend().await?;
128
129        let data = resp.data.unwrap();
130
131        assert!(data.recommend_room_list.len() > 0);
132        Ok(())
133    }
134}