HugKN commited on
Commit
377f457
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
@@ -33,6 +34,53 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import feedparser
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
 
34
  except Exception as e:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
37
+ @tool
38
+ def my_custom_tool(rss_url: str, max_items: int) -> str:
39
+ """
40
+ 從指定的 RSS Feed URL 獲取最新的新聞。
41
+
42
+ Args:
43
+ rss_url: 要讀取的 RSS Feed 的 URL。
44
+ max_items: 要顯示的最大新聞條目數量。
45
+ """
46
+ DEFAULT_RSS_URL = "https://rthk.hk/rthk/news/rss/c_expressnews_clocal.xml"
47
+
48
+ # 如果 rss_url 未提供或為空,則使用預設 URL
49
+ url_to_fetch = rss_url if rss_url and rss_url.strip() else DEFAULT_RSS_URL
50
+
51
+ # 確保 max_items 是正整數,如果不是或未提供,則使用預設值
52
+ try:
53
+ num_items = int(max_items)
54
+ if num_items <= 0:
55
+ num_items = 5 # 預設顯示 5 條
56
+ except (ValueError, TypeError):
57
+ num_items = 5 # 預設顯示 5 條
58
+
59
+ try:
60
+ feed = feedparser.parse(url_to_fetch)
61
+
62
+ if not feed.entries:
63
+ return f"無法從 {url_to_fetch} 獲取新聞,或者 RSS Feed 為空。"
64
+
65
+ news_output = f"來自 {url_to_fetch} 的最新新聞 (最多 {num_items} 條):\n"
66
+ for entry in feed.entries[:num_items]:
67
+ title = entry.get("title", "無標題")
68
+ link = entry.get("link", "#")
69
+ summary = entry.get("summary", "") # 嘗試獲取摘要
70
+
71
+ news_output += f"- 標題: {title}\n"
72
+ news_output += f" 連結: {link}\n"
73
+ if summary:
74
+ # 簡單清理 HTML 標籤 (如果有的話),並截斷摘要長度
75
+ import re
76
+ clean_summary = re.sub(r'<[^>]+>', '', summary)
77
+ news_output += f" 摘要: {clean_summary[:100]}...\n" # 顯示前100個字元
78
+ news_output += "\n"
79
+
80
+ return news_output
81
+
82
+ except Exception as e:
83
+ return f"讀取 RSS Feed ({url_to_fetch}) 時發生錯誤:{str(e)}"
84
 
85
  final_answer = FinalAnswerTool()
86