added corporate quarterly announcments for the last 4 years

This commit is contained in:
2025-11-23 16:02:23 +01:00
parent cd3f47d91f
commit 462f7ca672
12 changed files with 1457 additions and 104 deletions

View File

@@ -1,31 +1,83 @@
// src/corporate/update.rs
use super::{scraper::*, storage::*, types::*};
use super::{scraper::*, storage::*, helpers::*, types::*};
use crate::config::Config;
use chrono::Local;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
pub async fn run_full_update(client: &fantoccini::Client, tickers: Vec<String>, config: &Config) -> anyhow::Result<()> {
println!("Updating {} tickers (prices from {})", tickers.len(), config.corporate_start_date);
let today = chrono::Local::now().format("%Y-%m-%d").to_string();
for ticker in tickers {
let mut existing = load_existing_events().await?;
for ticker in &tickers {
print!("{:6} ", ticker);
// Earnings
if let Ok(events) = fetch_earnings_history(client, &ticker).await {
merge_and_save_events(&ticker, events.clone()).await?;
println!("{} earnings", events.len());
if let Ok(new_events) = fetch_earnings_history(client, ticker).await {
let result = process_batch(&new_events, &mut existing, &today);
save_changes(&result.changes).await?;
println!("{} earnings, {} changes", new_events.len(), result.changes.len());
}
// Prices now using config.corporate_start_date
if let Ok(prices) = fetch_price_history(client, &ticker, &config.corporate_start_date, &today).await {
save_prices_for_ticker(&ticker, prices).await?;
if let Ok(prices) = fetch_price_history(ticker, &config.corporate_start_date, &today).await {
save_prices_for_ticker(ticker, prices).await?;
}
tokio::time::sleep(tokio::time::Duration::from_millis(250)).await;
}
save_optimized_events(existing).await?;
Ok(())
}
pub struct ProcessResult {
pub changes: Vec<CompanyEventChange>,
}
pub fn process_batch(
new_events: &[CompanyEvent],
existing: &mut HashMap<String, CompanyEvent>,
today: &str,
) -> ProcessResult {
let mut changes = Vec::new();
for new in new_events {
let key = event_key(new);
if let Some(old) = existing.get(&key) {
changes.extend(detect_changes(old, new, today));
existing.insert(key, new.clone());
continue;
}
// Check for time change on same date
let date_key = format!("{}|{}", new.ticker, new.date);
let mut found_old = None;
for (k, e) in existing.iter() {
if format!("{}|{}", e.ticker, e.date) == date_key && k != &key {
found_old = Some((k.clone(), e.clone()));
break;
}
}
if let Some((old_key, old_event)) = found_old {
if new.date.as_str() > today {
changes.push(CompanyEventChange {
ticker: new.ticker.clone(),
date: new.date.clone(),
field_changed: "time".to_string(),
old_value: old_event.time.clone(),
new_value: new.time.clone(),
detected_at: Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
});
}
existing.remove(&old_key);
}
existing.insert(key, new.clone());
}
ProcessResult { changes }
}