added creating CompanyInfo mapping
This commit is contained in:
86
src/main.rs
86
src/main.rs
@@ -3,69 +3,41 @@ mod economic;
|
||||
mod corporate;
|
||||
mod config;
|
||||
mod util;
|
||||
mod scraper;
|
||||
|
||||
use fantoccini::{ClientBuilder};
|
||||
use serde_json::{Map, Value};
|
||||
use tokio::signal;
|
||||
use anyhow::Result;
|
||||
use config::Config;
|
||||
use scraper::webdriver::ChromeDriverPool;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The entry point of the application.
|
||||
///
|
||||
/// This function loads the configuration, initializes a shared ChromeDriver pool,
|
||||
/// and sequentially runs the full updates for corporate and economic data.
|
||||
/// Sequential execution helps prevent resource exhaustion from concurrent
|
||||
/// chromedriver instances and avoids spamming the target websites with too many requests.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if configuration loading fails, pool initialization fails,
|
||||
/// or if either update function encounters an issue (e.g., network errors,
|
||||
/// scraping failures, or chromedriver spawn failures like "program not found").
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// === Ensure data directories exist ===
|
||||
util::ensure_data_dirs().await?;
|
||||
async fn main() -> Result<()> {
|
||||
let config = Config::load().map_err(|err| {
|
||||
println!("Failed to load Config .env: {}", err);
|
||||
err
|
||||
})?;
|
||||
|
||||
// === Load configuration ===
|
||||
let config = config::Config::default();
|
||||
// Initialize the shared ChromeDriver pool once
|
||||
let pool_size = config.max_parallel_tasks;
|
||||
let pool = Arc::new(ChromeDriverPool::new(pool_size).await?);
|
||||
|
||||
// === Start ChromeDriver ===
|
||||
let mut child = std::process::Command::new("chromedriver-win64/chromedriver.exe")
|
||||
.args(["--port=9515"]) // Level 3 = minimal logs
|
||||
.spawn()?;
|
||||
// Run economic update first, passing the shared pool
|
||||
economic::run_full_update(&config, &pool).await?;
|
||||
|
||||
// Build capabilities to hide infobar + enable full rendering
|
||||
let port = 9515;
|
||||
let caps_value = serde_json::json!({
|
||||
"goog:chromeOptions": {
|
||||
"args": [
|
||||
//"--headless",
|
||||
"--disable-gpu",
|
||||
"--disable-notifications",
|
||||
"--disable-popup-blocking",
|
||||
"--disable-blink-features=AutomationControlled"
|
||||
],
|
||||
"excludeSwitches": ["enable-automation"]
|
||||
}
|
||||
});
|
||||
// Then run corporate update, passing the shared pool
|
||||
corporate::run_full_update(&config, &pool).await?;
|
||||
|
||||
let caps_map: Map<String, Value> = caps_value.as_object()
|
||||
.expect("Capabilities should be a JSON object")
|
||||
.clone();
|
||||
|
||||
let mut client = ClientBuilder::native()
|
||||
.capabilities(caps_map)
|
||||
.connect(&format!("http://localhost:{}", port))
|
||||
.await?;
|
||||
|
||||
// Graceful shutdown
|
||||
let client_clone = client.clone();
|
||||
tokio::spawn(async move {
|
||||
signal::ctrl_c().await.unwrap();
|
||||
client_clone.close().await.ok();
|
||||
std::process::exit(0);
|
||||
});
|
||||
|
||||
// === Economic Calendar Update ===
|
||||
println!("Updating Economic Calendar (High Impact Only)");
|
||||
economic::goto_and_prepare(&client).await?;
|
||||
economic::run_full_update(&client, &config).await?;
|
||||
|
||||
// === Corporate Earnings Update ===
|
||||
println!("\nUpdating Corporate Earnings");
|
||||
corporate::run_full_update(&client, &config).await?;
|
||||
|
||||
// === Cleanup ===
|
||||
client.close().await?;
|
||||
child.kill()?;
|
||||
|
||||
println!("\nAll data updated successfully!");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user