Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,9 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
import requests
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
|
@@ -35,34 +37,68 @@ def coin_price_and_news(coin: str) -> str:
|
|
| 35 |
str: A summary of the coin's price, 24h change, and the latest related news.
|
| 36 |
"""
|
| 37 |
|
|
|
|
| 38 |
coin = coin.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
data = cg.json()
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
root = ET.fromstring(rss.content)
|
| 53 |
-
|
|
|
|
| 54 |
for item in root.findall(".//item"):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import requests
|
| 7 |
+
import xml.etree.ElementTree as ET
|
| 8 |
+
from requests.exceptions import RequestException
|
| 9 |
|
| 10 |
|
| 11 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 37 |
str: A summary of the coin's price, 24h change, and the latest related news.
|
| 38 |
"""
|
| 39 |
|
| 40 |
+
|
| 41 |
coin = coin.lower()
|
| 42 |
+
price_info = "Price data unavailable"
|
| 43 |
+
news_info = "News data unavailable"
|
| 44 |
+
|
| 45 |
+
# Fetch price data
|
| 46 |
try:
|
| 47 |
+
cg = requests.get(
|
| 48 |
+
"https://api.coingecko.com/api/v3/simple/price",
|
| 49 |
+
params={
|
| 50 |
+
"ids": coin,
|
| 51 |
+
"vs_currencies": "usd",
|
| 52 |
+
"include_24hr_change": "true"
|
| 53 |
+
},
|
| 54 |
+
timeout=10
|
| 55 |
+
)
|
| 56 |
+
cg.raise_for_status() # Raise exception for HTTP errors
|
| 57 |
data = cg.json()
|
| 58 |
+
|
| 59 |
+
if coin in data:
|
| 60 |
+
price = data[coin].get("usd", "N/A")
|
| 61 |
+
change = data[coin].get("usd_24h_change")
|
| 62 |
+
change_str = f"{change:.2f}%" if change is not None else "N/A"
|
| 63 |
+
price_info = f"{coin.capitalize()} Price: ${price:.2f} USD, Change: {change_str}"
|
| 64 |
+
else:
|
| 65 |
+
price_info = f"No price data found for {coin}."
|
| 66 |
+
except RequestException as e:
|
| 67 |
+
price_info = f"Error fetching price: {str(e)}"
|
| 68 |
+
except (ValueError, KeyError) as e:
|
| 69 |
+
price_info = f"Error processing price data: {str(e)}"
|
| 70 |
+
|
| 71 |
+
# Fetch news
|
| 72 |
+
try:
|
| 73 |
+
rss = requests.get("https://cointelegraph.com/rss", timeout=10)
|
| 74 |
+
rss.raise_for_status()
|
| 75 |
root = ET.fromstring(rss.content)
|
| 76 |
+
|
| 77 |
+
news_items = []
|
| 78 |
for item in root.findall(".//item"):
|
| 79 |
+
title = item.find("title")
|
| 80 |
+
desc = item.find("description")
|
| 81 |
+
link = item.find("link")
|
| 82 |
+
|
| 83 |
+
title_text = title.text if title is not None and title.text else "No title"
|
| 84 |
+
desc_text = desc.text if desc is not None and desc.text else "No description"
|
| 85 |
+
link_text = link.text if link is not None and link.text else "No link"
|
| 86 |
+
|
| 87 |
+
if coin in title_text.lower() or coin in desc_text.lower():
|
| 88 |
+
news_items.append(f"Title: {title_text}\nLink: {link_text}\nDescription: {desc_text}")
|
| 89 |
+
if len(news_items) >= 3: # Limit to 3 news items
|
| 90 |
+
break
|
| 91 |
+
|
| 92 |
+
if news_items:
|
| 93 |
+
news_info = "\n\n".join(news_items)
|
| 94 |
+
else:
|
| 95 |
+
news_info = f"No recent news found for {coin}."
|
| 96 |
+
except RequestException as e:
|
| 97 |
+
news_info = f"Error fetching news: {str(e)}"
|
| 98 |
except Exception as e:
|
| 99 |
+
news_info = f"Error processing news data: {str(e)}"
|
| 100 |
+
|
| 101 |
+
return f"{price_info}\n\nNews:\n{news_info}"
|
| 102 |
|
| 103 |
|
| 104 |
|