use apify_client::ApifyClient;
use futures_util::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = std::env::var("APIFY_TOKEN").expect("set APIFY_TOKEN");
let client = ApifyClient::new(token);
let source_actor = "apify/hello-world";
let run = client
.actor(source_actor)
.start::<serde_json::Value>(None, Default::default())
.await?;
println!(
"Started run {} of {source_actor} — redirecting its log:",
run.id
);
let prefix = format!("{source_actor} -> ");
let mut stream = client.run(&run.id).log().stream().await?;
let mut buf = String::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
buf.push_str(&String::from_utf8_lossy(&chunk));
while let Some(newline) = buf.find('\n') {
let line: String = buf.drain(..=newline).collect();
print!("{prefix}{line}");
}
}
if !buf.is_empty() {
println!("{prefix}{buf}");
}
println!("--- log redirection ended (run finished) ---");
Ok(())
}