From 7b44a227be477fc455300e5ffda1deb07f224110 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Sun, 20 Feb 2022 11:17:52 -0600 Subject: [PATCH] Remove cache and send HEAD before parsing title --- src/main.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1dd44dc..053ef80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use futures::prelude::*; use htmlescape::decode_html; use irc::client::prelude::*; @@ -83,7 +81,7 @@ async fn main() -> anyhow::Result<()> { let title_regex = Regex::new(r"(?<=)(.*)(?=)").unwrap(); let spotify_regex = Regex::new(r"(?:https?|spotify):(?://open\.spotify\.com/)?(track|artist|album|playlist)[/:]([a-zA-Z0-9]*)").unwrap(); - let mut cache: HashMap = HashMap::new(); + let hclient = reqwest::Client::new(); while let Some(message) = stream.next().await.transpose()? { if vlog { print!("[IRC] {}", message) } @@ -101,18 +99,32 @@ async fn main() -> anyhow::Result<()> { } } else if let Some(m) = url_regex.find(&message) { let url = &message[m.0..m.1]; - if let Some(entry) = cache.get(&url.to_string()) { - client.send_privmsg(&channel, format!("\x039[Title]\x0311 {}", entry))?; - continue; + match hclient.head(url).send().await { + Ok(o) => { + let headers = o.headers(); + let ctype = match headers.get("Content-Type") { + Some(c) => match c.to_str() { + Ok(s) => s, + Err(_) => continue, + }, + None => continue, + }; + if !ctype.starts_with("text/html") { + continue; + } + }, + Err(e) => { + println!("[Title] Error! {}", e); + continue; + } } - match reqwest::get(url).await { + match hclient.get(url).send().await { Ok(o) => { let body = o.text().await?; if let Some(tm) = title_regex.find(&body) { let title_match = &body[tm.0..tm.1]; let result = decode_html(title_match).unwrap_or_else(|_| title_match.to_string()); client.send_privmsg(&channel, format!("\x039[Title]\x0311 {}", result))?; - cache.insert(url.to_string(), result.to_string()); } } Err(e) => println!("[Title] Error! {}", e)