use chrono::{DateTime, Utc};
use serde_with::{serde_as, DurationSeconds};
use std::time::Duration;
use super::prelude::*;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct VideoOwner {
pub mid: u64,
pub name: String,
#[serde(rename = "face")]
pub avatar_url: String,
}
#[serde_as]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct VideoInfo {
pub bvid: String,
pub aid: u64,
pub videos: usize,
pub title: String,
#[serde(default)]
pub copyright: i32,
#[serde(rename = "pubdate", with = "chrono::serde::ts_seconds")]
pub publish_at: DateTime<Utc>,
#[serde(rename = "ctime", with = "chrono::serde::ts_seconds_option", default)]
pub create_at: Option<DateTime<Utc>>,
pub desc: String,
#[serde_as(as = "DurationSeconds<u64>")]
pub duration: Duration,
#[serde(rename = "pic")]
pub cover_url: String,
pub stat: VideoStat,
pub owner: VideoOwner,
pub pages: Vec<VideoPage>,
}
#[serde_as]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct VideoPage {
pub cid: u64,
pub page: u64,
pub from: String,
pub part: String,
#[serde_as(as = "DurationSeconds<u64>")]
pub duration: Duration,
pub first_frame: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct VideoStat {
pub aid: u64,
pub view: u64,
pub danmaku: u64,
pub reply: u64,
pub favorite: u64,
pub coin: u64,
pub share: u64,
pub now_rank: u64,
#[serde(rename = "his_rank")]
pub history_rank: u64,
pub like: u64,
pub dislike: u64,
#[serde(default)]
pub argue_msg: String,
}
impl Request for VideoInfo {
type Args = String;
fn request(client: &Client, args: Self::Args) -> RequestResponse<Self> {
const URL: &str = "https://api.bilibili.com/x/web-interface/view";
let request = client.get(URL).query(&[("bvid", &args)]).send();
Box::pin(async move { request.await?.bili_data().await })
}
}
#[cfg(test)]
#[tokio::test]
async fn test_video_info() -> Result<()> {
let client = crate::connection::new_client()?;
let info = VideoInfo::request(&client, "BV1QB4y1u7Jj".to_string()).await?;
assert_eq!(info.aid, 588385189);
assert!(info.stat.like > 4100);
assert!(info.title.contains("亲爱的那不是爱情"));
assert_eq!(info.pages.len(), 1);
Ok(())
}