-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.rs
136 lines (118 loc) · 3.76 KB
/
page.rs
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
use std::io::Read;
use reqwest::Client;
use select::document::Document;
use select::predicate::*;
#[derive(Debug)]
pub struct DictDef {
pub word: String,
pub def: String,
pub example: Option<String>,
pub upvote: i32,
pub downvote: i32,
pub contributor: String,
pub date: String,
pub id: String,
pub sounds: Option<Vec<String>>,
pub similars: Option<Vec<String>>,
}
impl DictDef {}
fn urban_dict_url(word: &str) -> String {
let mut s = String::new();
s.push_str("https://www.urbandictionary.com/define.php?term=");
s.push_str(word);
s
}
fn json_list_to_strings(json: &str) -> Vec<String> {
json[1..json.len() - 1]
.split(", ")
.map(|s| s[1..s.len() - 1].to_owned())
.collect()
}
fn get_def_from_doc(doc: &Document) -> Option<DictDef> {
if let Some(panel) = doc.find(Class("def-panel")).into_selection().first() {
if let Some(word) = panel.find(Class("word")).into_selection().first() {
let word = word.text();
let def = panel
.find(Class("meaning"))
.into_selection()
.first()
.unwrap()
.text();
let defid = panel.attr("data-defid").unwrap_or("").to_owned();
let example = panel
.find(Class("example"))
.into_selection()
.first()
.and_then(|e| Some(e.text()));
let author = panel
.find(Class("contributor"))
.into_selection()
.first()
.and_then(|c| {
c.find(Name("a"))
.into_selection()
.first()
.and_then(|e| Some(e.text()))
})
.unwrap_or("".to_owned());
let date = panel
.find(Class("contributor"))
.into_selection()
.first()
.and_then(|e| e.last_child())
.and_then(|e| Some(e.text().trim().to_owned()))
.unwrap_or("".to_owned());
let sounds = panel
.find(Class("play-sound"))
.into_selection()
.first()
.and_then(|e| e.attr("data-urls").and_then(|s| Some(s.to_owned())))
.and_then(|l| Some(json_list_to_strings(&l)));
let similars = Some(
doc.find(Name("ul").and(Class("alphabetical")))
.into_selection()
.find(Name("li"))
.find(Name("a"))
.iter()
.map(|e| e.text())
.collect(),
);
Some(DictDef {
word: word.trim().to_owned(),
def: def.trim().to_owned(),
example: example,
upvote: 0,
downvote: 0,
contributor: author,
date: date,
id: defid,
sounds: sounds,
similars: similars,
})
} else {
None
}
} else {
None
}
}
fn find_from_url(url: &str) -> Option<DictDef> {
let client = Client::new();
let mut resp = client.get(url).send().unwrap();
let mut body = String::new();
{
resp.read_to_string(&mut body).unwrap();
}
let dom = Document::from(body.as_str());
get_def_from_doc(&dom)
}
pub fn find_word_of_the_day() -> Option<DictDef> {
find_from_url("https://www.urbandictionary.com/")
}
pub fn get_random_word() -> Option<DictDef> {
find_from_url("https://www.urbandictionary.com/random.php")
}
pub fn find_on_urban_dict(word: &str) -> Option<DictDef> {
let url = urban_dict_url(word);
find_from_url(&url)
}