diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-03-08 16:53:11 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-03-08 16:56:29 +0100 |
| commit | 2208ada6677b8aa4a5651fb21021049c1e59a1d4 (patch) | |
| tree | cf93bc68e96a7ca5ee68fd09f61275861eac18d8 /src/plugins/url.rs | |
| parent | 1bb6e307f1011456b28bb6eb27abc80e71ef187d (diff) | |
| download | frippy-2208ada6677b8aa4a5651fb21021049c1e59a1d4.tar.gz frippy-2208ada6677b8aa4a5651fb21021049c1e59a1d4.zip | |
Use a naive search for html titles to speed it up
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 21 |
1 files changed, 9 insertions, 12 deletions
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<String> { - 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("<title>") + .map(|start| body.find("</title>").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<String, UrlError> { 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()) } } |
