74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
"""
|
|
Lightweight Python API service for signature validation
|
|
This can run independently to support the PHP application
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
|
|
from bibapi import catalogue
|
|
from fastapi import FastAPI, Query
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
|
|
app = FastAPI(title="Signature Validation API")
|
|
|
|
# Allow PHP application to call this API
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # In production, restrict to your PHP server domain
|
|
allow_credentials=True,
|
|
allow_methods=["GET"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Initialize catalogue for signature validation
|
|
cat = catalogue.Catalogue()
|
|
|
|
|
|
@app.get("/api/validate-signature")
|
|
async def validate_signature(signature: str = Query(...)):
|
|
"""Validate a book signature and return total pages"""
|
|
try:
|
|
book_result = cat.get_book_with_data(signature)
|
|
if book_result and hasattr(book_result, "pages") and book_result.pages:
|
|
# Try to extract numeric page count
|
|
pages_str = str(book_result.pages)
|
|
# Extract first number from pages string (e.g., "245 S." -> 245)
|
|
match = re.search(r"(\d+)", pages_str)
|
|
if match:
|
|
total_pages = int(match.group(1))
|
|
return JSONResponse(
|
|
{"valid": True, "total_pages": total_pages, "signature": signature}
|
|
)
|
|
|
|
return JSONResponse(
|
|
{
|
|
"valid": False,
|
|
"error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar",
|
|
"signature": signature,
|
|
}
|
|
)
|
|
except Exception as e:
|
|
return JSONResponse(
|
|
{
|
|
"valid": False,
|
|
"error": f"Fehler bei der Validierung: {str(e)}",
|
|
"signature": signature,
|
|
},
|
|
status_code=500,
|
|
)
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint"""
|
|
return {"status": "ok", "service": "signature-validation"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
port = int(os.getenv("API_PORT", "8001"))
|
|
uvicorn.run(app, host="0.0.0.0", port=port)
|