diff options
| author | R0flcopt3r <eiriklavik@gmail.com> | 2021-10-05 21:44:33 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-10 16:21:55 +0200 |
| commit | 6e330c81af0905182e14b1fd5358c8e31dcd2c3d (patch) | |
| tree | a4c40933c29db8211468047dab856885d1c9a4fc /src | |
| parent | use merge request pipelines (diff) | |
for mr maintainer boi
Diffstat (limited to 'src')
| -rw-r--r-- | src/hooks/mod.rs | 1 | ||||
| -rw-r--r-- | src/hooks/url.rs | 71 | ||||
| -rw-r--r-- | src/main.rs | 6 |
3 files changed, 78 insertions, 0 deletions
diff --git a/src/hooks/mod.rs b/src/hooks/mod.rs index c5d6e1d..924fe2e 100644 --- a/src/hooks/mod.rs +++ b/src/hooks/mod.rs @@ -7,6 +7,7 @@ pub mod intensify; pub mod pet; pub mod sed; pub mod shifty_eyes; +pub mod url; pub use intensify::intensify; pub use shifty_eyes::shifty_eyes; diff --git a/src/hooks/url.rs b/src/hooks/url.rs new file mode 100644 index 0000000..d5836d0 --- /dev/null +++ b/src/hooks/url.rs @@ -0,0 +1,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); + } +} diff --git a/src/main.rs b/src/main.rs index 9ab7f6c..12881e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,12 @@ async fn main() { PRIVMSG, sed.log ), + hook( + "url_preview", + "Send preview of website", + PRIVMSG, + catinator::hooks::url::url_preview + ) matcher( "shifty_eyes", ">.>", |
