use std::collections::HashMap;
use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ParseMode {
Accurate,
Speed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PageComplexity {
Low,
High,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum DetailLevel {
Low,
Medium,
High,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ScrapeRequest {
pub website_url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub clean: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parse_mode: Option<ParseMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tag_truncate: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extract_links: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_age: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stealth: Option<bool>,
}
impl ScrapeRequest {
pub fn new(website_url: impl Into<String>) -> Self {
Self {
website_url: website_url.into(),
..Default::default()
}
}
pub fn clean(mut self, clean: bool) -> Self {
self.clean = Some(clean);
self
}
pub fn parse_mode(mut self, mode: ParseMode) -> Self {
self.parse_mode = Some(mode);
self
}
pub fn tag_truncate(mut self, tag_truncate: bool) -> Self {
self.tag_truncate = Some(tag_truncate);
self
}
pub fn extract_links(mut self, extract_links: bool) -> Self {
self.extract_links = Some(extract_links);
self
}
pub fn include_tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.include_tags = Some(tags.into_iter().map(Into::into).collect());
self
}
pub fn exclude_tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.exclude_tags = Some(tags.into_iter().map(Into::into).collect());
self
}
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
self.headers = Some(headers);
self
}
pub fn max_age(mut self, seconds: u64) -> Self {
self.max_age = Some(seconds);
self
}
pub fn stealth(mut self, stealth: bool) -> Self {
self.stealth = Some(stealth);
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SmartScraperRequest {
pub website_url: String,
pub user_prompt: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub output_schema: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_complexity: Option<PageComplexity>,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail_level: Option<DetailLevel>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parse_mode: Option<ParseMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub plain_text: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reduce_content: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub experimental: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_age: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stealth: Option<bool>,
}
impl SmartScraperRequest {
pub fn new(website_url: impl Into<String>, user_prompt: impl Into<String>) -> Self {
Self {
website_url: website_url.into(),
user_prompt: user_prompt.into(),
..Default::default()
}
}
pub fn output_schema(mut self, schema: Value) -> Self {
self.output_schema = Some(schema);
self
}
pub fn page_complexity(mut self, complexity: PageComplexity) -> Self {
self.page_complexity = Some(complexity);
self
}
pub fn detail_level(mut self, level: DetailLevel) -> Self {
self.detail_level = Some(level);
self
}
pub fn parse_mode(mut self, mode: ParseMode) -> Self {
self.parse_mode = Some(mode);
self
}
pub fn plain_text(mut self, plain_text: bool) -> Self {
self.plain_text = Some(plain_text);
self
}
pub fn include_tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.include_tags = Some(tags.into_iter().map(Into::into).collect());
self
}
pub fn exclude_tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.exclude_tags = Some(tags.into_iter().map(Into::into).collect());
self
}
pub fn reduce_content(mut self, reduce_content: bool) -> Self {
self.reduce_content = Some(reduce_content);
self
}
pub fn experimental(mut self, experimental: bool) -> Self {
self.experimental = Some(experimental);
self
}
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
self.headers = Some(headers);
self
}
pub fn max_age(mut self, seconds: u64) -> Self {
self.max_age = Some(seconds);
self
}
pub fn stealth(mut self, stealth: bool) -> Self {
self.stealth = Some(stealth);
self
}
}