import os, requests
cache = {} # use your database in production
def enrich(domain):
if domain in cache:
return cache[domain]
body = requests.get(
"https://api.prefetch.io/enrich",
params={"url": domain},
headers={"X-API-Key": os.environ["PREFETCH_API_KEY"]},
timeout=60,
).json()
if not body["success"]:
return None # not charged; sign the user up without a profile
d = body["data"]
company, brand, cls = d.get("company") or {}, d.get("brand") or {}, d.get("classification") or {}
colors = (brand.get("colors") or {}).get("brand_colors") or []
profile = {
"domain": domain,
"name": company.get("display_name") or company.get("name"),
"logo": ((brand.get("logos") or {}).get("primary") or {}).get("url"),
"color": colors[0]["hex"] if colors else None,
"industry": cls.get("category"),
"description": cls.get("tagline_description"),
}
cache[domain] = profile
return profile