diff --git a/off.py b/off.py index ac4e9a7..0d55eef 100644 --- a/off.py +++ b/off.py @@ -2,7 +2,9 @@ alimento de la app. Tolerante a fallos: devuelve [] si algo va mal.""" import httpx -_SEARCH = "https://world.openfoodfacts.org/cgi/search.pl" +# Search-a-licious: el endpoint de texto fiable de OFF (el legacy cgi/search.pl +# devuelve 503 a IPs de datacenter). +_SEARCH = "https://search.openfoodfacts.org/search" def search(q: str, limit: int = 12) -> list[dict]: @@ -11,22 +13,19 @@ def search(q: str, limit: int = 12) -> list[dict]: resp = httpx.get( _SEARCH, params={ - "search_terms": q, - "search_simple": 1, - "action": "process", - "json": 1, + "q": q, "page_size": limit, - "fields": "product_name,nutriments,code", + "fields": "code,product_name,nutriments", }, timeout=6.0, - headers={"User-Agent": "autoayuno/1.0 (self-hosted)"}, + headers={"User-Agent": "autoayuno/1.0 (self-hosted; carlos)"}, ) if resp.status_code != 200: return [] out = [] - for p in resp.json().get("products", []): - name = (p.get("product_name") or "").strip() - kcal = p.get("nutriments", {}).get("energy-kcal_100g") + for h in resp.json().get("hits", []): + name = (h.get("product_name") or "").replace("\n", " ").strip() + kcal = (h.get("nutriments") or {}).get("energy-kcal_100g") if not name or kcal is None: continue try: @@ -38,8 +37,8 @@ def search(q: str, limit: int = 12) -> list[dict]: out.append({ "name": name[:120], "kcal_100g": kcal, - "off_id": p.get("code"), - "barcode": p.get("code"), + "off_id": h.get("code"), + "barcode": h.get("code"), }) return out except Exception: