#[cfg(feature = "std")]
fn main() -> Result<clientele::SysexitsError, Box<dyn std::error::Error>> {
use asimov_brightdata_module::{
api::{BrightData, ScrapeInput, ScrapeRequest},
find_dataset_for, jq,
};
use asimov_module::getenv;
use clientele::SysexitsError::*;
use std::{io::stdout, str::FromStr};
clientele::dotenv().ok();
let args = clientele::args_os()?;
#[cfg(feature = "tracing")]
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_max_level(tracing_subscriber::filter::LevelFilter::WARN)
.init();
let urls: Vec<String> = args
.iter()
.skip(1)
.map(|arg| String::from_str(&arg.to_string_lossy()).unwrap())
.collect();
if urls.is_empty() {
return Ok(EX_OK);
}
let Some(api_key) = getenv::var_secret("BRIGHTDATA_API_KEY") else {
return Ok(EX_CONFIG); };
let api = BrightData::new(api_key);
for url in urls {
let Some(dataset) = find_dataset_for(&url) else {
return Ok(EX_UNAVAILABLE); };
let request = ScrapeRequest::from(vec![ScrapeInput::from_str(&url).unwrap()]);
let response = api.scrape_dataset(dataset.id, &request)?;
let response = jq::x_profile().filter_json_str(response)?;
if cfg!(feature = "pretty") {
colored_json::write_colored_json(&response, &mut stdout())?;
println!();
} else {
println!("{}", serde_json::to_string(&response).unwrap());
}
}
Ok(EX_OK)
}
#[cfg(not(feature = "std"))]
fn main() {
unimplemented!("asimov-brightdata-importer requires the 'std' feature")
}