Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,6 +27,7 @@ def coin_price_and_news(coin: str) -> str:
|
|
| 27 |
coin = coin.lower()
|
| 28 |
price_info = "Price data unavailable"
|
| 29 |
news_info = "News data unavailable"
|
|
|
|
| 30 |
|
| 31 |
# Fetch price data
|
| 32 |
try:
|
|
@@ -46,14 +47,51 @@ def coin_price_and_news(coin: str) -> str:
|
|
| 46 |
price = data[coin].get("usd", "N/A")
|
| 47 |
change = data[coin].get("usd_24h_change")
|
| 48 |
change_str = f"{change:.2f}%" if change is not None else "N/A"
|
| 49 |
-
price_info = f"{coin.capitalize()} Price: ${price:.2f} USD, Change: {change_str}"
|
| 50 |
else:
|
| 51 |
-
price_info = f"No price data found for {coin}."
|
| 52 |
except RequestException as e:
|
| 53 |
-
price_info = f"Error fetching price: {str(e)}"
|
| 54 |
except (ValueError, KeyError) as e:
|
| 55 |
-
price_info = f"Error processing price data: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Fetch news
|
| 58 |
try:
|
| 59 |
rss = requests.get("https://cointelegraph.com/rss", timeout=10)
|
|
@@ -65,27 +103,50 @@ def coin_price_and_news(coin: str) -> str:
|
|
| 65 |
title = item.find("title")
|
| 66 |
desc = item.find("description")
|
| 67 |
link = item.find("link")
|
|
|
|
| 68 |
|
| 69 |
title_text = title.text if title is not None and title.text else "No title"
|
| 70 |
desc_text = desc.text if desc is not None and desc.text else "No description"
|
| 71 |
link_text = link.text if link is not None and link.text else "No link"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
if coin in title_text.lower() or coin in desc_text.lower():
|
| 74 |
-
news_items.append(
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
break
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
if news_items:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
-
news_info = f"No recent news found for {coin}."
|
| 82 |
except RequestException as e:
|
| 83 |
-
news_info = f"Error fetching news: {str(e)}"
|
| 84 |
except Exception as e:
|
| 85 |
-
news_info = f"Error processing news data: {str(e)}"
|
| 86 |
|
| 87 |
-
return f"{price_info}\n\
|
| 88 |
-
|
| 89 |
|
| 90 |
|
| 91 |
|
|
|
|
| 27 |
coin = coin.lower()
|
| 28 |
price_info = "Price data unavailable"
|
| 29 |
news_info = "News data unavailable"
|
| 30 |
+
historical_info = "Historical data unavailable"
|
| 31 |
|
| 32 |
# Fetch price data
|
| 33 |
try:
|
|
|
|
| 47 |
price = data[coin].get("usd", "N/A")
|
| 48 |
change = data[coin].get("usd_24h_change")
|
| 49 |
change_str = f"{change:.2f}%" if change is not None else "N/A"
|
| 50 |
+
price_info = f"💰 {coin.capitalize()} Price: ${price:.2f} USD, Change: {change_str}"
|
| 51 |
else:
|
| 52 |
+
price_info = f"❌ No price data found for {coin}."
|
| 53 |
except RequestException as e:
|
| 54 |
+
price_info = f"❌ Error fetching price: {str(e)}"
|
| 55 |
except (ValueError, KeyError) as e:
|
| 56 |
+
price_info = f"❌ Error processing price data: {str(e)}"
|
| 57 |
+
|
| 58 |
+
# Fetch historical data for 7-day and 30-day trends
|
| 59 |
+
try:
|
| 60 |
+
historical = requests.get(
|
| 61 |
+
f"https://api.coingecko.com/api/v3/coins/{coin}/market_chart",
|
| 62 |
+
params={
|
| 63 |
+
"vs_currency": "usd",
|
| 64 |
+
"days": "30",
|
| 65 |
+
"interval": "daily"
|
| 66 |
+
},
|
| 67 |
+
timeout=10
|
| 68 |
+
)
|
| 69 |
+
historical.raise_for_status()
|
| 70 |
+
hist_data = historical.json()
|
| 71 |
|
| 72 |
+
if 'prices' in hist_data and len(hist_data['prices']) > 0:
|
| 73 |
+
# Get 7-day data (last 7 points)
|
| 74 |
+
seven_day_prices = hist_data['prices'][-7:]
|
| 75 |
+
seven_day_start = seven_day_prices[0][1]
|
| 76 |
+
seven_day_end = seven_day_prices[-1][1]
|
| 77 |
+
seven_day_change = ((seven_day_end - seven_day_start) / seven_day_start) * 100
|
| 78 |
+
|
| 79 |
+
# Get 30-day data (or as many days as available)
|
| 80 |
+
thirty_day_prices = hist_data['prices']
|
| 81 |
+
thirty_day_start = thirty_day_prices[0][1]
|
| 82 |
+
thirty_day_end = thirty_day_prices[-1][1]
|
| 83 |
+
thirty_day_change = ((thirty_day_end - thirty_day_start) / thirty_day_start) * 100
|
| 84 |
+
|
| 85 |
+
historical_info = f"""📈 Historical Trends:
|
| 86 |
+
• 7-Day: ${seven_day_start:.2f} → ${seven_day_end:.2f} ({seven_day_change:.2f}%)
|
| 87 |
+
• 30-Day: ${thirty_day_start:.2f} → ${thirty_day_end:.2f} ({thirty_day_change:.2f}%)"""
|
| 88 |
+
else:
|
| 89 |
+
historical_info = "📈 Historical data not available for this coin."
|
| 90 |
+
except RequestException as e:
|
| 91 |
+
historical_info = f"📈 Error fetching historical data: {str(e)}"
|
| 92 |
+
except (ValueError, KeyError) as e:
|
| 93 |
+
historical_info = f"📈 Error processing historical data: {str(e)}"
|
| 94 |
+
|
| 95 |
# Fetch news
|
| 96 |
try:
|
| 97 |
rss = requests.get("https://cointelegraph.com/rss", timeout=10)
|
|
|
|
| 103 |
title = item.find("title")
|
| 104 |
desc = item.find("description")
|
| 105 |
link = item.find("link")
|
| 106 |
+
pub_date = item.find("pubDate")
|
| 107 |
|
| 108 |
title_text = title.text if title is not None and title.text else "No title"
|
| 109 |
desc_text = desc.text if desc is not None and desc.text else "No description"
|
| 110 |
link_text = link.text if link is not None and link.text else "No link"
|
| 111 |
+
date_text = pub_date.text if pub_date is not None and pub_date.text else None
|
| 112 |
+
|
| 113 |
+
# Format the date if available
|
| 114 |
+
formatted_date = "Unknown date"
|
| 115 |
+
if date_text:
|
| 116 |
+
try:
|
| 117 |
+
from email.utils import parsedate_to_datetime
|
| 118 |
+
date_obj = parsedate_to_datetime(date_text)
|
| 119 |
+
formatted_date = date_obj.strftime("%b %d, %Y")
|
| 120 |
+
except Exception:
|
| 121 |
+
formatted_date = date_text
|
| 122 |
|
| 123 |
if coin in title_text.lower() or coin in desc_text.lower():
|
| 124 |
+
news_items.append({
|
| 125 |
+
"title": title_text,
|
| 126 |
+
"link": link_text,
|
| 127 |
+
"description": desc_text,
|
| 128 |
+
"date": formatted_date,
|
| 129 |
+
"date_obj": date_obj if 'date_obj' in locals() else None
|
| 130 |
+
})
|
| 131 |
+
if len(news_items) >= 5: # Limit to 5 news items
|
| 132 |
break
|
| 133 |
|
| 134 |
+
# Sort news items by date (newest first)
|
| 135 |
+
news_items.sort(key=lambda x: x["date_obj"] if x["date_obj"] else datetime.datetime.min, reverse=True)
|
| 136 |
+
|
| 137 |
if news_items:
|
| 138 |
+
news_texts = []
|
| 139 |
+
for item in news_items:
|
| 140 |
+
news_texts.append(f"📰 [{item['date']}] {item['title']}\n 🔗 {item['link']}\n 📝 {item['description'][:150]}...")
|
| 141 |
+
news_info = "\n\n".join(news_texts)
|
| 142 |
else:
|
| 143 |
+
news_info = f"📰 No recent news found for {coin}."
|
| 144 |
except RequestException as e:
|
| 145 |
+
news_info = f"📰 Error fetching news: {str(e)}"
|
| 146 |
except Exception as e:
|
| 147 |
+
news_info = f"📰 Error processing news data: {str(e)}"
|
| 148 |
|
| 149 |
+
return f"🪙 {coin.upper()} INFORMATION\n\n{price_info}\n\n{historical_info}\n\nNEWS:\n{news_info}"
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
|