slight update

This commit is contained in:
2025-11-11 14:34:43 +01:00
parent 760a458e66
commit f23f4b71d8
7 changed files with 748 additions and 34 deletions

View File

@@ -29,35 +29,12 @@ def init_db():
subscribers_collection.create_index('subscribed_at')
# Create unique index on RSS feed URLs
rss_feeds_collection.create_index('url', unique=True)
# Create index on category for filtering
rss_feeds_collection.create_index('category')
# Initialize tracking collections indexes
init_tracking_collections()
# Initialize default RSS feeds if collection is empty
if rss_feeds_collection.count_documents({}) == 0:
default_feeds = [
{
'name': 'Süddeutsche Zeitung München',
'url': 'https://www.sueddeutsche.de/muenchen/rss',
'active': True,
'created_at': datetime.utcnow()
},
{
'name': 'Münchner Merkur',
'url': 'https://www.merkur.de/muenchen/rss',
'active': True,
'created_at': datetime.utcnow()
},
{
'name': 'Abendzeitung München',
'url': 'https://www.abendzeitung-muenchen.de/rss',
'active': True,
'created_at': datetime.utcnow()
}
]
rss_feeds_collection.insert_many(default_feeds)
print(f"Initialized {len(default_feeds)} default RSS feeds")
print("Database initialized with indexes")

View File

@@ -10,15 +10,24 @@ rss_bp = Blueprint('rss', __name__)
@rss_bp.route('/api/rss-feeds', methods=['GET'])
def get_rss_feeds():
"""Get all RSS feeds"""
"""Get all RSS feeds, optionally filtered by category"""
try:
cursor = rss_feeds_collection.find().sort('created_at', -1)
# Get optional category filter
category = request.args.get('category')
# Build query
query = {}
if category:
query['category'] = category
cursor = rss_feeds_collection.find(query).sort('created_at', -1)
feeds = []
for feed in cursor:
feeds.append({
'id': str(feed['_id']),
'name': feed.get('name', ''),
'url': feed.get('url', ''),
'category': feed.get('category', 'general'),
'active': feed.get('active', True),
'created_at': feed.get('created_at', '').isoformat() if feed.get('created_at') else ''
})
@@ -29,10 +38,11 @@ def get_rss_feeds():
@rss_bp.route('/api/rss-feeds', methods=['POST'])
def add_rss_feed():
"""Add a new RSS feed"""
"""Add a new RSS feed with optional category"""
data = request.json
name = data.get('name', '').strip()
url = data.get('url', '').strip()
category = data.get('category', 'general').strip().lower()
if not name or not url:
return jsonify({'error': 'Name and URL are required'}), 400
@@ -40,6 +50,12 @@ def add_rss_feed():
if not url.startswith('http://') and not url.startswith('https://'):
return jsonify({'error': 'URL must start with http:// or https://'}), 400
# Validate category (optional, but if provided should be reasonable)
valid_categories = ['general', 'local', 'politics', 'sports', 'culture', 'business', 'technology', 'entertainment']
if category and category not in valid_categories:
# Allow custom categories but warn
pass
try:
# Test if the RSS feed is valid
try:
@@ -52,6 +68,7 @@ def add_rss_feed():
feed_doc = {
'name': name,
'url': url,
'category': category,
'active': True,
'created_at': datetime.utcnow()
}
@@ -60,7 +77,8 @@ def add_rss_feed():
result = rss_feeds_collection.insert_one(feed_doc)
return jsonify({
'message': 'RSS feed added successfully',
'id': str(result.inserted_id)
'id': str(result.inserted_id),
'category': category
}), 201
except DuplicateKeyError:
return jsonify({'error': 'RSS feed URL already exists'}), 409
@@ -122,3 +140,15 @@ def toggle_rss_feed(feed_id):
except Exception as e:
return jsonify({'error': str(e)}), 500
@rss_bp.route('/api/rss-feeds/categories', methods=['GET'])
def get_categories():
"""Get all unique categories from RSS feeds"""
try:
categories = rss_feeds_collection.distinct('category')
# Filter out None/empty and sort
categories = sorted([c for c in categories if c])
return jsonify({'categories': categories}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500