kovi_plugin_live_agent/
live.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Bilibili live module

use std::{fmt::Display, sync::Arc, time::Duration};

use indoc::{formatdoc, writedoc};
use kovi::{Message, MsgEvent};
use serde::{Deserialize, Deserializer};

use crate::{
    exception::PluginResult,
    global_state::{self, LiveSwitch},
    std_error, std_info,
    util::schedule_task_blocking,
    CONFIG,
};

async fn query_liveroom(room_id: &str) -> PluginResult<LiveRoom> {
    let url = "https://api.live.bilibili.com/room/v1/Room/get_info";
    let params = [("room_id", room_id)];
    let client = reqwest::Client::new();
    let room = client.get(url).query(&params).send().await?.json().await?;
    Ok(room)
}

async fn query_handler(e: Arc<MsgEvent>, room_id: &str, online_msg: &str, offline_msg: &str) {
    // no-op if not group message
    if e.group_id.is_none() {
        return;
    };

    let room = match query_liveroom(room_id).await {
        Ok(room) => room,
        Err(err) => {
            std_error!("Query liveroom failed: {err}");
            return;
        }
    };
    if !room.exist {
        let message = Message::from(format!("直播间{}不存在", room_id));
        e.reply(message);
        return;
    }
    let status_str = if room.data.is_streaming {
        online_msg
    } else {
        offline_msg
    };
    let resp = formatdoc!(
        "
        {status_str}
        链接:{}
        {}
        ",
        LiveRoom::url_from_id(room_id),
        room
    );
    let mut message = Message::new().add_text(resp);
    // add key_frame if exists, otherwise fallback to user_cover
    let fallback_list = [room.data.keyframe, room.data.user_cover];
    let image = fallback_list.iter().find(|&x| !x.is_empty());
    if let Some(img) = image {
        message = message.add_image(img);
    }
    e.reply(message);
}

pub async fn general_query_handler(e: Arc<MsgEvent>) {
    // no-op if no text
    let Some(msg) = e.borrow_text() else {
        return;
    };
    let query_message = "查询直播间";
    if !msg.contains(query_message) {
        return;
    }
    let msg = msg.replace(query_message, "");
    let room_id = msg.trim();
    if room_id.parse::<usize>().is_err() {
        e.reply("直播间不存在");
        return;
    }
    query_handler(e, room_id, "直播中", "不在直播").await;
}

pub async fn local_query_handler(e: Arc<MsgEvent>) {
    // no-op if not group message
    let Some(group_id) = e.group_id else {
        return;
    };
    // no-op if no text
    let Some(msg) = e.borrow_text() else {
        return;
    };
    // no-op if no group config
    let config = CONFIG.get().unwrap();
    let Some(ref groups) = config.groups else {
        return;
    };
    let Some(group) = groups.iter().find(|&g| g.id == group_id) else {
        return;
    };
    // no-op if no live config
    let Some(ref live) = group.live else {
        return;
    };

    // now pre-configured group found, and it has live setting
    // check query_msg
    if msg.contains(&live.query_message) {
        query_handler(e, &live.room_id, &live.online_msg, &live.offline_msg).await;
    }
}

pub async fn subscribe_live() {
    let config = CONFIG.get().unwrap();

    // no-op if no group config
    let Some(ref groups) = config.groups else {
        return;
    };

    let id_lives = groups
        .iter()
        .filter_map(|g| g.live.as_ref().map(|live| (g.id, live)));

    // spawn a task for each live room
    for (group_id, live) in id_lives {
        kovi::spawn(async move {
            let duration = Duration::from_secs(live.poll_interval_sec);
            schedule_task_blocking(duration, move || {
                async move {
                    let room = match query_liveroom(&live.room_id).await {
                        Ok(v) => v,
                        Err(err) => {
                            std_error!("Query live room failed: {err}");
                            return;
                        }
                    };
                    if !room.exist {
                        std_error!("直播间{}不存在", live.room_id);
                        return;
                    }
                    let bot = global_state::get_bot();
                    match live.get_switch() {
                        LiveSwitch::On => {
                            // used to be online, send msg only if offline
                            if !room.data.is_streaming {
                                std_info!("not streaming, offline notification");
                                let msg = Message::new().add_text(&live.offline_msg);
                                bot.send_group_msg(group_id, msg);
                                live.set_switch(LiveSwitch::Off);
                            }
                        }
                        LiveSwitch::Off => {
                            // used to be offline, send msg only if online
                            if room.data.is_streaming {
                                std_info!("streaming, online notification");
                                let resp = formatdoc!(
                                    "
                                    {}
                                    链接:{}
                                    {}
                                    ",
                                    &live.online_msg,
                                    LiveRoom::url_from_id(&live.room_id),
                                    room
                                );
                                let mut message = Message::new().add_text(resp);

                                // add key_frame if exists, otherwise fallback to user_cover
                                let fallback_list = [room.data.keyframe, room.data.user_cover];
                                let image = fallback_list.iter().find(|&x| !x.is_empty());
                                if let Some(img) = image {
                                    message = message.add_image(img);
                                }
                                bot.send_group_msg(group_id, message);
                                live.set_switch(LiveSwitch::On);
                            }
                        }
                        LiveSwitch::Init => {
                            // avoid online notification on launching
                            std_info!("Live switch: Init");
                            match room.data.is_streaming {
                                true => live.set_switch(LiveSwitch::On),
                                false => live.set_switch(LiveSwitch::Off),
                            }
                        }
                        LiveSwitch::Trap => {
                            // if I were myself 2 years ago I would use unreachable!()
                            std_error!(
                                "Subscribe live in trap state: group id = {}",
                                &live.room_id
                            );
                        }
                    }
                }
            })
            .await;
        });
    }
}

#[derive(Deserialize, Debug)]
pub struct LiveRoom {
    #[serde(rename = "code", deserialize_with = "parse_code")]
    pub exist: bool,
    pub data: LiveData,
}

impl LiveRoom {
    pub fn url_from_id(room_id: &str) -> String {
        format!("https://live.bilibili.com/{}", room_id)
    }
}

impl Display for LiveRoom {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writedoc!(
            f,
            "
            分区:{}
            标题:{}
            简介:{}
            热度:{}, 关注:{}
            ",
            self.data.area_name,
            self.data.title,
            self.data.description,
            self.data.online,
            self.data.attention
        )
    }
}

#[derive(Deserialize, Debug)]
pub struct LiveData {
    #[serde(rename = "live_status", deserialize_with = "parse_status")]
    pub is_streaming: bool,
    pub online: usize,
    pub attention: usize,
    pub keyframe: String,
    pub user_cover: String,
    pub area_name: String,
    pub description: String,
    pub title: String,
}

fn parse_code<'de, D>(d: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    let Ok(code) = i32::deserialize(d) else {
        return Ok(false);
    };
    match code {
        0 => Ok(true),
        _ => Ok(false),
    }
}

fn parse_status<'de, D>(d: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    let Ok(code) = i32::deserialize(d) else {
        return Ok(false);
    };
    match code {
        1 => Ok(true),
        _ => Ok(false),
    }
}