Spaces:
Running
Running
| from collections import defaultdict | |
| from .models import NewsCluster | |
| class NewsPresenter: | |
| CATEGORIES = ["Geopolitical", "World Health", "Tech", "Cybersecurity", "Funny/Weird", "Gaming", "Movies", "Arab World", "Tunisia"] | |
| def display(clusters: list[NewsCluster], top_n: int = 10): | |
| if not clusters: | |
| print("\n No news stories matched your interests this round.") | |
| return | |
| by_cat: dict[str, list[NewsCluster]] = defaultdict(list) | |
| for c in clusters: | |
| cat = "General" | |
| if c.articles and c.articles[0].analysis: | |
| ca = c.articles[0].analysis.category | |
| cat = ca if ca in NewsPresenter.CATEGORIES else "General" | |
| by_cat[cat].append(c) | |
| print("β" + "β" * 78 + "β") | |
| print("β π° NEWS DIGEST β Top Stories β".center(80)) | |
| print("β" + "β" * 78 + "β") | |
| for cat in NewsPresenter.CATEGORIES: | |
| items = by_cat.get(cat, []) | |
| items.sort(key=lambda x: (x.articles[0].article.published_iso or x.articles[0].post.published_iso or "", x.final_score), reverse=True) | |
| items = items[:top_n] | |
| if not items: | |
| continue | |
| print(f"\n ββ {cat} ({len(items)}) ββ\n") | |
| for i, cluster in enumerate(items, 1): | |
| print(f" #{i:<2} [{cluster.topic:<30}] " | |
| f"Score: {cluster.final_score:.2f} " | |
| f"Trust: {cluster.avg_trustworthiness:.0%}") | |
| item = cluster.articles[0] | |
| title = item.article.title[:72] + "β¦" if item.article.title and len(item.article.title) > 72 else (item.article.title or item.post.title) | |
| print(f" {title}") | |
| published = item.article.published or item.post.published | |
| if published: | |
| print(f" π {published}") | |
| if item.analysis and item.analysis.summary: | |
| short = item.analysis.summary[:72] + "β¦" if len(item.analysis.summary) > 72 else item.analysis.summary | |
| print(f" β {short}") | |
| print() | |
| print("β" * 80) | |