Last active
February 7, 2016 16:20
-
-
Save alexg0/a02e167e5bff500fd5f0 to your computer and use it in GitHub Desktop.
converts Outlook style dump of contacts (only tested with Google) into format suitable for importing into RingCentral.
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 ruby | |
# Copyright (c) 2016, Alexander Goldstein | |
# Permission to use, copy, modify, and/or distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
# | |
# This converts Outlook style dump of contacts (only tested with Google) into format | |
# suitable for importing into RingCentral. | |
# | |
require 'rubygems' | |
require 'csv' | |
require 'set' | |
# require 'byebug' | |
# require 'pry' | |
# require 'pry-byebug' | |
def usage(message) | |
puts message if message | |
puts "$0 filename.csv [output.csv]" | |
puts "" | |
puts "if name of the output csv is ommited output name is based on the input name" | |
exit 1 | |
end | |
OUTPUT_INFIX = '.ringcentral' | |
FILE_EXT = '.csv' | |
input_file = ARGV[0] or usage "no argument given" | |
output_file = ARGV[1] | |
output_file ||= File.join(File.dirname(input_file), | |
File.basename(input_file, FILE_EXT) + | |
OUTPUT_INFIX + FILE_EXT) | |
OUTPUT_FIELDS = [ | |
"First Name", | |
"Last Name", | |
"NickName", | |
"Job Title", | |
"Company", | |
"E-mail Address", | |
"E-mail 2 Address", | |
"E-mail 3 Address", | |
"Home Phone", | |
"Business Phone", | |
"Mobile Phone", | |
"Business Fax", | |
"Home Phone 2", | |
"Business Phone 2", | |
"Company Main Phone", | |
"Assistant's Phone", | |
"Car Phone", | |
"Other Phone", | |
"Other Fax", | |
"Callback", | |
"Home Fax", | |
"Pager", | |
"Home Street", | |
"Home City", | |
"Home State", | |
"Home Zip Code", | |
"Business Street", | |
"Business City", | |
"Business State", | |
"Business Zip Code", | |
"Other Street", | |
"Other City", | |
"Other State", | |
"Other Zip Code", | |
"Birthday" | |
] | |
OUTPUT_FIELDS_SET = Set.new(OUTPUT_FIELDS) | |
CSV.open(output_file, "wb", | |
:headers => OUTPUT_FIELDS + ["Name"], | |
:write_headers => true) do |out_csv| | |
CSV.foreach(input_file, :headers => true) do |row| | |
row.delete_if do |h,v| | |
!OUTPUT_FIELDS_SET.include? h | |
end | |
fn, ln, co = row["First Name"], row["Last Name"], row["Company"] | |
row["Name"] = [fn, ln, co && "[%s]" % co].compact.join(' ') | |
has_number = row.any? {|h,v| /Phone/ =~ h && v } | |
has_name = [fn, ln, co].any? | |
keep = has_number && has_name | |
# binding.pry | |
out_csv << row if keep | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment