use anyhow::{Result, bail};
use std::str::FromStr;
use url::Url;
pub struct Scrape(pub Url);
impl Scrape {
pub fn new(announce_url: &str, id20: Option<&[[u8; 20]]>) -> Result<Self> {
if id20.is_some_and(|b| b.is_empty()) {
bail!(
"info-hash value should not be empty; tip: use None to build the Full scrape query"
)
}
let query = id20.map(|value| {
value
.iter()
.map(|b| format!("info_hash={}", super::url_encode_bytes(b)))
.collect::<Vec<String>>()
.join("&")
});
let mut url = Url::from_str(announce_url)?;
url.set_path("scrape");
url.set_query(query.as_deref());
Ok(Self(url))
}
}
impl std::fmt::Display for Scrape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}