aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/url.rs
blob: 78479693992e798f9f01de96f25bb377ddaa3c95 (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
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
use anyhow::{bail, Context, Error, Result};
use irc::client::prelude::*;

use regex::Regex;

extern crate kuchiki;
use kuchiki::{parse_html, traits::*};
use reqwest::{get, Url};
use tracing::trace;

pub const URL_REGEX: &str = r#"(https?://|www.)\S+"#;

#[tracing::instrument]
pub fn url_parser(msg: &str) -> Vec<String> {
    let url_regex = Regex::new(URL_REGEX).unwrap();

    url_regex
        .find_iter(msg)
        .map(|u| u.as_str().to_string().replace("www.", "https://"))
        .collect::<Vec<String>>()
}

#[tracing::instrument]
pub async fn url_title(url: &str) -> Result<String, Error> {
    let body = get(Url::parse(url).context("Failed to parse url")?)
        .await
        .context("Failed to make request")?
        .text()
        .await
        .context("failed to get request response text")?;

    let document = parse_html().one(body);
    match document.select("title") {
        Ok(title) => Ok(title
            .into_iter()
            .nth(0)
            .context("title did not have text")?
            .text_contents()),
        Err(_) => bail!("could not find title"),
    }
}

#[tracing::instrument(skip(bot))]
pub async fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> {
    if let Command::PRIVMSG(target, text) = msg.command.clone() {
        let mut futures: Vec<tokio::task::JoinHandle<_>> = Vec::new();

        for url in url_parser(&text) {
            futures.push(tokio::spawn(async move {
                trace!("got url: {:?}", url);
                match url_title(&url.as_str()).await {
                    Ok(title) => {
                        trace!("extracted title from url: {:?}, {:?}", title, url);
                        Ok(title)
                    }
                    Err(err) => bail!("Failed to get urls title: {:?}", err),
                }
            }))
        }

        let titles = futures::future::join_all(futures).await;

        let titles: Vec<String> = titles
            .into_iter()
            .filter_map(|x| x.ok())
            .filter_map(|x| x.ok())
            .collect();

        if !titles.is_empty() {
            bot.send_privmsg(&target, &msg_builder(&titles))?;
        }
    }
    Ok(())
}

#[tracing::instrument]
pub fn msg_builder(titles: &Vec<String>) -> String {
    format!(
        "Title{}: {}",
        if titles.len() > 1 { "s" } else { "" },
        titles.join(" --- ")
    )
}

#[cfg(test)]
mod tests {

    use super::msg_builder;
    use super::url_parser;
    use super::url_title;
    use anyhow::{Error, Result};

    #[test]
    fn test_url_titel() {
        let title: String =
            tokio_test::block_on(url_title("https://news.ycombinator.com")).unwrap();
        assert_eq!(title.as_str(), "Hacker News");

        let title: String = tokio_test::block_on(url_title("https://google.com")).unwrap();
        assert_eq!(title.as_str(), "Google");

        assert!(tokio_test::block_on(url_title("random_site")).is_err())
    }
    #[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);
    }

    #[test]
    fn test_msg_builder() {
        let msg = msg_builder(&Vec::from(["hello".to_string(), "world".to_string()]));
        assert_eq!("Titles: hello --- world", msg);

        let msg = msg_builder(&Vec::from(["hello".to_string()]));
        assert_eq!("Title: hello", msg);
    }

    #[test]
    fn test_all() {
        let mut titles: Vec<String> = Vec::new();
        let text = "https://news.ycombinator.com www.google.com https://youtube.com";
        let urls = url_parser(&text);

        assert_eq!(urls.len(), 3);

        for url in &urls {
            if let Ok(title) = tokio_test::block_on(url_title(&url.as_str())) {
                titles.push(title);
            }
        }
        assert_eq!(
            msg_builder(&titles),
            "Titles: Hacker News --- Google --- YouTube"
        );
    }
}