make titlebot default the mime to text/html
This commit is contained in:
parent
4a19c2ce5a
commit
c7ce19275a
|
@ -2,6 +2,7 @@ use crate::bot::{Context, Trigger};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use fancy_regex::{Captures, Regex};
|
use fancy_regex::{Captures, Regex};
|
||||||
use htmlescape::decode_html;
|
use htmlescape::decode_html;
|
||||||
|
use hyper::header::HeaderValue;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
pub struct Title {
|
pub struct Title {
|
||||||
|
@ -31,25 +32,25 @@ impl Trigger for Title {
|
||||||
let request = self.http.get(url).build()?;
|
let request = self.http.get(url).build()?;
|
||||||
let response = self.http.execute(request).await?;
|
let response = self.http.execute(request).await?;
|
||||||
let headers = response.headers();
|
let headers = response.headers();
|
||||||
return if let Some(header) = headers.get("Content-Type") {
|
|
||||||
let mime = header.to_str()?;
|
let header = headers.get("Content-Type");
|
||||||
if mime.contains("text/html") {
|
let mime = header
|
||||||
let body = response.text().await?;
|
.map(HeaderValue::to_str)
|
||||||
if let Some(tm) = self.title_regex.find(&body)? {
|
.transpose()?
|
||||||
let title_match = &body[tm.start()..tm.end()];
|
.unwrap_or("text/html");
|
||||||
let result =
|
if mime.contains("text/html") {
|
||||||
decode_html(title_match).unwrap_or_else(|_| title_match.to_string());
|
let body = response.text().await?;
|
||||||
Ok(format!("\x039[Title]\x0311 {}", result))
|
if let Some(tm) = self.title_regex.find(&body)? {
|
||||||
} else {
|
let title_match = tm.as_str();
|
||||||
Ok("\x039[Title]\x0311 No title".into())
|
let result = decode_html(title_match).unwrap_or_else(|_| title_match.to_string());
|
||||||
}
|
Ok(format!("\x039[Title]\x0311 {}", result))
|
||||||
} else {
|
} else {
|
||||||
let content_length = response.content_length().map(|l| (l / 1024).to_string());
|
Ok("\x039[Title]\x0311 No title".into())
|
||||||
let size = content_length.as_deref().unwrap_or("unknown");
|
|
||||||
Ok(format!("\x039[Title]\x0311 File: {}; {}kb", mime, size))
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok("\x039[Title]\x0311 No Content-Type header".into())
|
let content_length = response.content_length().map(|l| (l / 1024).to_string());
|
||||||
};
|
let size = content_length.as_deref().unwrap_or("unknown");
|
||||||
|
Ok(format!("\x039[Title]\x0311 File: {}; {}kb", mime, size))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue