use finance_query_core::{FetchClient, IndexStream, YahooAuthManager, YahooFinanceClient};
use futures_util::StreamExt;
use std::sync::Arc;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fetch_client = Arc::new(FetchClient::new(None)?);
let cookie_jar = fetch_client.cookie_jar().clone();
let auth_manager = Arc::new(YahooAuthManager::new(None, cookie_jar));
let client = Arc::new(YahooFinanceClient::new(auth_manager.clone(), fetch_client));
auth_manager.refresh().await?;
println!("Starting real-time index stream...");
println!("Streaming S&P 500, Dow Jones, and NASDAQ every 5 seconds");
println!("Press Ctrl+C to stop\n");
let mut stream = IndexStream::us_major_indices(client.clone(), Duration::from_secs(5));
let mut count = 0;
while let Some(result) = stream.next().await {
match result {
Ok(indices) => {
count += 1;
println!("=== Update #{} ===", count);
for index in indices {
println!(
"{}: {:.2} ({}) {}",
index.name, index.value, index.change, index.percent_change
);
}
println!();
if count >= 10 {
println!("Demo complete - received 10 updates");
break;
}
}
Err(e) => {
eprintln!("Error fetching indices: {}", e);
}
}
}
Ok(())
}