forked from mrhapile/BuriBuri_Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitals_monitor.py
More file actions
82 lines (68 loc) · 2.77 KB
/
vitals_monitor.py
File metadata and controls
82 lines (68 loc) · 2.77 KB
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
import feedparser
from datetime import datetime, timezone, timedelta
def fetch_sector_news() -> list[dict]:
"""
Fetches recent Technology sector news headlines using Google News RSS.
Returns:
list[dict]: A list of dictionaries with 'title' and 'published' keys.
Returns empty list on failure.
Includes only headlines from the last 24 hours.
"""
# Hardcoded URL for Technology Sector (India Region)
rss_url = "https://news.google.com/rss/search?q=technology+sector&hl=en-IN&gl=IN&ceid=IN:en"
try:
# Parse RSS Feed
feed = feedparser.parse(rss_url)
# Check for parsing failure or empty feed
if not feed.entries:
print("Error: Empty feed or parsing failure (check network).")
return []
headlines = []
now_utc = datetime.now(timezone.utc)
one_day_ago = now_utc - timedelta(hours=24)
for entry in feed.entries:
title = entry.get("title", "").strip()
# Title Validation
if not title:
continue
# Time Handling
pub_date = None
if hasattr(entry, "published_parsed") and entry.published_parsed:
try:
# published_parsed is a struct_time tuple in UTC
pub_date = datetime(*entry.published_parsed[:6], tzinfo=timezone.utc)
except Exception:
pass
# If date is missing or invalid, we skip it (Strict 24h rule)
if not pub_date:
continue
# Time Filter (Last 24 hours)
if pub_date < one_day_ago:
continue
# Add valid headline
headlines.append({
"title": title,
"published": pub_date.isoformat()
})
return headlines
except Exception as e:
# Catch-all for any other runtime errors to ensure no crashes
print(f"Error fetching news: {e}")
return []
# ---------------------------------------------------------
# Usage Example
# ---------------------------------------------------------
if __name__ == "__main__":
print("=== VITALS MONITOR: News Validation (Phase 1) ===")
print("\n[Test] Fetching Technology Sector News (Last 24h)...")
try:
news = fetch_sector_news()
print(f"Fetched {len(news)} headlines.")
if news:
print("Sample Headline:")
print(f" - Title: {news[0]['title']}")
print(f" - Published: {news[0]['published']}")
else:
print("No news found (or network error).")
except Exception as e:
print(f"CRITICAL FAILURE: {e}")