use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PlayerInfoResponseData {
pub aid: u64,
pub bvid: String,
pub allow_bp: bool,
pub no_share: bool,
pub cid: u64,
pub dm_mask: Option<DmMaskInfo>,
pub subtitle: Option<SubtitleInfo>,
#[serde(default)]
pub view_points: Vec<ViewPoint>,
pub ip_info: Option<serde_json::Value>,
pub login_mid: u64,
pub login_mid_hash: Option<String>,
pub is_owner: bool,
pub name: String,
pub permission: String,
pub level_info: Option<serde_json::Value>,
pub vip: Option<serde_json::Value>,
pub answer_status: u8,
pub block_time: u64,
pub role: String,
pub last_play_time: u64,
pub last_play_cid: u64,
pub now_time: u64,
pub online_count: Option<u64>,
pub need_login_subtitle: bool,
pub preview_toast: String,
pub interaction: Option<InteractionInfo>,
pub options: Option<PlayerOptions>,
pub guide_attention: Option<serde_json::Value>,
pub jump_card: Option<serde_json::Value>,
pub operation_card: Option<serde_json::Value>,
pub online_switch: Option<serde_json::Value>,
pub fawkes: Option<serde_json::Value>,
pub show_switch: Option<serde_json::Value>,
pub bgm_info: Option<BgmInfo>,
pub toast_block: bool,
pub is_upower_exclusive: bool,
pub is_upower_play: bool,
pub is_ugc_pay_preview: bool,
pub elec_high_level: Option<ElecHighLevel>,
pub disable_show_up_info: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DmMaskInfo {
pub cid: u64,
pub plat: u8,
pub fps: u64,
pub time: u64,
pub mask_url: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SubtitleInfo {
pub allow_submit: bool,
pub lan: String,
pub lan_doc: String,
#[serde(default)]
pub subtitles: Vec<SubtitleItem>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SubtitleItem {
pub ai_status: u8,
pub ai_type: u8,
pub id: u64,
pub id_str: String,
pub is_lock: bool,
pub lan: String,
pub lan_doc: String,
pub subtitle_url: String,
#[serde(rename = "type")]
pub subtitle_type: u8,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ViewPoint {
pub content: String,
pub from: u64,
pub to: u64,
#[serde(rename = "type")]
pub point_type: u8,
#[serde(rename = "imgUrl")]
pub img_url: String,
#[serde(rename = "logoUrl")]
pub logo_url: String,
pub team_type: String,
pub team_name: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct InteractionInfo {
pub graph_version: u64,
pub msg: Option<String>,
pub error_toast: Option<String>,
pub mark: Option<u8>,
pub need_reload: Option<u8>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PlayerOptions {
pub is_360: bool,
pub without_vip: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BgmInfo {
pub music_id: String,
pub music_title: String,
pub jump_url: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ElecHighLevel {
pub privilege_type: u64,
pub title: String,
pub sub_title: String,
pub show_button: bool,
pub button_text: String,
pub jump_url: Option<serde_json::Value>,
pub intro: String,
#[serde(default)]
pub open: bool,
#[serde(default)]
pub new: bool,
#[serde(default)]
pub question_text: String,
#[serde(default)]
pub qa_detail_link: String,
}
impl BpiClient {
pub async fn video_player_info_v2(
&self,
aid: Option<u64>,
bvid: Option<&str>,
cid: u64,
season_id: Option<u64>,
ep_id: Option<u64>
) -> Result<BpiResponse<PlayerInfoResponseData>, BpiError> {
if aid.is_none() && bvid.is_none() {
return Err(BpiError::parse("必须提供 aid 或 bvid"));
}
let mut params = vec![("cid", cid.to_string())];
if let Some(a) = aid {
params.push(("aid", a.to_string()));
}
if let Some(b) = bvid {
params.push(("bvid", b.to_string()));
}
if let Some(s) = season_id {
params.push(("season_id", s.to_string()));
}
if let Some(e) = ep_id {
params.push(("ep_id", e.to_string()));
}
let params = self.get_wbi_sign2(params).await?;
self
.get("https://api.bilibili.com/x/player/wbi/v2")
.query(¶ms)
.send_bpi("获取 web 播放器信息").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
const TEST_AID: u64 = 1906473802;
const TEST_CID: u64 = 636329244;
#[tokio::test]
async fn test_video_player_info_v2_by_aid() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.video_player_info_v2(Some(TEST_AID), None, TEST_CID, None, None).await?;
let data = resp.into_data()?;
info!("播放器信息: {:?}", data);
assert_eq!(data.aid, TEST_AID);
assert_eq!(data.cid, TEST_CID);
Ok(())
}
#[tokio::test]
async fn test_video_player_info_v2_by_bvid() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.video_player_info_v2(Some(TEST_AID), None, TEST_CID, None, None).await?;
let data = resp.into_data()?;
info!("播放器信息: {:?}", data);
assert_eq!(data.aid, TEST_AID);
assert_eq!(data.cid, TEST_CID);
Ok(())
}
}