Skip to main content

bpi_rs/live/
emoticons.rs

1use serde::{ Deserialize, Serialize };
2
3use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
4
5// ================= 数据结构 =================
6
7#[derive(Debug, Serialize, Clone, Deserialize)]
8pub struct EmoticonItem {
9    /// 突出展示
10    pub bulge_display: i32,
11    /// 描述
12    pub descript: String,
13    /// 触发关键词
14    pub emoji: String,
15    /// 表情ID
16    pub emoticon_id: i64,
17    /// 表情唯一标识
18    pub emoticon_unique: String,
19    /// 表情值类型
20    pub emoticon_value_type: i32,
21    /// 表情图片高度
22    pub height: i32,
23    /// 身份限制标识
24    pub identity: i32,
25    /// 播放器区域内展示
26    pub in_player_area: i32,
27    /// 是否为动态表情
28    pub is_dynamic: i32,
29    /// 使用权限
30    pub perm: i32,
31    /// 解锁需求礼物
32    pub unlock_need_gift: i32,
33    /// 解锁需求等级
34    pub unlock_need_level: i32,
35    /// 解锁展示颜色
36    pub unlock_show_color: String,
37    /// 解锁展示图片
38    pub unlock_show_image: String,
39    /// 解锁展示文字
40    pub unlock_show_text: String,
41    /// 表情图片URL
42    pub url: String,
43    /// 表情图片宽度
44    pub width: i32,
45}
46
47#[derive(Debug, Serialize, Clone, Deserialize)]
48pub struct TopShowItem {
49    /// 图片
50    pub image: String,
51    /// 文字
52    pub text: String,
53}
54
55#[derive(Debug, Serialize, Clone, Deserialize)]
56pub struct TopShow {
57    /// 左上
58    pub top_left: TopShowItem,
59    /// 右上
60    pub top_right: TopShowItem,
61}
62
63#[derive(Debug, Serialize, Clone, Deserialize)]
64pub struct EmoticonPackage {
65    /// 封面URL
66    pub current_cover: String,
67    /// 表情列表
68    pub emoticons: Vec<EmoticonItem>,
69    /// 文字描述
70    pub pkg_descript: String,
71    /// 包ID
72    pub pkg_id: i64,
73    /// 包名称
74    pub pkg_name: String,
75    /// 使用权限
76    pub pkg_perm: i32,
77    /// 包类型
78    pub pkg_type: i32,
79    /// 最近使用的表情
80    pub recently_used_emoticons: Vec<serde_json::Value>,
81    /// 顶部展示信息
82    pub top_show: Option<TopShow>,
83    /// 最近使用的顶部展示信息
84    pub top_show_recent: Option<TopShow>,
85    /// 解锁所需身份标识
86    pub unlock_identity: i32,
87    /// 解锁所需礼物
88    pub unlock_need_gift: i32,
89}
90
91#[derive(Debug, Serialize, Clone, Deserialize)]
92pub struct EmoticonData {
93    /// 表情包数据
94    pub data: Vec<EmoticonPackage>,
95    /// 品牌标识
96    pub fans_brand: i32,
97    /// 购买链接
98    pub purchase_url: Option<String>,
99}
100
101pub type EmoticonResponse = BpiResponse<EmoticonData>;
102
103// ================= 实现 =================
104
105impl BpiClient {
106    /// 获取直播间的表情包
107    ///
108    pub async fn live_emoticons(
109        &self,
110        room_id: i64,
111        platform: &str
112    ) -> Result<EmoticonResponse, BpiError> {
113        let params = [
114            ("room_id", room_id.to_string()),
115            ("platform", platform.to_string()),
116        ];
117
118        let resp: EmoticonResponse = self
119            .get("https://api.live.bilibili.com/xlive/web-ucenter/v2/emoticon/GetEmoticons")
120            .query(&params)
121            .send_bpi("获取直播间表情包").await?;
122
123        Ok(resp)
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[tokio::test]
132    async fn test_get_live_emoticons() -> Result<(), Box<BpiError>> {
133        let bpi = BpiClient::new();
134        let resp = bpi.live_emoticons(14047, "pc").await?;
135
136        let data = resp.data.unwrap();
137        assert!(data.data.len() > 0);
138        Ok(())
139    }
140}