Last active
July 9, 2018 21:33
-
-
Save phin3has/53fbf7ad95229fa9f9cd to your computer and use it in GitHub Desktop.
Compiles email addresses from a .csv of first and last names
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/python | |
__author__ = '@awhitehatter' | |
__version__= 1.0 | |
''' | |
WhoWho is script that builds username addresses from CSV imports. | |
example: | |
From test.csv: | |
first_name,last_name | |
bob,alice | |
./whowho.py -p fn.ln -d github.com -f test.csv | |
bob.alice@github.com | |
./whowho.py -p fi.ln -d github.com -f test.csv | |
b.alice@github.com | |
''' | |
import csv, argparse, re, sys | |
def main(): | |
#parse arguments | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-p','--pattern', action='store', type=str, required=True, help='username pattern where \(user fi, fn, li, ln\)') | |
parser.add_argument('-o', '--outfile', action='store', default=None, help='Write results to file') | |
parser.add_argument('-d', '--domain', action='store', type=str, required=True, help='Domain for username addresses') | |
parser.add_argument('-f', '--file', action='store', required=True, help='CSV file to import') | |
args = parser.parse_args() | |
pattern = args.pattern | |
outfile = args.outfile | |
domain = args.domain | |
contacts = args.file | |
contactscsv = csv.DictReader(open(contacts,'rt')) | |
# verify headers are correct | |
headers = contactscsv.reader.next() | |
hreg = re.compile(r'.*first|last\_name.*first|last\_name.*') | |
mo = hreg.search(str(headers)) | |
if bool(mo): | |
print '[*] - Correct Headers found' | |
else: | |
print 'CSV must have headers of first_name, last_name\n' | |
sys.exit(1) | |
# reopen csv because python has already read the first line to confirm headers | |
contactscsv = csv.DictReader(open(contacts,'rt')) | |
# print out username addresses | |
for row in contactscsv: | |
fname = row['first_name'].lower() | |
finit = row['first_name'][:1].lower() | |
lname= row['last_name'].lower() | |
linit = row['last_name'][:1].lower() | |
# match and replacemai | |
username = pattern | |
username = re.sub(r'fn', fname, username) | |
username = re.sub(r'fi', finit, username) | |
username = re.sub(r'ln', lname, username) | |
username = re.sub(r'li', linit, username) | |
# print the results | |
if outfile is None: | |
print username + '@' + domain | |
else: | |
f = open(outfile, 'a') | |
f.write('\n' + username + '@' + domain) | |
print username + '@' + domain | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment