Spaces:
TDN-M
/
Running on Zero

File size: 7,928 Bytes
36d7d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Module lấy tin thể thao quốc tế từ các nguồn miễn phí
Hỗ trợ RSS feeds và API miễn phí
"""

import requests
import feedparser
from bs4 import BeautifulSoup
import json
from datetime import datetime, timedelta
import re
from typing import List, Dict, Optional
import logging

# Cấu hình logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class SportsNewsAggregator:
    """Class tổng hợp tin thể thao từ nhiều nguồn miễn phí"""
    
    def __init__(self):
        self.sources = {
            'bbc_sport': {
                'url': 'http://feeds.bbci.co.uk/sport/rss.xml',
                'type': 'rss',
                'language': 'en'
            },
            'espn': {
                'url': 'https://www.espn.com/espn/rss/news',
                'type': 'rss',
                'language': 'en'
            },
            'sky_sports': {
                'url': 'https://www.skysports.com/rss/12040',
                'type': 'rss',
                'language': 'en'
            },
            'vnexpress_sport': {
                'url': 'https://vnexpress.net/rss/the-thao.rss',
                'type': 'rss',
                'language': 'vi'
            },
            'thanhnien_sport': {
                'url': 'https://thanhnien.vn/rss/the-thao.rss',
                'type': 'rss',
                'language': 'vi'
            }
        }
        
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        })
    
    def get_rss_news(self, source_config: Dict) -> List[Dict]:
        """Lấy tin từ RSS feed"""
        try:
            response = self.session.get(source_config['url'], timeout=10)
            response.raise_for_status()
            
            feed = feedparser.parse(response.content)
            news_items = []
            
            for entry in feed.entries[:10]:  # Lấy 10 tin mới nhất
                # Xử lý thời gian
                pub_date = None
                if hasattr(entry, 'published_parsed') and entry.published_parsed:
                    pub_date = datetime(*entry.published_parsed[:6])
                elif hasattr(entry, 'updated_parsed') and entry.updated_parsed:
                    pub_date = datetime(*entry.updated_parsed[:6])
                
                # Làm sạch nội dung
                summary = self.clean_text(getattr(entry, 'summary', ''))
                title = self.clean_text(getattr(entry, 'title', ''))
                
                news_item = {
                    'title': title,
                    'summary': summary,
                    'link': getattr(entry, 'link', ''),
                    'published': pub_date.isoformat() if pub_date else None,
                    'source': source_config.get('name', 'Unknown'),
                    'language': source_config.get('language', 'en')
                }
                
                news_items.append(news_item)
            
            return news_items
            
        except Exception as e:
            logger.error(f"Error fetching RSS from {source_config['url']}: {str(e)}")
            return []
    
    def clean_text(self, text: str) -> str:
        """Làm sạch văn bản HTML và ký tự đặc biệt"""
        if not text:
            return ""
        
        # Loại bỏ HTML tags
        soup = BeautifulSoup(text, 'html.parser')
        text = soup.get_text()
        
        # Loại bỏ ký tự xuống dòng và khoảng trắng thừa
        text = re.sub(r'\s+', ' ', text).strip()
        
        return text
    
    def get_all_sports_news(self, language: Optional[str] = None, limit: int = 20) -> List[Dict]:
        """Lấy tin thể thao từ tất cả nguồn"""
        all_news = []
        
        for source_name, source_config in self.sources.items():
            # Lọc theo ngôn ngữ nếu được chỉ định
            if language and source_config.get('language') != language:
                continue
            
            logger.info(f"Fetching news from {source_name}")
            source_config['name'] = source_name
            
            if source_config['type'] == 'rss':
                news_items = self.get_rss_news(source_config)
                all_news.extend(news_items)
        
        # Sắp xếp theo thời gian (mới nhất trước)
        all_news.sort(key=lambda x: x.get('published', ''), reverse=True)
        
        return all_news[:limit]
    
    def get_football_news(self, limit: int = 10) -> List[Dict]:
        """Lấy tin bóng đá cụ thể"""
        all_news = self.get_all_sports_news(limit=50)
        
        # Lọc tin bóng đá
        football_keywords = [
            'football', 'soccer', 'fifa', 'premier league', 'champions league',
            'bóng đá', 'world cup', 'euro', 'manchester', 'liverpool', 'arsenal',
            'real madrid', 'barcelona', 'psg', 'bayern', 'juventus'
        ]
        
        football_news = []
        for news in all_news:
            title_lower = news['title'].lower()
            summary_lower = news['summary'].lower()
            
            if any(keyword in title_lower or keyword in summary_lower 
                   for keyword in football_keywords):
                football_news.append(news)
        
        return football_news[:limit]
    
    def format_news_for_tts(self, news_items: List[Dict], language: str = 'vi') -> str:
        """Format tin tức thành văn bản phù hợp cho TTS"""
        if not news_items:
            return "Không có tin thể thao mới nào được tìm thấy."
        
        if language == 'vi':
            intro = "Tin thể thao quốc tế mới nhất:\n\n"
        else:
            intro = "Latest international sports news:\n\n"
        
        formatted_text = intro
        
        for i, news in enumerate(news_items[:5], 1):  # Chỉ lấy 5 tin đầu
            title = news['title']
            summary = news['summary'][:200] + "..." if len(news['summary']) > 200 else news['summary']
            
            if language == 'vi':
                news_text = f"Tin thứ {i}: {title}. {summary}\n\n"
            else:
                news_text = f"News {i}: {title}. {summary}\n\n"
            
            formatted_text += news_text
        
        return formatted_text

# Hàm tiện ích để sử dụng trong app chính
def get_sports_news_content(news_type: str = 'all', language: str = 'vi', limit: int = 5) -> str:
    """
    Hàm chính để lấy nội dung tin thể thao
    
    Args:
        news_type: 'all', 'football', 'basketball', etc.
        language: 'vi' hoặc 'en'
        limit: Số lượng tin tức
    
    Returns:
        Chuỗi văn bản đã format cho TTS
    """
    try:
        aggregator = SportsNewsAggregator()
        
        if news_type == 'football':
            news_items = aggregator.get_football_news(limit=limit)
        else:
            news_items = aggregator.get_all_sports_news(language=language, limit=limit)
        
        return aggregator.format_news_for_tts(news_items, language)
        
    except Exception as e:
        logger.error(f"Error getting sports news: {str(e)}")
        if language == 'vi':
            return f"Xin lỗi, có lỗi khi lấy tin thể thao: {str(e)}"
        else:
            return f"Sorry, error getting sports news: {str(e)}"

# Test function
if __name__ == "__main__":
    # Test lấy tin thể thao
    print("Testing sports news aggregator...")
    
    content = get_sports_news_content('all', 'vi', 3)
    print("Vietnamese sports news:")
    print(content)
    print("\n" + "="*50 + "\n")
    
    content_en = get_sports_news_content('all', 'en', 3)
    print("English sports news:")
    print(content_en)