📝 consolidate multiple list endpoints for comics

This commit is contained in:
mi
2025-11-15 20:40:18 +10:00
parent 52b80563ba
commit 3153455355
3 changed files with 209 additions and 48 deletions

40
app.py
View File

@@ -318,24 +318,19 @@ def terms():
@app.route('/api/comics')
def api_comics():
"""API endpoint - returns all comics as JSON"""
return jsonify([enrich_comic(comic) for comic in COMICS])
"""API endpoint - returns all comics as JSON (optionally paginated with sections)"""
# Check for pagination parameters
page = request.args.get('page', type=int)
per_page = request.args.get('per_page', type=int)
group_by_section = request.args.get('group_by_section', 'false').lower() in ('true', '1', 'yes')
# If no pagination requested, return simple array (backward compatible)
if page is None and per_page is None and not group_by_section:
return jsonify([enrich_comic(comic) for comic in COMICS])
@app.route('/api/comics/<int:comic_id>')
def api_comic(comic_id):
"""API endpoint - returns a specific comic as JSON"""
comic = get_comic_by_number(comic_id)
if not comic:
return jsonify({'error': 'Comic not found'}), 404
return jsonify(comic)
@app.route('/api/archive')
def api_archive():
"""API endpoint - returns paginated archive data"""
page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 24, type=int)
# Pagination requested - return paginated response
page = page or 1
per_page = per_page or 24
# Limit per_page to reasonable values
per_page = min(max(per_page, 1), 100)
@@ -343,8 +338,8 @@ def api_archive():
# Reverse order to show newest first
all_comics = [enrich_comic(comic) for comic in reversed(COMICS)]
# Group by section if enabled
sections = group_comics_by_section(all_comics)
# Group by section if enabled globally or requested via parameter
sections = group_comics_by_section(all_comics) if (SECTIONS_ENABLED or group_by_section) else [(None, all_comics)]
# Calculate pagination
total_comics = len(all_comics)
@@ -388,6 +383,15 @@ def api_archive():
})
@app.route('/api/comics/<int:comic_id>')
def api_comic(comic_id):
"""API endpoint - returns a specific comic as JSON"""
comic = get_comic_by_number(comic_id)
if not comic:
return jsonify({'error': 'Comic not found'}), 404
return jsonify(comic)
@app.route('/sitemap.xml')
def sitemap():
"""Serve the static sitemap.xml file"""