Created
June 9, 2016 17:18
-
-
Save amitvasudev/898f656bd9b77fceba295bbe704ada6a to your computer and use it in GitHub Desktop.
CombinedLookup_Enrichment_EmailandNames
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
require 'csv' | |
require 'pmap' | |
require 'clearbit' | |
Clearbit.key = ENV['CLEARBIT_KEY'] | |
def flatten_hash(hash) | |
hash.each_with_object({}) do |(k, v), h| | |
if v.is_a? Hash | |
flatten_hash(v).map do |h_k, h_v| | |
h["#{k}.#{h_k}".to_sym] = h_v | |
end | |
else | |
h[k] = v | |
end | |
end | |
end | |
puts "which csv?" | |
file = gets.chomp | |
Dir.glob(file) do |theCSV| | |
puts "Working on: #{theCSV}..." | |
@csv_name = File.basename(theCSV).gsub('.csv', '') | |
@csv_array = [] | |
list = CSV.foreach(theCSV, {:encoding => "ISO-8859-15:UTF-8", :headers => true, :header_converters => :symbol, :converters => [:all]}) do |row| | |
@csv_array << row.to_hash | |
end | |
end | |
cb_results = [] | |
puts "#{@csv_array.count} emails to process" | |
i = 0 | |
@csv_array.each do |row| | |
email = row[:email] | |
fname = row[:first_name] | |
lname = row[:last_name] | |
#comp = row[:company] | |
begin | |
response = Clearbit::Enrichment.find(email: email, stream: true, given_name: fname, family_name: lname)#, company: comp) | |
if response["person"] && response["company"] | |
@person_hash = flatten_hash(response["person"]) | |
@person_new_hash = {} | |
@person_hash.keys.each do |key| | |
@person_hash[key] = nil | |
@person_hash["person.#{key}"] = @person_hash.delete key | |
end | |
@company_hash = flatten_hash(response["company"]) | |
@company_hash.keys.each do |key| | |
@company_hash[key] = nil | |
@company_hash["company.#{key}"] = @company_hash.delete key | |
end | |
end | |
break if @person_hash | |
rescue | |
p email | |
p $! | |
break if @person_hash | |
end | |
end | |
@csv_array.peach(5) do |row| | |
email = row[:email] | |
fname = row[:first_name] | |
lname = row[:last_name] | |
#comp = row[:company] | |
i += 1 | |
begin | |
puts "#{i}: #{email}" | |
response = Clearbit::Enrichment.find(email: email, stream: true, given_name: fname, family_name: lname)#, company: comp) | |
if response["person"] | |
flat_person = flatten_hash(response["person"]) | |
flat_person.keys.each do |key| | |
flat_person["person.#{key}"] = flat_person.delete key | |
end | |
else | |
flat_person = @person_hash | |
end | |
if response["company"] | |
flat_company = flatten_hash(response["company"]) | |
flat_company.keys.each do |key| | |
flat_company["company.#{key}"] = flat_company.delete key | |
end | |
else | |
flat_company = @company_hash | |
end | |
hash = {:email => email} | |
hash.merge!(flat_person) | |
hash.merge!(flat_company) | |
cb_results << hash | |
rescue | |
puts "#{i}: #{email}" | |
puts "Error: #{$!}" | |
cb_results << {:email => email} | |
next | |
end | |
end | |
header_hash = {:email => nil} | |
header_hash.merge!(@person_hash) | |
header_hash.merge!(@company_hash) | |
CSV.open("#{@csv_name}_output.csv", "w") do |csv| | |
csv << header_hash.keys | |
cb_results.each {|x| csv << x.values} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment