# rstat_tool/fetcher.py # A dedicated, isolated script for fetching financial data. import sys import json import yfinance as yf import pandas as pd import logging # Suppress verbose yfinance logging in this isolated process logging.getLogger("yfinance").setLevel(logging.CRITICAL) def get_financial_data_isolated(ticker_symbol): """ Fetches market cap and the most recent closing price for a ticker. This is the robust version of the function. """ market_cap = None closing_price = None try: data = yf.download( ticker_symbol, period="2d", progress=False, auto_adjust=False ) if not data.empty: last_close_raw = data['Close'].iloc[-1] if pd.notna(last_close_raw): closing_price = float(last_close_raw) try: market_cap = yf.Ticker(ticker_symbol).info.get('marketCap') except Exception: # This is a non-critical failure, we can proceed without market cap pass return {"market_cap": market_cap, "closing_price": closing_price} except Exception: # This is a critical failure, return None for both return {"market_cap": None, "closing_price": None} if __name__ == "__main__": if len(sys.argv) < 2: # This script requires a ticker symbol as an argument sys.exit(1) ticker_to_fetch = sys.argv[1] result = get_financial_data_isolated(ticker_to_fetch) # Print the result as a JSON string to standard output print(json.dumps(result))