Created
November 17, 2018 10:07
-
-
Save backupbrain/6265f0aaa84ee634f62826a41cb2eb34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import urllib.request | |
import json | |
import requests | |
full_name = "Professor John Roberts Smith Jr." | |
domain = "example.com" | |
api_key = "<neverbounce_api_key>" | |
name_parts = full_name.lower().split(" ") | |
known_suffixes = ["jr", "ii", "iii", "ix", "sr", "the", "third", "fourth"] | |
known_prefixes = ["mr", "mrs", "ms", "dr", "prof", "professor"] | |
known_last_names = ["van", "der", "mc", "mac", "de", "la"] | |
prefixes = [] | |
suffixes = [] | |
names = [] | |
for name_id in range(0, len(name_parts)): | |
name = name_parts[name_id] | |
test_name = name.lower().strip(".") | |
if test_name in known_prefixes: | |
prefixes.append(name) | |
elif test_name in known_suffixes: | |
suffixes.append(name) | |
else: | |
names.append(name) | |
# for now, assume first name | |
composite_name = { | |
"prefixes": prefixes, | |
"given": "", | |
"middle": [], | |
"surnames": [] | |
} | |
num_names = len(names) | |
if num_names >= 1: | |
composite_name["given"] = names[0] | |
if num_names > 2: | |
for middle_name_id in range(1, num_names-1): | |
middle_name = names[middle_name_id] | |
test_middle_name = middle_name.lower().strip() | |
if test_middle_name in known_last_names: | |
composite_name["surnames"].append(middle_name) | |
else: | |
composite_name["middle"].append(middle_name) | |
if num_names >= 2: | |
composite_name["surnames"].append(names[num_names-1]) | |
print(composite_name) | |
combos = [ | |
"{given_name}", | |
"{surname_name}", | |
"{given_name}{surname_name}", | |
"{given_name}.{surname_name}", | |
"{given_initial}{surname_name}", | |
"{given_initial}.{surname_name}", | |
"{given_name}{surname_initial}", | |
"{given_name}.{surname_initial}", | |
"{given_initial}{surname_initial}", | |
"{given_initial}.{surname_initial}", | |
"{surname_name}{given_name}", | |
"{surname_name}.{given_name}", | |
"{surname_name}{given_initial}", | |
"{surname_name}.{given_initial}", | |
"{surname_initial}{given_name}", | |
"{surname_initial}.{given_name}", | |
"{surname_initial}{given_initial}", | |
"{surname_initial}.{given_initial}", | |
"{given_initial}{middle_initial}{surname_name}", | |
"{given_initial}{middle_initial}.{surname_name}", | |
"{given_name}{middle_initial}{surname_name}", | |
"{given_name}.{middle_initial}.{surname_name}", | |
"{given_name}{middle_name}{surname_name}", | |
"{given_name}.{middle_name}.{surname_name}", | |
"{given_name}-{surname_name}", | |
"{given_initial}-{surname_name}", | |
"{given_name}-{surname_initial}", | |
"{given_initial}-{surname_initial}", | |
"{surname_name}-{given_name}", | |
"{surname_name}-{given_initial}", | |
"{surname_initial}-{given_name}", | |
"{surname_initial}-{given_initial}", | |
"{given_initial}{middle_initial}-{surname_name}", | |
"{given_name}-{middle_initial}-{surname_name}", | |
"{given_name}-{middle_name}-{surname_name}", | |
"{given_name}_{surname_name}", | |
"{given_initial}_{surname_name}", | |
"{given_name}_{surname_initial}", | |
"{given_initial}_{surname_initial}", | |
"{surname_name}_{given_name}", | |
"{surname_name}_{given_initial}", | |
"{surname_initial}_{given_name}", | |
"{surname_initial}_{given_initial}", | |
"{given_initial}{middle_initial}_{surname_name}", | |
"{given_name}_{middle_initial}_{surname_name}", | |
"{given_name}_{middle_name}_{surname_name}" | |
] | |
given_name = composite_name["given"].lower() | |
given_initial = given_name[:1] | |
surname_name = "" | |
if len(composite_name["surnames"]) > 0: | |
surname_name = composite_name["surnames"][-1] | |
surname_initial = surname_name[:1] | |
middle_name = "" | |
if len(composite_name["middle"]) > 0: | |
middle_name = composite_name["middle"][0] | |
middle_initial = middle_name[:1] | |
possible_emails = [] | |
for combo in combos: | |
possible_email = combo.format( | |
given_name=given_name, | |
given_initial=given_initial, | |
middle_name=middle_name, | |
middle_initial=middle_initial, | |
surname_name=surname_name, | |
surname_initial=surname_initial | |
) | |
if ".." not in possible_email and "--" not in possible_email and "__" not in possible_email: | |
possible_email = "{}@{}".format(possible_email, domain) | |
possible_emails.append(possible_email) | |
url = "https://api.neverbounce.com/v4/single/check" | |
email_data = [] | |
for possible_email in possible_emails: | |
params = {"key": api_key, "email": possible_email} | |
response = requests.post(url, params) | |
json_response = response.json() | |
is_valid = False | |
if json_response["status"] == "success": | |
is_valid = json_response["result"] == "valid" | |
email_data.append({ | |
"email": possible_email, | |
"is_valid": is_valid | |
}) | |
print(email_data) | |
req = urllib.request.Request( | |
'https://api.fullcontact.com/v3/person.enrich' | |
) | |
req.add_header( | |
'Authorization', | |
'Bearer {}'.format(fullcontact_api_key) | |
) | |
for email_datum in email_data: | |
full_name = "" | |
params = json.dumps( | |
{"email": email_datum["email"]} | |
).encode("utf-8") | |
response = urllib.request.urlopen(req, data) | |
response_body = response.read() | |
json_response = json.loads(response_body.decode("utf-8")) | |
name_info = json_response["details"]["name"] | |
email_datum["given_name"] = name_info["given"] | |
email_datum["surname"] = name_info["family"] | |
desired_email = "" | |
for email_datum in email_data: | |
last_surname = composite_name["surnames"][ | |
len(composite_name["surnames"]) - 1 | |
] | |
if email_datum["is_valid"] is True and \ | |
email_datum["given_name"] == composite_name["given"] and \ | |
last_surname in email_datum["surname"]: | |
desired_email = email_datum["email"] | |
break | |
print(desired_email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment