use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OnlineTotalShowSwitch {
pub total: bool,
pub count: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OnlineTotalResponseData {
pub total: String,
pub count: String,
pub show_switch: OnlineTotalShowSwitch,
}
impl BpiClient {
pub async fn video_online_total(
&self,
aid: Option<u64>,
bvid: Option<&str>,
cid: u64
) -> Result<BpiResponse<OnlineTotalResponseData>, BpiError> {
if aid.is_none() && bvid.is_none() {
return Err(BpiError::parse("必须提供 aid 或 bvid"));
}
let mut req = self
.get("https://api.bilibili.com/x/player/online/total")
.query(&[("cid", &cid.to_string())]);
if let Some(a) = aid {
req = req.query(&[("aid", &a.to_string())]);
}
if let Some(b) = bvid {
req = req.query(&[("bvid", b)]);
}
req.send_bpi("获取视频在线人数").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
const TEST_AID: u64 = 759949922;
const TEST_CID: u64 = 392402545;
const TEST_BVID: &str = "BV1y64y1q757";
#[tokio::test]
async fn test_video_online_total_by_aid() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.video_online_total(Some(TEST_AID), None, TEST_CID).await?;
let data = resp.into_data()?;
info!("视频在线人数: {:?}", data);
assert!(!data.count.is_empty());
assert!(!data.total.is_empty());
Ok(())
}
#[tokio::test]
async fn test_video_online_total_by_bvid() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.video_online_total(None, Some(TEST_BVID), TEST_CID).await?;
let data = resp.into_data()?;
info!("视频在线人数: {:?}", data);
assert!(!data.count.is_empty());
assert!(!data.total.is_empty());
Ok(())
}
}