use async_trait::async_trait;
use mocra::prelude::*;
use serde::Serialize;
#[derive(Debug, Serialize)]
struct Page {
url: String,
status: u16,
bytes: usize,
}
struct Httpbin;
#[async_trait]
impl Spider for Httpbin {
type Item = Page;
fn name(&self) -> &str {
"httpbin"
}
async fn start(&self, s: &mut Seeds) {
s.get("https://httpbin.org/get");
s.get("https://httpbin.org/uuid");
}
async fn parse(&self, res: Response, cx: &mut Ctx<Self::Item>) -> Result<()> {
cx.emit(Page {
url: res.module_id(),
status: res.status_code,
bytes: res.text().map(|t| t.len()).unwrap_or(0),
});
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<()> {
Mocra::builder()
.spider(
Httpbin,
on_item(|page: Page| async move {
println!(
"[item] {} -> {} ({} bytes)",
page.url, page.status, page.bytes
);
}),
)
.run()
.await
}