forked from lemon/uberbot
Upstream sync
This commit is contained in:
commit
c6f8b13b38
|
@ -56,6 +56,10 @@ history_depth = 5
|
|||
# optional, default: uberbot.db3
|
||||
#db_path = "database.db3"
|
||||
|
||||
# User agent used for title requests. Important for sites like crates.io which require
|
||||
# a user agent to be set. Default: uberbot $VERSION (reqwest)
|
||||
#user_agent = "amazingbot"
|
||||
|
||||
# Allows disabling specific triggers in certain channels.
|
||||
# You can also disable a trigger in all channels by using '*'.
|
||||
# Currently available triggers are: sed, title, spotify
|
||||
|
|
|
@ -8,10 +8,11 @@ use reqwest::{Client, ClientBuilder};
|
|||
pub struct Title {
|
||||
http: Client,
|
||||
title_regex: Regex,
|
||||
user_agent: String,
|
||||
}
|
||||
|
||||
impl Title {
|
||||
pub fn new(skip_cert_validation: Option<bool>) -> anyhow::Result<Self> {
|
||||
pub fn new(user_agent: Option<String>, skip_cert_validation: Option<bool>) -> anyhow::Result<Self> {
|
||||
let http = if let Some(skip_cert_validation) = skip_cert_validation {
|
||||
ClientBuilder::new()
|
||||
.danger_accept_invalid_certs(skip_cert_validation)
|
||||
|
@ -22,6 +23,9 @@ impl Title {
|
|||
Ok(Title {
|
||||
http,
|
||||
title_regex: Regex::new(r"<title[^>]*>(.*?)</title>")?,
|
||||
user_agent: user_agent.unwrap_or_else(|| {
|
||||
format!("uberbot {} (reqwest)", env!("CARGO_PKG_VERSION")).to_string()
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +36,12 @@ impl Trigger for Title {
|
|||
let url = ctx.captures.get(0).unwrap();
|
||||
tracing::debug!("url: {}", url);
|
||||
|
||||
let request = self.http.get(url).build()?;
|
||||
let request = self
|
||||
.http
|
||||
.get(url)
|
||||
.header("User-Agent", &self.user_agent)
|
||||
.header("Accept", "text/html, */*")
|
||||
.build()?;
|
||||
let response = self.http.execute(request).await?;
|
||||
let headers = response.headers();
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ pub struct BotConfig {
|
|||
pub search_limit: Option<usize>,
|
||||
pub prefixes: Vec<String>,
|
||||
pub ignored_triggers: Option<HashMap<String, Vec<String>>>,
|
||||
pub user_agent: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
|
@ -212,7 +212,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
bot.add_trigger(
|
||||
"title".into(),
|
||||
Regex::new(r"https?://[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*")?,
|
||||
Title::new(cfg.skip_cert_validation)?
|
||||
Title::new(cfg.bot.user_agent, cfg.skip_cert_validation)?
|
||||
);
|
||||
#[cfg(feature = "debug")]
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue