Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

3.0 KB
Raw Blame
use serde::{Deserialize, Serialize};

use super::api::{ApiClient, BilibiliError, Result};
use super::wbi;

pub mod ass;
pub mod burn;
pub mod export;
pub mod filter;
pub mod json;
pub mod proto;
pub mod xml;

pub use filter::DanmakuFilter;
pub use proto::DanmakuElem;

const SEG_URL: &str = "https://api.bilibili.com/x/v2/dm/wbi/web/seg.so";
const SEGMENT_DURATION_SECS: u64 = 360;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum DanmakuFormat {
    Xml,
    Ass,
    Json,
}

impl DanmakuFormat {
    /// Nome vindo do front (ou de uma configuração), sem diferenciar caixa.
    pub fn from_name(name: &str) -> Option<Self> {
        match name.trim().to_lowercase().as_str() {
            "xml" => Some(DanmakuFormat::Xml),
            "ass" => Some(DanmakuFormat::Ass),
            "json" => Some(DanmakuFormat::Json),
            _ => None,
        }
    }

    pub fn extension(self) -> &'static str {
        match self {
            DanmakuFormat::Xml => "xml",
            DanmakuFormat::Ass => "ass",
            DanmakuFormat::Json => "json",
        }
    }
}

pub async fn fetch_elems(
    client: &ApiClient,
    cid: u64,
    duration_secs: u64,
) -> Result<Vec<DanmakuElem>> {
    let segments = ((duration_secs + SEGMENT_DURATION_SECS - 1) / SEGMENT_DURATION_SECS).max(1);
    let mut all: Vec<DanmakuElem> = Vec::new();
    for i in 1..=segments {
        let params: Vec<(&str, String)> = vec![
            ("type", "1".to_string()),
            ("oid", cid.to_string()),
            ("segment_index", i.to_string()),
        ];
        let signed = wbi::signed_query(client, &params).await?;
        let url = format!("{}?{}", SEG_URL, signed);
        match client.get_bytes(&url).await {
            Ok(bytes) => {
                if let Ok(elems) = proto::decode_segment(&bytes) {
                    all.extend(elems.into_iter().filter(|e| !e.content.is_empty()));
                }
            }
            Err(e) => {
                tracing::warn!(
                    "[bilibili] danmaku segment {}/{} fetch failed: {:?}",
                    i,
                    segments,
                    e
                );
            }
        }
    }
    if all.is_empty() {
        return Err(BilibiliError::ContentUnavailable);
    }
    all.sort_by_key(|e| e.progress_ms);
    Ok(all)
}

pub fn render(elems: &[DanmakuElem], format: DanmakuFormat) -> String {
    match format {
        DanmakuFormat::Xml => xml::render(elems),
        DanmakuFormat::Json => json::render(elems),
        DanmakuFormat::Ass => ass::render(elems, &ass::AssRenderOptions::default()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_names_are_case_insensitive() {
        assert_eq!(DanmakuFormat::from_name("ASS"), Some(DanmakuFormat::Ass));
        assert_eq!(
            DanmakuFormat::from_name(" json "),
            Some(DanmakuFormat::Json)
        );
        assert_eq!(DanmakuFormat::from_name("srt"), None);
        assert_eq!(DanmakuFormat::Xml.extension(), "xml");
    }
}