From f04903065051e82983040107683527de0c5e6d48 Mon Sep 17 00:00:00 2001 From: R0flcopt3r <12752060+R0flcopt3r@users.noreply.github.com> Date: Sat, 9 Oct 2021 14:14:46 +0200 Subject: finished implementing everything --- src/hooks/url.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/hooks/url.rs b/src/hooks/url.rs index d5836d0..252ac09 100644 --- a/src/hooks/url.rs +++ b/src/hooks/url.rs @@ -1,12 +1,11 @@ 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; +extern crate kuchiki; +use kuchiki::traits::*; + pub const URL_REGEX: &str = r#"(https?://|www.)\S+"#; pub fn url_parser(msg: &str) -> Vec { @@ -19,18 +18,19 @@ pub fn url_parser(msg: &str) -> Vec { } pub async fn url_title(url: &str) -> Option { - let body = reqwest::get(url).await.unwrap().text().await.unwrap(); - - let parsed_body = html_parser::Dom::parse(&body).unwrap().to_json(); + let body = reqwest::get(url).await.ok()?.text().await.ok()?; - - return Some("Hacker News".to_string()); + let document = kuchiki::parse_html().one(body); + match document.select("title") { + Ok(title) => Some(title.into_iter().nth(0)?.text_contents()), + Err(_) => None, + } } 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())){ + if let Some(title) = futures::executor::block_on(url_title(&url.as_str())) { bot.send_privmsg(&target, title.as_str())?; } } @@ -45,10 +45,18 @@ mod tests { #[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"); + 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"); + + let title: Option = + tokio_test::block_on(url_title("random_site")); + assert_eq!(title, None) + } #[test] fn test_url_parser() { -- cgit v1.2.3