adding corporate data to webscraper
This commit is contained in:
64
src/corporate/storage.rs
Normal file
64
src/corporate/storage.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
// src/corporate/storage.rs
|
||||
use super::types::{CompanyEvent, CompanyPrice};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use tokio::fs;
|
||||
use chrono::{Local, NaiveDate};
|
||||
|
||||
/// Load all events from disk into a HashMap<ticker|date, event>
|
||||
async fn load_all_events_map() -> anyhow::Result<HashMap<String, CompanyEvent>> {
|
||||
let mut map = HashMap::new();
|
||||
let dir = std::path::Path::new("corporate_events");
|
||||
if !dir.exists() {
|
||||
return Ok(map);
|
||||
}
|
||||
|
||||
let mut entries = fs::read_dir(dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("json") {
|
||||
let content = fs::read_to_string(&path).await?;
|
||||
if let Ok(events) = serde_json::from_str::<Vec<CompanyEvent>>(&content) {
|
||||
for event in events {
|
||||
let key = format!("{}|{}", event.ticker, event.date);
|
||||
map.insert(key, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
/// Merge new events with existing ones and save back to disk
|
||||
pub async fn merge_and_save_events(ticker: &str, new_events: Vec<CompanyEvent>) -> anyhow::Result<()> {
|
||||
let mut existing = load_all_events_map().await?;
|
||||
|
||||
// Insert or update
|
||||
for event in new_events {
|
||||
let key = format!("{}|{}", event.ticker, event.date);
|
||||
existing.insert(key, event);
|
||||
}
|
||||
|
||||
// Convert back to Vec and save (simple single file for now)
|
||||
let all_events: Vec<CompanyEvent> = existing.into_values().collect();
|
||||
let dir = std::path::Path::new("corporate_events");
|
||||
fs::create_dir_all(dir).await?;
|
||||
let path = dir.join("all_events.json");
|
||||
let json = serde_json::to_string_pretty(&all_events)?;
|
||||
fs::write(&path, json).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Save price history for a single ticker (overwrite old file)
|
||||
pub async fn save_prices_for_ticker(ticker: &str, prices: Vec<CompanyPrice>) -> anyhow::Result<()> {
|
||||
let dir = std::path::Path::new("corporate_prices");
|
||||
fs::create_dir_all(dir).await?;
|
||||
let path = dir.join(format!("{}.json", ticker));
|
||||
|
||||
// Optional: sort by date
|
||||
let mut prices = prices;
|
||||
prices.sort_by_key(|p| p.date.clone());
|
||||
|
||||
let json = serde_json::to_string_pretty(&prices)?;
|
||||
fs::write(&path, json).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user