blob: d5836d01a7c7e168bdd8bb5c3c22f0f781034b01 (
plain)
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
|
use anyhow::Result;
use irc::client::prelude::*;
use macros::privmsg;
use std::option::Option;
use std::str::FromStr;
use std::string::String;
use regex::Regex;
pub const URL_REGEX: &str = r#"(https?://|www.)\S+"#;
pub fn url_parser(msg: &str) -> Vec<String> {
let url_regex = Regex::new(URL_REGEX).unwrap();
url_regex
.find_iter(msg)
.map(|mat| mat.as_str().to_string())
.collect::<Vec<String>>()
}
pub async fn url_title(url: &str) -> Option<String> {
let body = reqwest::get(url).await.unwrap().text().await.unwrap();
let parsed_body = html_parser::Dom::parse(&body).unwrap().to_json();
return Some("Hacker News".to_string());
}
pub fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> {
if let Command::PRIVMSG(target, text) = msg.command.clone() {
for url in url_parser(&text) {
if let Some(title) = futures::executor::block_on(url_title(&url.as_str())){
bot.send_privmsg(&target, title.as_str())?;
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::hooks::url::url_parser;
use crate::hooks::url::url_title;
#[test]
fn test_url_titel() {
let title = tokio_test::block_on(
url_title("https://news.ycombinator.com/")
);
assert_eq!(title.unwrap().as_str(), "Hackerr News");
}
#[test]
fn test_url_parser() {
let url = url_parser("some message https://news.ycombinator.com/ here");
assert_eq!(url[0], "https://news.ycombinator.com/");
let url = url_parser("no url here!");
assert!(url.is_empty());
let url = url_parser(
&[
"https://new.ycombinator.com/ ",
"http://news.ycombinator.com/ ",
"www.google.com",
]
.concat(),
);
assert_eq!(url.len(), 3);
}
}
|