Update app.py
Browse files
app.py
CHANGED
|
@@ -34,6 +34,44 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
model = HfApiModel(
|
| 39 |
max_tokens=2096,
|
|
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
+
import requests
|
| 38 |
+
from bs4 import BeautifulSoup
|
| 39 |
+
|
| 40 |
+
@tool
|
| 41 |
+
def get_stock_data(stock_code: str) -> str:
|
| 42 |
+
"""A tool that fetches the latest stock data for a given stock code from East Money website.
|
| 43 |
+
Args:
|
| 44 |
+
stock_code: A string representing the stock code (e.g., 'SH600000' for Shanghai Pudong Development Bank).
|
| 45 |
+
"""
|
| 46 |
+
try:
|
| 47 |
+
# Construct the URL for the stock data
|
| 48 |
+
url = f"https://quote.eastmoney.com/{stock_code}.html"
|
| 49 |
+
|
| 50 |
+
# Send a GET request to the URL
|
| 51 |
+
response = requests.get(url)
|
| 52 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 53 |
+
|
| 54 |
+
# Parse the HTML content using BeautifulSoup
|
| 55 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 56 |
+
|
| 57 |
+
# Extract the stock name
|
| 58 |
+
stock_name = soup.find('div', class_='stock-name').text.strip()
|
| 59 |
+
|
| 60 |
+
# Extract the current price
|
| 61 |
+
current_price = soup.find('div', class_='stock-current').text.strip()
|
| 62 |
+
|
| 63 |
+
# Extract the change in price and percentage
|
| 64 |
+
price_change = soup.find('div', class_='stock-change').text.strip()
|
| 65 |
+
change_percentage = soup.find('div', class_='stock-change-percent').text.strip()
|
| 66 |
+
|
| 67 |
+
# Construct the result string
|
| 68 |
+
result = f"Stock: {stock_name}\nCurrent Price: {current_price}\nChange: {price_change} ({change_percentage})"
|
| 69 |
+
|
| 70 |
+
return result
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error fetching stock data for code '{stock_code}': {str(e)}"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
final_answer = FinalAnswerTool()
|
| 76 |
model = HfApiModel(
|
| 77 |
max_tokens=2096,
|