update all endpoints

This commit is contained in:
Ayomide
2025-07-08 19:57:35 +01:00
parent b76a3e75f3
commit f1af775950
7 changed files with 400 additions and 54 deletions
+61 -21
View File
@@ -1,26 +1,66 @@
# backend/news_fetcher.py
from datetime import datetime
import feedparser
import json
import os
from datetime import datetime
from .config import Config
def fetch_news(rss_url):
feed = feedparser.parse(rss_url)
def fetch_rss_news(feed_url):
"""Fetch news from RSS feed"""
feed = feedparser.parse(feed_url)
articles = []
for entry in feed.entries:
try:
# Try parsing with timezone first
pub_date = datetime.strptime(entry.published, "%a, %d %b %Y %H:%M:%S %z")
except ValueError:
try:
# Fallback to GMT format without timezone
pub_date = datetime.strptime(entry.published, "%a, %d %b %Y %H:%M:%S %Z")
except ValueError:
# Final fallback - use current time if parsing fails
pub_date = datetime.now()
articles.append({
article = {
"title": entry.title,
"content": entry.description,
"published": pub_date,
"source": rss_url
})
return articles
"content": getattr(entry, 'summary', ''),
"date": getattr(entry, 'published', ''),
"slug": entry.title.lower().replace(" ", "-").replace(",", "").replace(".", ""),
"categories": ["Technology", "AI and Innovation"],
"tags": ["AI", "Technology", "Innovation"],
"url": getattr(entry, 'link', ''),
"source": feed_url
}
articles.append(article)
return articles
def fetch_all_news():
"""Fetch news from all RSS feeds"""
all_articles = []
for feed_url in Config.RSS_FEEDS:
try:
articles = fetch_rss_news(feed_url)
all_articles.extend(articles)
except Exception as e:
print(f"Error fetching from {feed_url}: {str(e)}")
return all_articles
def save_raw_news(articles):
"""Save raw news articles to file"""
os.makedirs(Config.RAW_NEWS_PATH, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{Config.RAW_NEWS_PATH}news_{timestamp}.json"
with open(filename, 'w') as f:
json.dump(articles, f, indent=2)
return filename
def save_processed_news(articles):
"""Save processed news articles to file"""
os.makedirs(Config.PROCESSED_NEWS_PATH, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{Config.PROCESSED_NEWS_PATH}processed_news_{timestamp}.json"
with open(filename, 'w') as f:
json.dump(articles, f, indent=2)
return filename