From 19eee25f13d534557936ae4230d480fae27fbf5b Mon Sep 17 00:00:00 2001 From: R0flcopt3r <12752060+R0flcopt3r@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:07:01 +0200 Subject: why isn't this working --- src/hooks/url.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/src/hooks/url.rs b/src/hooks/url.rs index 252ac09..7b8a188 100644 --- a/src/hooks/url.rs +++ b/src/hooks/url.rs @@ -4,7 +4,8 @@ use irc::client::prelude::*; use regex::Regex; extern crate kuchiki; -use kuchiki::traits::*; +use kuchiki::{parse_html, traits::*}; +use reqwest::{get, Url}; pub const URL_REGEX: &str = r#"(https?://|www.)\S+"#; @@ -13,14 +14,14 @@ pub fn url_parser(msg: &str) -> Vec { url_regex .find_iter(msg) - .map(|mat| mat.as_str().to_string()) + .map(|u| u.as_str().to_string().replace("www.", "https://www.")) .collect::>() } pub async fn url_title(url: &str) -> Option { - let body = reqwest::get(url).await.ok()?.text().await.ok()?; + let body = get(Url::parse(url).ok()?).await.ok()?.text().await.ok()?; - let document = kuchiki::parse_html().one(body); + let document = parse_html().one(body); match document.select("title") { Ok(title) => Some(title.into_iter().nth(0)?.text_contents()), Err(_) => None, @@ -29,19 +30,33 @@ pub async fn url_title(url: &str) -> Option { pub fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> { if let Command::PRIVMSG(target, text) = msg.command.clone() { + let mut titles: Vec = Vec::new(); 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())?; + titles.push(title); } } + if !titles.is_empty() { + bot.send_privmsg(&target, &msg_builder(&titles))?; + } } Ok(()) } +pub fn msg_builder(titles: &Vec) -> String { + format!( + "Title{}: {}", + if titles.len() > 1 { "s" } else { "" }, + titles.join(" --- ") + ) +} + #[cfg(test)] mod tests { - use crate::hooks::url::url_parser; - use crate::hooks::url::url_title; + + use super::msg_builder; + use super::url_parser; + use super::url_title; #[test] fn test_url_titel() { @@ -49,14 +64,11 @@ mod tests { 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(); + let title: String = tokio_test::block_on(url_title("https://google.com")).unwrap(); assert_eq!(title.as_str(), "Google"); - let title: Option = - tokio_test::block_on(url_title("random_site")); + let title: Option = tokio_test::block_on(url_title("random_site")); assert_eq!(title, None) - } #[test] fn test_url_parser() { @@ -76,4 +88,32 @@ mod tests { ); 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 = 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 Some(title) = tokio_test::block_on(url_title(&url.as_str())) { + titles.push(title); + } + } + assert_eq!( + msg_builder(&titles), + "Titles: Hacker News --- Google --- YouTube" + ); + } } -- cgit v1.2.3