Spaces:
Sleeping
Sleeping
Delete api_client.py
Browse files- api_client.py +0 -39
api_client.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
|
| 3 |
-
def fetch_product(info):
|
| 4 |
-
"""Multi-step search strategy"""
|
| 5 |
-
# barcode search first
|
| 6 |
-
for barcode in info['barcodes']:
|
| 7 |
-
product = fetch_by_barcode(barcode)
|
| 8 |
-
if product:
|
| 9 |
-
return product
|
| 10 |
-
|
| 11 |
-
# incase Try brand + product name search
|
| 12 |
-
if info['brand'] and info['product_name']:
|
| 13 |
-
product = fetch_by_query(f"{info['brand']} {info['product_name']}")
|
| 14 |
-
if product:
|
| 15 |
-
return product
|
| 16 |
-
|
| 17 |
-
# Trying product name only
|
| 18 |
-
if info['product_name']:
|
| 19 |
-
product = fetch_by_query(info['product_name'])
|
| 20 |
-
if product:
|
| 21 |
-
return product
|
| 22 |
-
|
| 23 |
-
# Fallback to brand search
|
| 24 |
-
if info['brand']:
|
| 25 |
-
return fetch_by_query(info['brand'])
|
| 26 |
-
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
def fetch_by_barcode(barcode):
|
| 30 |
-
url = f"https://world.openfoodfacts.org/api/v0/product/{barcode}.json"
|
| 31 |
-
response = requests.get(url)
|
| 32 |
-
data = response.json()
|
| 33 |
-
return data.get('product') if data.get('status') == 1 else None
|
| 34 |
-
|
| 35 |
-
def fetch_by_query(query):
|
| 36 |
-
url = f"https://world.openfoodfacts.org/cgi/search.pl?search_terms={query}&sort_by=unique_scans_n&json=1"
|
| 37 |
-
response = requests.get(url)
|
| 38 |
-
products = response.json().get('products', [])
|
| 39 |
-
return products[0] if products else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|