Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -53,20 +53,38 @@ def get_current_stock_price(ticker_symbol: str) -> Optional[Dict[str, Any]]:
|
|
53 |
ticker_symbol: The stock ticker symbol to fetch the price for.
|
54 |
|
55 |
Returns:
|
56 |
-
A
|
57 |
- "price" : current closing price (float)
|
58 |
- "price_change" : absolute change over 5 days (float)
|
59 |
- "price_change_percent" : percent change over 5 days (float)
|
60 |
-
If there's an error, returns a dict
|
61 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
try:
|
63 |
-
url =
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
resp = requests.get(url, params=params, timeout=10)
|
70 |
resp.raise_for_status()
|
71 |
data = resp.json()
|
72 |
|
@@ -110,7 +128,6 @@ def get_current_stock_price(ticker_symbol: str) -> Optional[Dict[str, Any]]:
|
|
110 |
logger.error(f"Unexpected error fetching price for {ticker_symbol}: {e}")
|
111 |
return None
|
112 |
|
113 |
-
|
114 |
# Finally, SmolAgent’s final answer tool reference:
|
115 |
final_answer = FinalAnswerTool()
|
116 |
|
|
|
53 |
ticker_symbol: The stock ticker symbol to fetch the price for.
|
54 |
|
55 |
Returns:
|
56 |
+
A dict with:
|
57 |
- "price" : current closing price (float)
|
58 |
- "price_change" : absolute change over 5 days (float)
|
59 |
- "price_change_percent" : percent change over 5 days (float)
|
60 |
+
If there's an error, returns a dict containing an "error" key.
|
61 |
"""
|
62 |
+
url = f"https://query1.finance.yahoo.com/v8/finance/chart/{ticker_symbol}"
|
63 |
+
params = {
|
64 |
+
"range": "5d",
|
65 |
+
"interval": "1d",
|
66 |
+
}
|
67 |
+
# Send a common browser user-agent to reduce 429 rate‐limit responses
|
68 |
+
headers = {
|
69 |
+
"User-Agent": (
|
70 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
71 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
72 |
+
"Chrome/115.0.0.0 Safari/537.36"
|
73 |
+
)
|
74 |
+
}
|
75 |
+
|
76 |
try:
|
77 |
+
resp = requests.get(url, params=params, headers=headers, timeout=10)
|
78 |
+
|
79 |
+
# If rate‐limited, return a clear error
|
80 |
+
if resp.status_code == 429:
|
81 |
+
return {
|
82 |
+
"error": (
|
83 |
+
f"Rate limited by Yahoo Finance (HTTP 429). "
|
84 |
+
"Please wait a minute and try again."
|
85 |
+
)
|
86 |
+
}
|
87 |
|
|
|
88 |
resp.raise_for_status()
|
89 |
data = resp.json()
|
90 |
|
|
|
128 |
logger.error(f"Unexpected error fetching price for {ticker_symbol}: {e}")
|
129 |
return None
|
130 |
|
|
|
131 |
# Finally, SmolAgent’s final answer tool reference:
|
132 |
final_answer = FinalAnswerTool()
|
133 |
|