feat: add local ad-free newspaper
This commit is contained in:
+65
@@ -863,11 +863,76 @@ if __name__ == "__main__":
|
||||
# Update cache every 15 minutes
|
||||
time.sleep(900)
|
||||
|
||||
def update_news_cache():
|
||||
"""Fetch top RSS news feeds (BBC, NPR) in background and cache as local JSON to bypass CORS/Ads."""
|
||||
import xml.etree.ElementTree as ET
|
||||
import re
|
||||
cache_file = _HOME / "kiosk-home" / "news-data.json"
|
||||
|
||||
feeds = {
|
||||
"World News": "http://feeds.bbci.co.uk/news/world/rss.xml",
|
||||
"National News": "https://feeds.npr.org/1001/rss.xml"
|
||||
}
|
||||
|
||||
while True:
|
||||
payload = {}
|
||||
for category, url in feeds.items():
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
||||
xml_raw = urllib.request.urlopen(req, timeout=12).read()
|
||||
root = ET.fromstring(xml_raw)
|
||||
|
||||
stories = []
|
||||
channel = root.find("channel")
|
||||
if channel is not None:
|
||||
# Fetch up to 10 stories per category
|
||||
for idx, item in enumerate(channel.findall("item")):
|
||||
if idx >= 10:
|
||||
break
|
||||
title = item.find("title")
|
||||
desc = item.find("description")
|
||||
pub_date = item.find("pubDate")
|
||||
|
||||
t_str = title.text.strip() if title is not None and title.text else ""
|
||||
d_str = desc.text.strip() if desc is not None and desc.text else ""
|
||||
|
||||
# Clean HTML tags from description if any
|
||||
d_str = re.sub('<[^<]+?>', '', d_str)
|
||||
|
||||
p_str = ""
|
||||
if pub_date is not None and pub_date.text:
|
||||
try:
|
||||
p_str = " ".join(pub_date.text.split()[:4])
|
||||
except Exception:
|
||||
p_str = pub_date.text
|
||||
|
||||
stories.append({
|
||||
"title": t_str,
|
||||
"summary": d_str,
|
||||
"date": p_str
|
||||
})
|
||||
payload[category] = stories
|
||||
print(f"Successfully cached {len(stories)} stories for category: {category}")
|
||||
except Exception as e:
|
||||
print(f"Failed to fetch RSS category {category}: {e}")
|
||||
|
||||
if payload:
|
||||
try:
|
||||
cache_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
cache_file.write_text(json.dumps(payload, indent=2))
|
||||
print("Updated local news-data.json cache successfully")
|
||||
except Exception as e:
|
||||
print(f"Failed to write news cache file: {e}")
|
||||
|
||||
# Update news every 30 minutes
|
||||
time.sleep(1800)
|
||||
|
||||
def init_cdp():
|
||||
if cdp.wait_ready(timeout=30):
|
||||
print("CDP ready — persistent WebSocket connected successfully")
|
||||
|
||||
threading.Thread(target=update_weather_cache, daemon=True).start()
|
||||
threading.Thread(target=update_news_cache, daemon=True).start()
|
||||
threading.Thread(target=init_cdp, daemon=True).start()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
||||
Reference in New Issue
Block a user