Created
July 2, 2012 12:41
-
-
Save takuya/3033059 to your computer and use it in GitHub Desktop.
Lismoのアドレス帳をVCFに変換する ref: http://qiita.com/items/a0c921fbef3f94bbbe24
This file contains hidden or 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 'rubygems' | |
require 'kconv' | |
require 'csv' | |
require 'vpim' | |
$KCODE='u' | |
#Authors:: takuya_1st | |
#Copyright:: @takuya_1st | |
#License:: GPL | |
class CSV | |
## CSVファイルを読み込んで一行目を見出し行として、全部をハッシュに読み込む | |
def CSV.csv_to_hash(filename) | |
#一行目がヘッダのCSVファイル | |
f = CSV.open(filename, "r") | |
header = f.shift | |
f.map{|e| | |
Hash[*header.zip(e).flatten] | |
} | |
end | |
end | |
class LismoAddressBook | |
def csv_to_vcf_array(filename) | |
info_list = csv_to_info(filename) | |
vcf_list = info_list.map{|info,k| | |
info_to_vcf(info) | |
} | |
end | |
def csv_into_vcf_files(filename) | |
list = csv_to_info(filename) | |
list.each{|info| | |
save_vcf(info) | |
} | |
end | |
def csv_to_info(csv_file) | |
hash_list = CSV.csv_to_hash(csv_file) | |
hash_list.map{|e| | |
hash_to_info(e) | |
} | |
end | |
def hash_to_info(hash) | |
info ={}; | |
info['f_name' ] = hash["名前"] | |
info['l_name' ] = "" if false | |
info['kana' ] = hash["よみ"] | |
info['email' ] = [hash["Eメール1"], hash["Eメール2"],hash["Eメール3"],hash["Eメール4"],hash["Eメール5"],].select{|e| e} | |
info['tel' ] = [hash["電話番号1"],hash["電話番号2"], hash["電話番号3"],hash["電話番号4"],hash["電話番号5"], ].select{|e| e} | |
info['home_addr'] = "#{hash["郵便番号"]} #{hash["住所"]}" if hash["住所"] | |
info['birthday' ] = hash["誕生日"] if hash["誕生日"] | |
info['memo' ] = "#{hash["メモ"]}" if hash["メモ"] | |
info.delete("tel") if info["tel"].size < 1 | |
info.delete("email") if info["email"].size < 1 | |
return info | |
end | |
def info_to_vcf(info) | |
vcard = Vpim::Vcard.create | |
vcard << Vpim::DirectoryInfo::Field.create('N;CHARSET=utf-8' , "#{info['f_name']};#{info['l_name']};;;" ) | |
vcard << Vpim::DirectoryInfo::Field.create('FN;CHARSET=utf-8' , "#{info['l_name']}#{info['l_name']}" ) | |
vcard << Vpim::DirectoryInfo::Field.create('X-PHONETIC-FIRST-NAME' , "#{info['kana']}" ) | |
info["email"].each{|mail| | |
vcard << Vpim::DirectoryInfo::Field.create('EMAIL;INTERNET' , "#{mail}" ) | |
} if info["email"] | |
info["tel"].each{|num| | |
vcard << Vpim::DirectoryInfo::Field.create('TEL;CELL;VOICE' , "#{num}" ) | |
} if info["tel"] | |
vcard << Vpim::DirectoryInfo::Field.create('ADR;HOME;CHARSET=utf-8' , "#{info['home_addr']}" ) | |
vcard << Vpim::DirectoryInfo::Field.create('BDAY;value=date' , "#{info['birthday']}" ) | |
vcard << Vpim::DirectoryInfo::Field.create('NOTE;CHARSET=utf-8' , "#{info['memo']}" ) | |
vcard.to_s.gsub( /¥r?¥n/, "") | |
end | |
def save_vcf(info) | |
require 'digest/sha1' | |
vcf_string = info_to_vcf(info) | |
file_name = "#{info["f_name"]}#{info["l_name"]}.vcf" | |
file_name = "#{Digest::SHA1.hexdigest(vcf_string)}.vcf" if file_name.strip == ".vcf" | |
open("#{info["f_name"]}#{info["l_name"]}.vcf", "w" ){|f| | |
f.write vcf_string | |
} | |
puts "#{file_name} saved." | |
end | |
end | |
converter = LismoAddressBook.new | |
converter.csv_into_vcf_files("./address.csv") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment