Skip to main content

live_room_info/
live_room_info.rs

1//! 读取直播间基础信息。
2//!
3//! ```powershell
4//! $env:BILI_ROOM_ID = "3818081"
5//! cargo run --example live_room_info --features live
6//! ```
7
8use bpi_rs::{BpiClient, BpiError, BpiResult};
9
10const DEFAULT_ROOM_ID: i64 = 3818081;
11
12fn room_id() -> BpiResult<i64> {
13    match std::env::var("BILI_ROOM_ID") {
14        Ok(value) => value.parse().map_err(|_| {
15            BpiError::invalid_parameter("room_id", "room id must be a signed integer")
16        }),
17        Err(_) => Ok(DEFAULT_ROOM_ID),
18    }
19}
20
21#[tokio::main]
22async fn main() -> BpiResult<()> {
23    let room_id = room_id()?;
24    let client = BpiClient::new()?;
25    let info = client.live().room_info(room_id).await?;
26
27    println!("直播间: {} ({})", info.title, info.room_id);
28    println!("主播 mid: {}", info.uid);
29    println!(
30        "状态: live_status={} 分区={}·{}",
31        info.live_status, info.parent_area_name, info.area_name
32    );
33    println!("在线: {} 关注: {}", info.online, info.attention);
34
35    Ok(())
36}