update
This commit is contained in:
123
backend/routes/news_routes.py
Normal file
123
backend/routes/news_routes.py
Normal file
@@ -0,0 +1,123 @@
|
||||
from flask import Blueprint, jsonify
|
||||
from database import articles_collection
|
||||
from services.news_service import fetch_munich_news, save_articles_to_db
|
||||
|
||||
news_bp = Blueprint('news', __name__)
|
||||
|
||||
|
||||
@news_bp.route('/api/news', methods=['GET'])
|
||||
def get_news():
|
||||
"""Get latest Munich news"""
|
||||
try:
|
||||
# Fetch fresh news and save to database
|
||||
articles = fetch_munich_news()
|
||||
save_articles_to_db(articles)
|
||||
|
||||
# Get articles from MongoDB, sorted by created_at (newest first)
|
||||
cursor = articles_collection.find().sort('created_at', -1).limit(20)
|
||||
|
||||
db_articles = []
|
||||
for doc in cursor:
|
||||
article = {
|
||||
'title': doc.get('title', ''),
|
||||
'author': doc.get('author'),
|
||||
'link': doc.get('link', ''),
|
||||
'source': doc.get('source', ''),
|
||||
'published': doc.get('published_at', ''),
|
||||
'word_count': doc.get('word_count'),
|
||||
'has_full_content': bool(doc.get('content')),
|
||||
'has_summary': bool(doc.get('summary'))
|
||||
}
|
||||
|
||||
# Include AI summary if available
|
||||
if doc.get('summary'):
|
||||
article['summary'] = doc.get('summary', '')
|
||||
article['summary_word_count'] = doc.get('summary_word_count')
|
||||
article['summarized_at'] = doc.get('summarized_at', '').isoformat() if doc.get('summarized_at') else None
|
||||
# Fallback: Include preview of content if no summary (first 200 chars)
|
||||
elif doc.get('content'):
|
||||
article['preview'] = doc.get('content', '')[:200] + '...'
|
||||
|
||||
db_articles.append(article)
|
||||
|
||||
# Combine fresh articles with database articles and deduplicate
|
||||
seen_links = set()
|
||||
combined = []
|
||||
|
||||
# Add fresh articles first (they're more recent)
|
||||
for article in articles:
|
||||
link = article.get('link', '')
|
||||
if link and link not in seen_links:
|
||||
seen_links.add(link)
|
||||
combined.append(article)
|
||||
|
||||
# Add database articles
|
||||
for article in db_articles:
|
||||
link = article.get('link', '')
|
||||
if link and link not in seen_links:
|
||||
seen_links.add(link)
|
||||
combined.append(article)
|
||||
|
||||
return jsonify({'articles': combined[:20]}), 200
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@news_bp.route('/api/news/<path:article_url>', methods=['GET'])
|
||||
def get_article_by_url(article_url):
|
||||
"""Get full article content by URL"""
|
||||
try:
|
||||
# Decode URL
|
||||
from urllib.parse import unquote
|
||||
decoded_url = unquote(article_url)
|
||||
|
||||
# Find article by link
|
||||
article = articles_collection.find_one({'link': decoded_url})
|
||||
|
||||
if not article:
|
||||
return jsonify({'error': 'Article not found'}), 404
|
||||
|
||||
return jsonify({
|
||||
'title': article.get('title', ''),
|
||||
'author': article.get('author'),
|
||||
'link': article.get('link', ''),
|
||||
'content': article.get('content', ''),
|
||||
'summary': article.get('summary'),
|
||||
'word_count': article.get('word_count', 0),
|
||||
'summary_word_count': article.get('summary_word_count'),
|
||||
'source': article.get('source', ''),
|
||||
'published_at': article.get('published_at', ''),
|
||||
'crawled_at': article.get('crawled_at', '').isoformat() if article.get('crawled_at') else None,
|
||||
'summarized_at': article.get('summarized_at', '').isoformat() if article.get('summarized_at') else None,
|
||||
'created_at': article.get('created_at', '').isoformat() if article.get('created_at') else None
|
||||
}), 200
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@news_bp.route('/api/stats', methods=['GET'])
|
||||
def get_stats():
|
||||
"""Get subscription statistics"""
|
||||
try:
|
||||
from database import subscribers_collection
|
||||
|
||||
# Count only active subscribers
|
||||
subscriber_count = subscribers_collection.count_documents({'status': 'active'})
|
||||
|
||||
# Also get total article count
|
||||
article_count = articles_collection.count_documents({})
|
||||
|
||||
# Count crawled articles
|
||||
crawled_count = articles_collection.count_documents({'content': {'$exists': True, '$ne': ''}})
|
||||
|
||||
# Count summarized articles
|
||||
summarized_count = articles_collection.count_documents({'summary': {'$exists': True, '$ne': ''}})
|
||||
|
||||
return jsonify({
|
||||
'subscribers': subscriber_count,
|
||||
'articles': article_count,
|
||||
'crawled_articles': crawled_count,
|
||||
'summarized_articles': summarized_count
|
||||
}), 200
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user