diff --git a/asset/gtin_service.py b/asset/gtin_service.py index 9d28179..edd30ef 100644 --- a/asset/gtin_service.py +++ b/asset/gtin_service.py @@ -42,12 +42,35 @@ def ask_digiteyes_service(gtin): return product return None +def ask_opengtindb_service(gtin): + request = f'http://opengtindb.org/?ean={gtin}&cmd=query&queryid=400000000' + response = requests.get(request) + if response.status_code == 200: + product_txt = response.text + print(f"Got {product_txt}") + if product_txt.startswith('error=0'): + product = GtinProduct.objects.create(gtin=gtin, api_request=request, api_response=product_txt) + product_props = product_txt.splitlines() + for prop in product_props: + if prop.startswith('name='): + product.name = prop[5:] + if prop.startswith('vendor='): + product.brand = prop[7:] + LOGGER.debug( + f"Creating new product entry for gtin={gtin}:\nrequest={request}\nresponse={response.status_code}:{product_txt}") + product.save() + return product + return None + def ask_remote_services(gtin): product = ask_upcitemdb_service(gtin) if product is not None: return product product = ask_digiteyes_service(gtin) + if product is not None: + return product + product = ask_opengtindb_service(gtin) if product is not None: return product return None