From d1736fb0efabd052c06bd82015464d5659ffa776 Mon Sep 17 00:00:00 2001 From: mi Date: Thu, 6 Nov 2025 22:41:30 +1000 Subject: [PATCH] :sparkles: api --- README.md | 32 ++++++++++++++++++++++++++++++++ app.py | 21 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83e987a..3848171 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,38 @@ For larger comic archives, consider replacing the `COMICS` list with a database: - `/about` - About the comic and author - `/static/feed.rss` - RSS feed +## API Endpoints + +The app exposes a JSON API for programmatic access: + +- **GET `/api/comics`** - Returns all comics as a JSON array + ```json + [ + { + "number": 1, + "title": "First Comic", + "filename": "comic-001.png", + "date": "2025-01-01", + "alt_text": "The very first comic", + "author_note": "This is where your comic journey begins!" + } + ] + ``` + +- **GET `/api/comics/`** - Returns a specific comic as JSON + ```json + { + "number": 1, + "title": "First Comic", + "filename": "comic-001.png", + "date": "2025-01-01", + "alt_text": "The very first comic", + "author_note": "This is where your comic journey begins!" + } + ``` + + Returns 404 with `{"error": "Comic not found"}` if the comic doesn't exist. + ## License [Add your license here] diff --git a/app.py b/app.py index 94789af..c2d3175 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ import os -from flask import Flask, render_template, abort +from flask import Flask, render_template, abort, jsonify, request from comics_data import COMICS app = Flask(__name__) @@ -59,9 +59,28 @@ def about(): return render_template('about.html', title='About') +@app.route('/api/comics') +def api_comics(): + """API endpoint - returns all comics as JSON""" + return jsonify(COMICS) + + +@app.route('/api/comics/') +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.errorhandler(404) def page_not_found(e): """404 error handler""" + # Return JSON for API requests + if request.path.startswith('/api/'): + return jsonify({'error': 'Not found'}), 404 + # Return HTML for regular pages return render_template('404.html', title='Page Not Found'), 404