walidukdz commited on
Commit
fb37c7c
·
verified ·
1 Parent(s): 9f3bf44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -23
app.py CHANGED
@@ -3,7 +3,9 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- import requests, xml.etree.ElementTree as ET
 
 
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
- # Fetch price data from CoinGecko
41
- cg = requests.get("https://api.coingecko.com/api/v3/simple/price",
42
- params={"ids": coin, "vs_currencies": "usd", "include_24hr_change": "true"},
43
- timeout=5)
 
 
 
 
 
 
44
  data = cg.json()
45
- if coin not in data:
46
- return f"No data found for {coin}."
47
- price = data[coin]["usd"]
48
- change = data[coin]["usd_24hr_change"]
49
-
50
- # Fetch Cointelegraph RSS feed and search for news mentioning the coin
51
- rss = requests.get("https://cointelegraph.com/rss", timeout=5)
 
 
 
 
 
 
 
 
 
 
52
  root = ET.fromstring(rss.content)
53
- news = "No news found."
 
54
  for item in root.findall(".//item"):
55
- text = ((item.find("title").text or "") + " " + (item.find("description").text or "")).lower()
56
- if coin in text:
57
- title = item.find("title").text or "No title"
58
- link = item.find("link").text or "No link"
59
- desc = item.find("description").text or "No description"
60
- news = f"Title: {title}\nLink: {link}\nDescription: {desc}"
61
- break
62
-
63
- return f"{coin.capitalize()} Price: ${price:.2f} USD, Change: {change:.2f}%\n\nNews:\n{news}"
 
 
 
 
 
 
 
 
 
 
64
  except Exception as e:
65
- return f"Error: {str(e)}"
 
 
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