Spaces:
Running
Running
File size: 950 Bytes
1b050e5 008f4ec 1b050e5 6192fe2 1b050e5 91990bf 6192fe2 91990bf 6192fe2 91990bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import requests
from fastapi import HTTPException
async def fetch_product_data_from_api(barcode):
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
response = requests.get(url)
if response.status_code != 200:
return {"error": f"Failed to fetch data for barcode {barcode}",'status':0}
return response.json()
def extract_product_info(product_data: dict):
"""
Extracts product information (found status, name, ingredients) from OpenFoodFacts API response.
"""
found = product_data.get('status') == 1
if not found:
return False, None, []
product = product_data.get('product')
if not product:
return False, None, []
name = product.get('product_name')
ingredients = []
ingredients_list = product.get('ingredients', [])
for ingredient in ingredients_list:
ingredients.append(ingredient.get('text'))
return found, name, ingredients
|