speedboat/src/common/net.rs
2024-05-02 16:30:16 -05:00

62 lines
1.4 KiB
Rust

use reqwest::{redirect::Policy, Client};
use select::{document::Document, predicate::Name};
use std::time::Duration;
use super::console::parsehit;
pub fn mkclient(redir: bool) -> Result<Client, reqwest::Error> {
let rpolicy: Policy = if redir {
Policy::limited(5)
} else {
Policy::none()
};
Client::builder()
.user_agent("buttplug/1.0")
.redirect(rpolicy)
.timeout(Duration::from_secs(2))
.connect_timeout(Duration::from_millis(500))
.build()
}
pub async fn query(
c: Client,
url: &str,
codes: Vec<u16>,
exclude: bool,
) -> Result<(), reqwest::Error> {
let response = c.get(format!("http://{url}/")).send().await?;
let statcode = response.status().as_u16();
if codes.len() > 0 {
if codes.contains(&statcode) {
if exclude {
return Ok(());
}
} else if !exclude {
return Ok(());
}
}
let sc = response.status().as_u16();
let url: String = response.url().to_string();
let body = response.text().await?;
// Parse the HTML document
let document = Document::from(body.as_str());
// Use select to find the <title> tag
let title = document
.find(Name("title"))
.next()
.map(|n| n.text())
.unwrap_or_else(|| "".to_string());
println!(
"{}",
parsehit(sc, url, title.trim_matches(['\n', '\t', '\r']))
);
Ok(())
}