1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! # daumdic
//!
//! Find word (Korean, English, Japanese, Chinese, ...) in Daum Dictionary
//! and returns its meaning and pronuciation.
//!
//! # Examples
//!
//! ```
//! # use futures::prelude::*;
//! # use tokio::runtime::current_thread::Runtime;
//! # fn main() {
//! # let mut rt = Runtime::new().unwrap();
//! let res_future = daumdic::search("독수리");
//! let res = &rt.block_on(res_future).unwrap().words[0];
//! assert_eq!(res.word, "독수리");
//! assert_eq!(res.lang, daumdic::Lang::Korean);
//! println!("{:?} {}", res.pronounce, res.meaning.join(", "));
//! # }
//! ```

pub mod errors;

use {
    crate::errors::Result,
    lazy_static::lazy_static,
    reqwest::Client,
    scraper::{Html, Selector},
};

/// Type of word language
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Lang {
    Korean,
    English,
    Japanese,
    Hanja,
    Other(String),
}

/// Result of `search` function
#[derive(Clone, Debug)]
pub struct Word {
    pub word: String,
    pub meaning: Vec<String>,
    pub pronounce: Option<String>,
    pub lang: Lang,
}

impl std::fmt::Display for Word {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if let Lang::Other(ref d) = self.lang {
            write!(f, "({})  ", d)?;
        }
        write!(f, "{}  ", self.word)?;
        if let Some(ref pronounce) = self.pronounce {
            write!(f, "{}  ", pronounce)?;
        }
        write!(f, "{}", self.meaning.join(", "))
    }
}

/// Output of `search` function.
#[derive(Debug, Clone)]
pub struct Search {
    pub words: Vec<Word>,
    pub alternatives: Vec<String>,
}

fn parse_document(document: &Html) -> Result<Search> {
    lazy_static! {
        static ref SELECTOR_CARD: Selector = Selector::parse(".card_word").unwrap();
        static ref SELECTOR_ITEM: Selector =
            Selector::parse(".cleanword_type,.search_type").unwrap();
        static ref SELECTOR_WORD: Selector =
            Selector::parse(".txt_cleansch,.txt_searchword,.txt_hanjaword").unwrap();
        static ref SELECTOR_LANG: Selector = Selector::parse(".tit_word").unwrap();
        static ref SELECTOR_PRONOUNCE: Selector =
            Selector::parse(".sub_read,.txt_pronounce").unwrap();
        static ref SELECTOR_MEANING: Selector = Selector::parse(".txt_search").unwrap();
        static ref SELECTOR_ALTERNATIVES: Selector = Selector::parse(".link_speller").unwrap();
    }

    let words = document
        .select(&SELECTOR_CARD)
        .filter_map(|card| {
            let lang = card
                .select(&SELECTOR_LANG)
                .map(|element| element.text().collect::<Vec<_>>().join(""))
                .map(|lang| {
                    if lang.starts_with("한국") {
                        Lang::Korean
                    } else if lang.starts_with("영") {
                        Lang::English
                    } else if lang.starts_with("일") {
                        Lang::Japanese
                    } else if lang.starts_with("한자") {
                        Lang::Hanja
                    } else {
                        Lang::Other(lang.to_string())
                    }
                })
                .next();

            card.select(&SELECTOR_ITEM)
                .map(|item| {
                    let word = item
                        .select(&SELECTOR_WORD)
                        .map(|element| element.text().collect::<Vec<_>>().join(""))
                        .next();
                    let pronounce = item
                        .select(&SELECTOR_PRONOUNCE)
                        .map(|element| element.text().collect::<Vec<_>>().join(""))
                        .next();
                    let meaning = item
                        .select(&SELECTOR_MEANING)
                        .map(|element| element.text().collect::<Vec<_>>().join(""))
                        .collect::<Vec<_>>();

                    (word, lang.clone(), pronounce, meaning)
                })
                .filter_map(|t| match t {
                    (Some(word), Some(lang), pronounce, meaning) => {
                        Some((word, lang, pronounce, meaning))
                    }
                    _ => None,
                })
                .map(|(word, lang, pronounce, meaning)| Word {
                    word,
                    lang,
                    pronounce,
                    meaning,
                })
                .next()
        })
        .collect();
    let alternatives = document
        .select(&SELECTOR_ALTERNATIVES)
        .map(|element| element.text().collect::<Vec<_>>().join(""))
        .collect();

    Ok(Search {
        words,
        alternatives,
    })
}

/// The main function.
///
/// # Example
///
/// ```
/// # use futures::prelude::*;
/// # use tokio::runtime::current_thread::Runtime;
/// # fn main() {
/// # let mut rt = Runtime::new().unwrap();
/// let res = rt.block_on(daumdic::search("zoo")).unwrap();
/// for word in res.words {
///     println!("{}", word);
/// }
/// # }
/// ```
///
/// # Errors
///
/// This function fails if:
///
/// - given word is empty
/// - GET request failed
pub async fn search(word: &str) -> Result<Search> {
    if word.is_empty() {
        Err(errors::DictionaryError::EmptyWord.into())
    } else {
        let client = Client::new();
        let url = format!("https://dic.daum.net/search.do?q={}", word);

        let resp = client.get(&url).send().await?;
        let body = resp.text().await?;
        let document = Html::parse_document(&body);
        parse_document(&document)
    }
}