adding openfigi as identifier for company data
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// src/corporate/update.rs
|
||||
use super::{scraper::*, storage::*, helpers::*, types::*, aggregation::aggregate_best_price_data};
|
||||
use super::{scraper::*, storage::*, helpers::*, types::*, aggregation::*, openfigi::*};
|
||||
use crate::config::Config;
|
||||
|
||||
use chrono::Local;
|
||||
@@ -9,19 +9,32 @@ pub async fn run_full_update(client: &fantoccini::Client, config: &Config) -> an
|
||||
println!("Starting LEI-based corporate update");
|
||||
|
||||
// 1. Download fresh GLEIF ISIN↔LEI mapping on every run
|
||||
let lei_to_isins: HashMap<String, Vec<String>> = match load_isin_lei_csv() {
|
||||
let lei_to_isins: HashMap<String, Vec<String>> = match load_isin_lei_csv().await {
|
||||
Ok(map) => map,
|
||||
Err(e) => {
|
||||
println!("Warning: Failed to load ISIN↔LEI mapping: {}", e);
|
||||
HashMap::new()
|
||||
}
|
||||
};
|
||||
//let _isin_to_lei = load_isin_to_lei()?; // optional, useful for migration scripts
|
||||
|
||||
let figi_to_lei: HashMap<String, String> = match build_figi_to_lei_map(&lei_to_isins).await {
|
||||
Ok(map) => map,
|
||||
Err(e) => {
|
||||
println!("Warning: Failed to build FIGI→LEI map: {}", e);
|
||||
HashMap::new()
|
||||
}
|
||||
};
|
||||
|
||||
let today = chrono::Local::now().format("%Y-%m-%d").to_string();
|
||||
let mut existing_events = load_existing_events().await?;
|
||||
|
||||
let companies = load_companies().await?; // Vec<CompanyMetadata> with lei, isins, tickers
|
||||
let mut companies: Vec<CompanyMetadata> = match load_or_build_companies_figi(&lei_to_isins, &figi_to_lei).await {
|
||||
Ok(comps) => comps,
|
||||
Err(e) => {
|
||||
println!("Error loading/building company metadata: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
}; // Vec<CompanyMetadata> with lei, isins, tickers
|
||||
|
||||
for mut company in companies {
|
||||
println!("\nProcessing company: {} (LEI: {})", company.name, company.lei);
|
||||
@@ -38,6 +51,7 @@ pub async fn run_full_update(client: &fantoccini::Client, config: &Config) -> an
|
||||
}
|
||||
|
||||
// Ensure company directory exists (now uses LEI)
|
||||
//let figi_dir = format!("data/companies_by_figi/{}/", company.primary_figi);
|
||||
ensure_company_dirs(&company.lei).await?;
|
||||
save_company_metadata(&company).await?;
|
||||
|
||||
@@ -56,7 +70,8 @@ pub async fn run_full_update(client: &fantoccini::Client, config: &Config) -> an
|
||||
} else {
|
||||
for disc in discovered {
|
||||
if !all_tickers.iter().any(|t| t.ticker == disc.ticker && t.exchange_mic == disc.exchange_mic) {
|
||||
println!(" Found new listing: {} ({}) [ISIN: {}]", disc.ticker, disc.exchange_mic, isin);
|
||||
println!(" New equity listing → {} ({}) via ISIN {}",
|
||||
disc.ticker, disc.exchange_mic, isin);
|
||||
all_tickers.push(disc);
|
||||
}
|
||||
}
|
||||
@@ -140,6 +155,33 @@ pub async fn run_full_update(client: &fantoccini::Client, config: &Config) -> an
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn enrich_companies_with_leis(
|
||||
companies: &mut Vec<CompanyMetadata>,
|
||||
lei_to_isins: &HashMap<String, Vec<String>>,
|
||||
) {
|
||||
for company in companies.iter_mut() {
|
||||
if company.lei.is_empty() {
|
||||
// Try to find LEI by any known ISIN
|
||||
for isin in &company.isins {
|
||||
for (lei, isins) in lei_to_isins {
|
||||
if isins.contains(isin) {
|
||||
company.lei = lei.clone();
|
||||
println!("Found real LEI {} for {}", lei, company.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !company.lei.is_empty() { break; }
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: generate fake LEI if still missing
|
||||
if company.lei.is_empty() {
|
||||
company.lei = format!("FAKE{:019}", rand::random::<u64>());
|
||||
println!("No real LEI found → using fake for {}", company.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProcessResult {
|
||||
pub changes: Vec<CompanyEventChange>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user