Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved email lookup with seon fallback #224

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions recipes/EmailFaceInpainting.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,33 @@ class TwitterError(Exception):


def get_photo_for_email(email_address):
saved_photo = get_stored_photo_for_email(email_address)
if saved_photo:
return saved_photo

r = requests.get(
f"https://api.seon.io/SeonRestService/email-api/v2.2/{email_address}",
headers={"X-API-KEY": settings.SEON_API_KEY},
)
r.raise_for_status()

return update_stored_photo_for_email(email_address, r.json())


def get_stored_photo_for_email(email_address):
doc_ref = db.get_doc_ref(email_address, collection_id="apollo_io_photo_cache")

doc = db.get_or_create_doc(doc_ref).to_dict()
photo_url = doc.get("photo_url")
if photo_url:
return photo_url

r = requests.get(
f"https://api.seon.io/SeonRestService/email-api/v2.2/{email_address}",
headers={"X-API-KEY": settings.SEON_API_KEY},
)
r.raise_for_status()
return None

account_details = glom.glom(r.json(), "data.account_details", default={})

def update_stored_photo_for_email(email_address, profile):
doc_ref = db.get_doc_ref(email_address, collection_id="apollo_io_photo_cache")
account_details = glom.glom(profile, "data.account_details", default={})
for spec in [
"linkedin.photo",
"facebook.photo",
Expand All @@ -379,6 +392,7 @@ def get_photo_for_email(email_address):
doc_ref.set({"photo_url": photo_url})

return photo_url
return None


def get_photo_for_twitter_handle(twitter_handle):
Expand Down
12 changes: 7 additions & 5 deletions recipes/QRCodeGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
Schedulers,
)
from daras_ai_v2.vcard import VCARD
from recipes.EmailFaceInpainting import get_photo_for_email
from recipes.SocialLookupEmail import get_profile_for_email
from recipes.SocialLookupEmail import get_profile_for_email, extend_profile_for_email
from url_shortener.models import ShortenedURL

ATTEMPTS = 1
Expand Down Expand Up @@ -631,16 +630,19 @@ class InvalidQRCode(AssertionError):
def get_vcard_from_email(
email: str, url_fields=("github_url", "linkedin_url", "facebook_url", "twitter_url")
) -> VCARD | None:
person = get_profile_for_email(email)
try:
person = get_profile_for_email(email)
extend_profile_for_email(email, person)
except requests.exceptions.HTTPError:
return None
if not person:
return None
photo_url = get_photo_for_email(email)
return VCARD(
email=email,
format_name=person.get("name") or "",
tel=person.get("phone"),
role=person.get("title"),
photo_url=photo_url,
photo_url=person.get("photo"),
urls=list(set(filter(None, [person.get(field, "") for field in url_fields]))),
note=person.get("headline"),
organization=person.get("organization", {}).get("name"),
Expand Down
104 changes: 104 additions & 0 deletions recipes/SocialLookupEmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import requests
from pydantic import BaseModel
import glom

import gooey_ui as st
from bots.models import Workflow
Expand All @@ -12,6 +13,10 @@
from daras_ai_v2.language_model import run_language_model, LargeLanguageModels
from daras_ai_v2.loom_video_widget import youtube_video
from daras_ai_v2.redis_cache import redis_cache_decorator
from recipes.EmailFaceInpainting import (
get_stored_photo_for_email,
update_stored_photo_for_email,
)

email_regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
DEFAULT_SOCIAL_LOOKUP_EMAIL_META_IMG = "https://storage.googleapis.com/dara-c1b52.appspot.com/daras_ai/media/assets/email%20ver%202.png"
Expand Down Expand Up @@ -246,3 +251,102 @@ def get_profile_for_email(email_address) -> dict | None:
return

return person


def extend_profile_for_email(email_address, profile_to_extend) -> None:
seon_profile = get_profile_for_email_seon(email_address)
if not seon_profile:
return

account_details = glom.glom(seon_profile, "data.account_details", default={})
for spec in [
"linkedin.name",
"facebook.name",
"airbnb.first_name",
"skype.name",
"flickr.username",
"gravatar.name",
]:
profile_to_extend["name"] = profile_to_extend.get("name") or glom.glom(
account_details, spec, default=None
)

for spec in ["linkedin.title"]:
profile_to_extend["title"] = profile_to_extend.get("title") or glom.glom(
account_details, spec, default=None
)

profile_to_extend["photo"] = profile_to_extend.get(
"photo"
) or get_stored_photo_for_email(email_address)
profile_to_extend["photo"] = profile_to_extend.get(
"photo"
) or update_stored_photo_for_email(email_address, seon_profile)

profile_to_extend["facebook_url"] = profile_to_extend.get(
"facebook_url"
) or glom.glom(account_details, "facebook.url", default=None)
profile_to_extend["linkedin_url"] = profile_to_extend.get(
"linkedin_url"
) or glom.glom(account_details, "linkedin.url", default=None)
profile_to_extend["twitter_url"] = profile_to_extend.get(
"twitter_url"
) or glom.glom(account_details, "linkedin.twitter", default=None)

for spec in [
"skype.bio",
"foursquare.bio",
]:
profile_to_extend["headline"] = profile_to_extend.get("headline") or glom.glom(
account_details, spec, default=None
)

profile_to_extend["organization"] = profile_to_extend.get("organization") or {}
for spec in [
"linkedin.company",
]:
profile_to_extend["organization"]["name"] = profile_to_extend[
"organization"
].get("name") or glom.glom(account_details, spec, default=None)

for spec in [
"skype.city",
"ok.city",
]:
profile_to_extend["city"] = profile_to_extend.get("city") or glom.glom(
account_details, spec, default=None
)
for spec in [
"skype.state",
"airbnb.location",
]:
profile_to_extend["state"] = profile_to_extend.get("state") or glom.glom(
account_details, spec, default=None
)
for spec in [
"skype.country",
"linkedin.location",
"gravatar.location",
]:
profile_to_extend["country"] = profile_to_extend.get("country") or glom.glom(
account_details, spec, default=None
)


@redis_cache_decorator
def get_profile_for_email_seon(email_address) -> dict | None:
try:
r = requests.get(
f"https://api.seon.io/SeonRestService/email-api/v2.2/{email_address}",
headers={"X-API-KEY": settings.SEON_API_KEY},
)
r.raise_for_status()
except requests.exceptions.HTTPError:
# us region lock
r = requests.get(
f"https://api.us-east-1-main.seon.io/SeonRestService/email-api/v2.2/{email_address}",
headers={"X-API-KEY": settings.SEON_API_KEY},
)
r.raise_for_status()

return r.json()
Loading