Fix Barcode-Lookup: Open Food Facts v0-API statt v2

Die v2-API liefert kein status:1-Feld (nur code+product) und antwortet mit
HTTP 404 bei Nicht-Treffern; unser Code prueft aber auf status==1 -> selbst
vorhandene Produkte wurden als "unbekannt" gemeldet. Umstellung auf die v0-API
(status 1/0, HTTP 200), plus robustes JSON-Parsing und follow_redirects.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Scarriffle
2026-07-22 09:52:14 +02:00
parent 12823a6d73
commit 9d4fd030d8

View File

@@ -28,12 +28,16 @@ def lookup_barcode(barcode: str) -> dict | None:
Gibt ein vorbefülltes Produkt-Dict zurück oder None, wenn nicht gefunden. Gibt ein vorbefülltes Produkt-Dict zurück oder None, wenn nicht gefunden.
""" """
url = f"{settings.off_base_url}/api/v2/product/{barcode}.json" # v0-API: liefert bei Treffer status=1 + product, bei Nicht-Treffer status=0
# (HTTP 200). Die v2-API hat kein status-Feld und antwortet mit HTTP 404,
# weshalb wir bewusst v0 nutzen.
url = f"{settings.off_base_url}/api/v0/product/{barcode}.json"
try: try:
resp = httpx.get( resp = httpx.get(
url, url,
timeout=settings.off_timeout_seconds, timeout=settings.off_timeout_seconds,
headers={"User-Agent": "Project-Good-Selfhosted/1.0"}, headers={"User-Agent": "Project-Good-Selfhosted/1.0"},
follow_redirects=True,
) )
except httpx.HTTPError: except httpx.HTTPError:
return None return None
@@ -41,7 +45,10 @@ def lookup_barcode(barcode: str) -> dict | None:
if resp.status_code != 200: if resp.status_code != 200:
return None return None
try:
data = resp.json() data = resp.json()
except ValueError:
return None
if data.get("status") != 1: if data.get("status") != 1:
return None return None