TEST-FRANKO / api /receipt_routes.py
Kristijan Nincevic
Fixed spar and Kaufland parsers, raised rate limiter
1d22489
from fastapi import APIRouter, File, UploadFile, HTTPException, Request, Depends
from receipt_processor.google_ocr import GoogleVisionOCR
from receipt_processor.parsers.parser_selector import ParserSelector
from db.receipt_repository import ReceiptRepository
from utils.rate_limiter import RateLimiter
import re
# Initialize OCR and parser selector
rate_limiter = RateLimiter(max_requests=200)
receipt_repository = ReceiptRepository()
ocr_processor = GoogleVisionOCR()
parser_selector = ParserSelector()
router = APIRouter(prefix="/receipts", tags=["Receipt Processing"])
@router.post("/scan")
async def process_receipt(
request: Request,
file: UploadFile = File(...),
client_ip: str = Depends(rate_limiter.check_rate_limit)
):
try:
print(f"Received file: {file.filename} ({file.content_type})")
# Read the file content
content = await file.read()
print(f"File size: {len(content)} bytes")
# Extract text using Google OCR
extracted_text = ocr_processor.extract_text(content)
print(f"Extracted text length: {len(extracted_text)} chars")
if not extracted_text:
raise HTTPException(400, "No text extracted from image")
# Select and use appropriate parser
parser = parser_selector.get_store_parser(extracted_text)
print(f"Using parser: {parser.__class__.__name__}")
parsed_receipt = parser.parse(extracted_text)
print("Parsing completed successfully")
# Try to extract date from the receipt
receipt_date = None
date_match = re.search(r'(\d{2}[./]\d{2}[./]\d{2,4})', extracted_text)
if date_match:
receipt_date = date_match.group(1)
# Store the receipt in Supabase
receipt_repository.create_receipt_request(
receipt_image=content,
parsed_data=parsed_receipt,
receipt_date=receipt_date,
submission_ip=client_ip
)
return {
"status": "success",
"message": "Receipt submitted successfully and pending review."
}
except HTTPException as e:
raise e
except Exception as e:
print(f"ERROR: {str(e)}")
raise HTTPException(500, f"Receipt processing error: {str(e)}")