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:
@@ -28,12 +28,16 @@ def lookup_barcode(barcode: str) -> dict | None:
|
||||
|
||||
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:
|
||||
resp = httpx.get(
|
||||
url,
|
||||
timeout=settings.off_timeout_seconds,
|
||||
headers={"User-Agent": "Project-Good-Selfhosted/1.0"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
except httpx.HTTPError:
|
||||
return None
|
||||
@@ -41,7 +45,10 @@ def lookup_barcode(barcode: str) -> dict | None:
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
|
||||
try:
|
||||
data = resp.json()
|
||||
except ValueError:
|
||||
return None
|
||||
if data.get("status") != 1:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user