From 2208ada6677b8aa4a5651fb21021049c1e59a1d4 Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 8 Mar 2018 16:53:11 +0100 Subject: Use a naive search for html titles to speed it up --- src/plugins/url.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/plugins/url.rs') diff --git a/src/plugins/url.rs b/src/plugins/url.rs index fa4c6f4..4c33cba 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -1,13 +1,9 @@ extern crate regex; -extern crate select; use irc::client::prelude::*; use self::regex::Regex; -use self::select::document::Document; -use self::select::predicate::Name; - use plugin::*; use utils; @@ -39,22 +35,23 @@ impl Url { Some(captures.get(2)?.as_str().to_owned()) } - fn get_title(&self, body: &str) -> Option { - let doc = Document::from(body.as_ref()); - let title = doc.find(Name("title")).next()?; - let title = title.children().next()?; - let title_text = title.as_text()?.trim().replace("\n", "|"); + fn get_title<'a>(&self, body: &'a str) -> Option<&'a str> { + let title = body.find("") + .map(|start| body.find("").map(|end| &body[start + 7..end])) + .and_then(|s| s); + debug!("Title: {:?}", title); - debug!("Text: {:?}", title_text); - Some(title_text) + title } fn url(&self, text: &str) -> Result { let url = self.grep_url(text).ok_or(ErrorKind::MissingUrl)?; let body = utils::download(&url, Some(self.max_kib)).context(ErrorKind::Download)?; - Ok(self.get_title(&body).ok_or(ErrorKind::MissingTitle)?) + let title = self.get_title(&body).ok_or(ErrorKind::MissingTitle)?; + + Ok(title.to_owned()) } } -- cgit v1.2.3-70-g09d2