Skip to main content

shinden_pl_api/
episodes.rs

1use crate::{client::ShindenAPI, models::Episode};
2use anyhow::{Context, Result};
3// Potrzebne do .context()
4use scraper::{Html, Selector};
5
6impl ShindenAPI {
7    pub async fn get_episodes(&self, link: &str) -> Result<Vec<Episode>> {
8        let url = format!("{}/all-episodes", link);
9
10        let html = self.get_html(&url).await?;
11        let doc = Html::parse_document(&html);
12
13        let tbody_selector = Selector::parse("tbody.list-episode-checkboxes").unwrap();
14        let tbody_element = doc.select(&tbody_selector)
15            .next()
16            .context("Could not find tbody element on the page")?;
17
18        let title_selector = Selector::parse(".ep-title").unwrap();
19        let button_selector = Selector::parse("a.button.active").unwrap();
20
21        let mut episodes = Vec::new();
22
23        for el in tbody_element.select(&title_selector) {
24            episodes.push(Episode {
25                title: el.text().collect::<String>(),
26                link: String::new(),
27            });
28        }
29
30        for (i, el) in tbody_element.select(&button_selector).enumerate() {
31            if let Some(href) = el.value().attr("href") {
32                if let Some(ep) = episodes.get_mut(i) {
33                    ep.link = format!("https://shinden.pl{}", href);
34                }
35            }
36        }
37
38        episodes.reverse();
39        Ok(episodes)
40    }
41}