diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-12-13 18:37:53 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-12-13 18:37:53 +0100 |
| commit | d0a417cd9569d135a1dab0d49fe2f5dfbc65bad3 (patch) | |
| tree | 27a136bc4a06f0e928b7bdfbee258c22699a7262 /src/plugins/url.rs | |
| parent | 709a293da674c5682bada5bd2b8fad0967e8080c (diff) | |
| download | frippy-d0a417cd9569d135a1dab0d49fe2f5dfbc65bad3.tar.gz frippy-d0a417cd9569d135a1dab0d49fe2f5dfbc65bad3.zip | |
Trim url titles and move downloading into a function
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index f1f6828..f6e06f3 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -43,16 +43,9 @@ impl Url { } } - fn url(&self, server: &IrcServer, message: &str, target: &str) -> Result<(), IrcError> { - let url = match self.grep_url(message) { - Some(url) => url, - None => { - return Ok(()); - } - }; - + fn download(&self, url: &str) -> Option<String> { let response = Client::new() - .get(&url) + .get(url) .header(Connection::close()) .send(); @@ -70,16 +63,16 @@ impl Url { Ok(len) => len, Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, Err(e) => { - debug!("Download from \"{}\" failed: {}", url, e); - return Ok(()) + debug!("Download from {:?} failed: {}", url, e); + return None; } }; let slice = match str::from_utf8(&buf[..len]) { Ok(slice) => slice, Err(e) => { - debug!("Failed to read bytes from \"{}\" as UTF8: {}", url, e); - return Ok(()); + debug!("Failed to read bytes from {:?} as UTF8: {}", url, e); + return None; } }; @@ -88,26 +81,46 @@ impl Url { // Check if the file is too large to download if written > self.max_kib * 1024 { - debug!("Stopping download - File from \"{}\" is larger than {} KiB", url, self.max_kib); - return Ok(()); + debug!("Stopping download - File from {:?} is larger than {} KiB", url, self.max_kib); + return None; } + } + Some(body) // once told me + } + Err(e) => { + debug!("Bad response from {:?}: ({})", url, e); + return None; + } + } + } + + fn url(&self, server: &IrcServer, message: &str, target: &str) -> Result<(), IrcError> { + let url = match self.grep_url(message) { + Some(url) => url, + None => { + return Ok(()); + } + }; + + + match self.download(&url) { + Some(body) => { let doc = Document::from(body.as_ref()); if let Some(title) = doc.find(Name("title")).next() { let text = title.children().next().unwrap(); + let message = text.as_text().unwrap().trim().replace("\n", "|"); debug!("Title: {:?}", text); + debug!("Message: {:?}", message); - server.send_privmsg(target, text.as_text().unwrap()) + server.send_privmsg(target, &message) } else { Ok(()) } } - Err(e) => { - debug!("Bad response from \"{}\": ({})", url, e); - Ok(()) - } + None => Ok(()), } } } |
