diff options
| author | Max Audron <audron@cocaine.farm> | 2021-10-09 16:39:49 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-10 16:21:55 +0200 |
| commit | 1388f85af78bb07c417f0b8cc7f2c3eb8327b1ef (patch) | |
| tree | e97051e9ae41eef9bfc8813b664eab314b6fbcb0 | |
| parent | strip www. (diff) | |
add tracing and error context to url preview
| -rw-r--r-- | src/hooks/url.rs | 37 | ||||
| -rw-r--r-- | src/main.rs | 7 |
2 files changed, 30 insertions, 14 deletions
diff --git a/src/hooks/url.rs b/src/hooks/url.rs index 68caea2..2392d1d 100644 --- a/src/hooks/url.rs +++ b/src/hooks/url.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{bail, Context, Error, Result}; use irc::client::prelude::*; use regex::Regex; @@ -6,9 +6,11 @@ use regex::Regex; extern crate kuchiki; use kuchiki::{parse_html, traits::*}; use reqwest::{get, Url}; +use tracing::{error, 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(); @@ -18,24 +20,42 @@ pub fn url_parser(msg: &str) -> Vec<String> { .collect::<Vec<String>>() } -pub async fn url_title(url: &str) -> Option<String> { - let body = get(Url::parse(url).ok()?).await.ok()?.text().await.ok()?; +#[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) => Some(title.into_iter().nth(0)?.text_contents()), - Err(_) => None, + 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 fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> { if let Command::PRIVMSG(target, text) = msg.command.clone() { let mut titles: Vec<String> = Vec::new(); + for url in url_parser(&text) { - if let Some(title) = futures::executor::block_on(url_title(&url.as_str())) { - titles.push(title); + trace!("got url: {:?}", url); + match futures::executor::block_on(url_title(&url.as_str())) { + Ok(title) => { + trace!("extracted title from url: {:?}, {:?}", title, url); + titles.push(title); + }, + Err(err) => error!("Failed to get urls title: {:?}", err), } } + if !titles.is_empty() { bot.send_privmsg(&target, &msg_builder(&titles))?; } @@ -43,12 +63,13 @@ pub fn url_preview(bot: &crate::Bot, msg: Message) -> Result<()> { Ok(()) } +#[tracing::instrument] pub fn msg_builder(titles: &Vec<String>) -> String { format!( "Title{}: {}", if titles.len() > 1 { "s" } else { "" }, titles.join(" --- ") - ) + ) } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index a9faa78..776299d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,7 @@ async fn main() { use catinator::catinator; - tracing_subscriber::fmt() - .compact() - .with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL) - .with_max_level(tracing::Level::DEBUG) - .with_thread_ids(true) - .init(); + tracing_subscriber::fmt::init(); let mut sed = catinator::hooks::sed::Sed::new(); |
