Skip to content

Instantly share code, notes, and snippets.

@julzhk
Last active December 29, 2020 08:07
Show Gist options
  • Save julzhk/f94948012abd8645172eddbad5207a67 to your computer and use it in GitHub Desktop.
Save julzhk/f94948012abd8645172eddbad5207a67 to your computer and use it in GitHub Desktop.
www.digit-eyes.com Python API
"""
https://www.digit-eyes.com/ has an API, that uses 2 keys: one to access the API, and another to sign each request.
The documentation doesn't appear to have a Python 3.7 implementation of the signing algorithm, hence this gist.
---
Uses the requests HTTP library.
"""
import os
import base64
import hashlib
import requests
import hmac
DIGIT_EYES_KEY_M = os.environ.get('DIGIT_EYES_KEY_M', '??')
DIGIT_EYES_KEY_K = os.environ.get('DIGIT_EYES_KEY_K', '??')
def generate_digiteyes_url(upc, sig, auth_key_k=DIGIT_EYES_KEY_K):
DIGIT_EYES_URL = 'https://www.digit-eyes.com/gtin/v2_0/?upcCode={upc}&' \
'language=en&app_key={key}&signature={signature}'
return DIGIT_EYES_URL.format(
upc=upc,
key=auth_key_k,
signature=sig,
)
def generate_signature(upc,
auth_key_m=DIGIT_EYES_KEY_M):
sha_hash = hmac.new(str.encode(auth_key_m), str.encode(upc), hashlib.sha1)
sig = base64.b64encode(sha_hash.digest()).decode()
return sig
def DigitEyes_lookup(upc,
auth_key_m=DIGIT_EYES_KEY_M,
auth_key_k=DIGIT_EYES_KEY_K):
try:
sig = generate_signature(upc, auth_key_m)
url = generate_digiteyes_url(upc, sig, auth_key_k)
response = requests.request("GET",
url,
headers={'cache-control': "no-cache", })
product_data = response.json()
if response.status_code == 200:
return product_data
except Exception as err:
print(err)
return None
@ksdme
Copy link

ksdme commented Dec 29, 2020

I built https://gist.github.com/ksdme/cce86ba6a517c1c78e2f79ce1e652f1a with support for optional caching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment