Skip to main content

lazyanki_parser/
lib.rs

1use anyhow::Ok;
2use async_trait::async_trait;
3use lazyanki_common::{get_client, load_config};
4use regex::Regex;
5use scraper::{Html, Selector};
6use url::Url;
7
8#[async_trait]
9pub trait ParserStrategy {
10    async fn parse(&self) -> anyhow::Result<ParseResult>;
11}
12
13pub struct GermanStrategy {
14    pub url: String,
15    pub word: String,
16}
17
18pub struct EnglishStrategy {
19    pub url: String,
20    pub word: String,
21}
22
23pub struct ParseResult {
24    pub word: String,
25    pub translation: String,
26    pub table: String,
27}
28
29pub async fn get_strategy(word: String) -> anyhow::Result<Box<dyn ParserStrategy>> {
30    let config = load_config().expect("config not found");
31
32    let strategy: Box<dyn ParserStrategy> = match config.languages.target_lang_tag.as_str() {
33        "en-US" => Box::new(EnglishStrategy {
34            url: "https://verbformen.ru/".to_string(),
35            word: word.clone(),
36        }),
37        "de-DE" => Box::new(GermanStrategy {
38            url: "https://verbformen.ru/".to_string(),
39            word: word.clone(),
40        }),
41        _ => {
42            println!("Unsupported language");
43            return Err(anyhow::anyhow!("Unsupported language"));
44        }
45    };
46
47    Ok(strategy)
48}
49
50#[async_trait]
51impl ParserStrategy for EnglishStrategy {
52    async fn parse(&self) -> anyhow::Result<ParseResult> {
53        Ok(ParseResult {
54            word: self.word.clone(),
55            translation: String::from("not implemented"),
56            table: String::from("not implemented"),
57        })
58    }
59}
60
61#[async_trait]
62impl ParserStrategy for GermanStrategy {
63    async fn parse(&self) -> anyhow::Result<ParseResult> {
64        let mut url = Url::parse(&self.url)?;
65        let client = get_client().await?;
66        url.query_pairs_mut().append_pair("w", &self.word);
67
68        let resp = client.get(url).send().await?;
69        let html = resp.text().await?;
70
71        let document = Html::parse_document(&html);
72
73        let translation_selector = Selector::parse(r#"span[lang]"#).unwrap();
74
75        let table_selector = Selector::parse(".vTbl").unwrap();
76        let example_selector = Selector::parse("p.rInf.r1Zeile.rU3px.rO0px.rNt").unwrap();
77
78        let span = document.select(&translation_selector).next().unwrap();
79
80        let table_html = format!(
81            r#"<div style="display: flex; flex-wrap: wrap; gap: 18px; justify-content: center;">
82            {}
83            </div>"#,
84            document
85                .select(&table_selector)
86                .take(7)
87                .map(|el| {
88                    let html = el.html();
89                    let re = Regex::new(r"<span[^>]*>.*?</span>").unwrap();
90                    re.replace_all(&html, "").to_string()
91                })
92                .collect::<Vec<_>>()
93                .join("\n")
94        );
95
96        let example_text = document
97            .select(&example_selector)
98            .last()
99            .map(|el| el.text().collect::<String>())
100            .unwrap_or_default();
101        let text = span.text().collect::<String>();
102
103        Ok(ParseResult {
104            word: self.word.clone(),
105            translation: format!("{}<br><br>{}", text, example_text),
106            table: table_html,
107        })
108    }
109}