| |
| """ |
| update_all_stats.py |
| =================== |
| Orchestrator script to run all data scrapers located in the subdirectories of |
| Stats_data_collection, and finally run Stats.py to compute the risk factors. |
| """ |
|
|
| import os |
| import sys |
| import glob |
| import subprocess |
| import time |
|
|
| |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| STATS_COLLECTION_DIR = os.path.join(BASE_DIR, "Stats_data_collection") |
| STATS_PY = os.path.join(STATS_COLLECTION_DIR, 'Stats.py') |
| WEATHER_STRAT_PY = os.path.join(STATS_COLLECTION_DIR, 'Weather_LLM_Strategy.py') |
|
|
| def main(): |
| print(f"{'β'*70}") |
| print(f" TEXBASE Global Stats Orchestrator") |
| print(f" Starting to run all scrapers in: {STATS_COLLECTION_DIR}") |
| print(f"{'β'*70}\n") |
|
|
| if not os.path.exists(STATS_COLLECTION_DIR): |
| print(f"β Error: Stats directory not found at {STATS_COLLECTION_DIR}") |
| sys.exit(1) |
|
|
| |
| search_pattern = os.path.join(STATS_COLLECTION_DIR, "*", "*.py") |
| scraper_scripts = glob.glob(search_pattern) |
| |
| |
| scraper_scripts = [s for s in scraper_scripts if os.path.isfile(s) and not s.endswith('__init__.py')] |
| |
| if not scraper_scripts: |
| print("β οΈ No scraper scripts found. Check your directories.") |
| sys.exit(1) |
|
|
| print(f"Found {len(scraper_scripts)} scrapers to execute.\n") |
|
|
| success_count = 0 |
| fail_count = 0 |
| failed_scripts = [] |
|
|
| for i, script_path in enumerate(scraper_scripts, 1): |
| script_name = os.path.relpath(script_path, STATS_COLLECTION_DIR) |
| print(f"[{i}/{len(scraper_scripts)}] Running {script_name}...") |
| |
| |
| |
| script_dir = os.path.dirname(script_path) |
| |
| try: |
| start_time = time.time() |
| |
| result = subprocess.run( |
| [sys.executable, script_path], |
| cwd=script_dir, |
| capture_output=True, |
| text=True, |
| check=True |
| ) |
| elapsed = time.time() - start_time |
| print(f" β
Success in {elapsed:.1f}s") |
| success_count += 1 |
| |
| except subprocess.CalledProcessError as e: |
| elapsed = time.time() - start_time |
| print(f" β FAILED in {elapsed:.1f}s") |
| print(f" Error Output:\n{e.stderr.strip()}") |
| fail_count += 1 |
| failed_scripts.append(script_name) |
| except fileNotFoundError: |
| print(f" β Python executable not found: {sys.executable}") |
| sys.exit(1) |
|
|
| print(f"\n{'β'*70}") |
| print(f" Scraping Completed: {success_count} succeeded, {fail_count} failed") |
| if failed_scripts: |
| print(" Failed scripts:") |
| for fs in failed_scripts: |
| print(f" - {fs}") |
| print(f"{'β'*70}\n") |
|
|
| |
| if os.path.exists(STATS_PY): |
| print(f"Executing Risk Analysis Engine: {os.path.basename(STATS_PY)}...") |
| try: |
| subprocess.run( |
| [sys.executable, STATS_PY], |
| cwd=STATS_COLLECTION_DIR, |
| check=True |
| ) |
| print("\nRisk Analysis successfully completed.") |
| except subprocess.CalledProcessError: |
| print("\nRisk Analysis Engine failed to run.") |
| else: |
| print(f"Warning: Cannot find {STATS_PY}") |
|
|
| |
| if os.path.exists(WEATHER_STRAT_PY): |
| print(f"\nExecuting Weather Strategy Engine: {os.path.basename(WEATHER_STRAT_PY)}...") |
| try: |
| subprocess.run( |
| [sys.executable, WEATHER_STRAT_PY], |
| cwd=STATS_COLLECTION_DIR, |
| check=True |
| ) |
| print("\nWeather Strategy Analysis successfully completed.") |
| except subprocess.CalledProcessError: |
| print("\nWeather Strategy Engine failed to run.") |
| else: |
| print(f"Warning: Cannot find {WEATHER_STRAT_PY}") |
|
|
| if __name__ == "__main__": |
| main() |
|
|