Skip to content

Instantly share code, notes, and snippets.

@yasuoza
Last active August 29, 2015 14:06
Show Gist options
  • Save yasuoza/794d17a4f99fd0f27be3 to your computer and use it in GitHub Desktop.
Save yasuoza/794d17a4f99fd0f27be3 to your computer and use it in GitHub Desktop.
Convert exported csv to vcf card
require 'csv'
input = ARGV[0] || 'exported_contacts.CSV'
output = "#{Time.now.strftime('%Y%m%d-%H%M%S')}.vcf"
groups = {
'グループなし' => 0,
}
File.open(output, 'w:cp932') do |file|
CSV.foreach(input, encoding: 'cp932', headers: :first_row) do |row|
# docomo headers
# => 名前,カナメイ,電話1,電話2,メール1,メール2,登録No.,グループ,住所,メモ,誕生日,血液型
name, kana, tel1, tel2, mail1, mail2, _, group = row.fields
line = $. - 1
groups[group] = groups.keys.count unless groups.has_key?(group)
vcard = <<CARD
BEGIN:VCARD
VERSION:3.0
PRODID:SUMIMOTO
N;CHARSET=SHIFT_JIS:#{name};;;;
SOUND;CHARSET=SHIFT_JIS;X-IRMC-N:#{kana};;;;
CARD
vcard += "TEL;HOME;CELL:#{tel1}\n" if tel1
vcard += "TEL;HOME;VOICE:#{tel2}\n" if tel2
vcard += "EMAIL;INTERNET:#{mail1}\n" if mail1
vcard += "EMAIL;INTERNET:#{mail2}\n" if mail2
vcard += <<CARD
X-CLASS:PUBLIC
NOTE:
X-NO:#{line}
X-GNO:#{groups[group]}
X-GN;CHARSET=SHIFT_JIS:#{group}
X-LAMP:8080
X-SH-CATEGORIES:BACKUP
END:VCARD
CARD
file.puts vcard.encode(crlf_newline: true)
end
end
require 'csv'
input = ARGV[0] || '201409291630.CSV'
output = "#{Time.now.strftime('%Y%m%d-%H%M%S')}.vcf"
groups = {
'グループなし' => 0,
}
File.open(output, 'w:cp932') do |file|
CSV.foreach(input, encoding: 'cp932', headers: :first_row) do |row|
# docomo headers
# => 名前,カナメイ,電話1,電話2,メール1,メール2,登録No.,グループ,住所,メモ,誕生日,血液型
name, kana, tel1, tel2, mail1, mail2, _, group = row.fields
line = $. - 1
groups[group] = groups.keys.count unless groups.has_key?(group)
vcard = <<CARD
BEGIN:VCARD
VERSION:2.1
PRODID:SUMIMOTO
N;CHARSET=SHIFT_JIS:#{name};;;
SOUND;X-IRMC-N;CHARSET=SHIFT_JIS:#{kana};;;;
CARD
vcard += "TEL;HOME;VOICE:#{tel1}\n" if tel1
vcard += "TEL;HOME;CELL:#{tel2}\n" if tel2
vcard += "EMAIL;INTERNET:#{mail1}\n" if mail1
vcard += "EMAIL;INTERNET:#{mail2}\n" if mail2
encoded_group_name = [group].pack('M').gsub(/\n/, "\r\n")
vcard += <<CARD
X-CLASS:PUBLIC
NOTE:
X-NO:#{line}
X-GNO:#{groups[group]}
X-GN;CHARSET=SHIFT_JIS;ENCODING=QUOTED-PRINTABLE:#{encoded_group_name}
X-GNO:#{groups[group]}
X-SH-CATEGORIES:BACKUP
END:VCARD
CARD
file.puts vcard.encode(crlf_newline: true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment