Skip to main content

shinden_pl_api/
player.rs

1use crate::{client::ShindenAPI, models::Player};
2use anyhow::Result;
3use scraper::{Html, Selector};
4use serde_json::Value;
5
6impl ShindenAPI {
7    pub async fn get_players(&self, episode_link: &str) -> Result<Vec<Player>> {
8        let html = self.get_html(episode_link).await?;
9        let doc = Html::parse_document(&html);
10        let buttons_selector = Selector::parse(".ep-buttons a").unwrap();
11
12        let mut result = Vec::new();
13        for el in doc.select(&buttons_selector) {
14            if let Some(data) = el.value().attr("data-episode") {
15                if let Ok(json) = serde_json::from_str::<Value>(data) {
16                    let player = Player {
17                        player: json["player"].as_str().unwrap_or("").to_string(),
18                        max_res: json["max_res"].as_str().unwrap_or("").to_string(),
19                        lang_audio: json["lang_audio"].as_str().unwrap_or("").to_string(),
20                        lang_subs: json["lang_subs"].as_str().unwrap_or("").to_string(),
21                        online_id: json["online_id"].as_str().unwrap_or("").to_string(),
22                    };
23                    result.push(player);
24                }
25            }
26        }
27
28        Ok(result)
29    }
30}
31