Skip to content

Instantly share code, notes, and snippets.

@wtachau
Created June 8, 2015 22:47
Show Gist options
  • Select an option

  • Save wtachau/20f6bca218fbadc967ce to your computer and use it in GitHub Desktop.

Select an option

Save wtachau/20f6bca218fbadc967ce to your computer and use it in GitHub Desktop.
email script
import sys
import csv
import json
import pycurl
from StringIO import StringIO
import urllib
# get input file
if len(sys.argv) < 2:
print "Usage: $ python email.py <inputCSV>"
exit(1)
inputFile = sys.argv[1]
# accumulate rows to write to file
rowsToWrite = []
# Loop through csv grabbing names and domains
with open(inputFile, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(spamreader)
for row in spamreader:
first = row[3].lower()
last = row[4].strip().lower()
domain = row[5]
print "Checking emails for %s %s..." % (first.capitalize(), last.capitalize())
# Generate all permutations given first and last name
permutations = []
def addPermutation(username):
permutations.append(urllib.quote_plus("%s@%s" % (username, domain)))
addPermutation(first) # {first}
addPermutation(last) # {last}
addPermutation(first+last) # {first}{last}
addPermutation(first+"."+last) # {first}.{last}
addPermutation(first[0]+last) # {f}{last}
addPermutation(first[0]+"."+last) # {f}.{last}
# Check each permutation using cURL against LinkedIn API
validEmails = []
for permutation in permutations:
buffer = StringIO()
c = pycurl.Curl()
url = 'https://api.linkedin.com/v1/people/email=%s:(first-name,last-name,headline,location,distance,positions,twitter-accounts,im-accounts,phone-numbers,member-url-resources,picture-urls::(original),site-standard-profile-request,public-profile-url,relation-to-viewer:(connections:(person:(first-name,last-name,headline,site-standard-profile-request,picture-urls::(original)))))' % permutation
c.setopt(c.URL, url )
# exp p7gkypXnXtKsgdnCh6L6_hbk5OTxWS4kk3In
# live q1IZeXkdme5e1sSxMdXQr3OP2JYyFVSJQBakF
header = [ 'oauth_token: q1IZeXkdme5e1SxMdXQr3OP2JYyFVSJQBakF', 'x-li-format: json' ]
c.setopt(c.HTTPHEADER, header)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = json.loads(buffer.getvalue())
# check that response was successful (and that token is valid)
if "status" in body:
if body["status"] == 401:
print "ERROR: EXPIRED TOKEN"
exit(1)
else:
validEmails.append(permutation)
rowsToWrite.append([first.capitalize(), last.capitalize()] + map(urllib.unquote, validEmails))
print "Writing to csv..."
with open('emails.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in rowsToWrite:
spamwriter.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment