-
-
Save pdxmph/dc36f05a536e7a8130dab3b8bc82430b to your computer and use it in GitHub Desktop.
vcard file to org contacts
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 | |
# CAREFUL: Uses the first record in a given vcard because my use case was very simple and my data was very clean. | |
# USAGE: Save it, make it executable, run it and pipe it out to a new file and review it, e.g. | |
# $ chmod +x vcard-to-org-contacts.rb | |
# $ ./vcard-to-org-contacts.rb >> contacts.org | |
require 'vcard' | |
require 'ostruct' | |
require 'optparse' | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: vcard_to_ostruct_v4.rb [options]" | |
opts.on("-i", "--input INPUT", "Input vCard file") do |input| | |
options[:input] = input | |
end | |
end.parse! | |
if options[:input].nil? | |
puts "Please provide an input vCard file." | |
exit | |
end | |
def vcard_to_ostruct(contact) | |
OpenStruct.new( | |
name: contact['FN'].to_s, | |
email: contact['EMAIL'].to_s, | |
phone: contact['TEL'].to_s, | |
address: contact['ADR'].to_s, | |
note: contact['NOTE'].to_s, | |
company: contact['ORG'].to_s, | |
birthday: contact['BDAY'].to_s, | |
website: contact['URL'].to_s | |
) | |
end | |
input_file = options[:input] | |
vcard_data = File.read(input_file) | |
contacts = Vcard::Vcard.decode(vcard_data) | |
ostruct_contacts = contacts.map { |contact| vcard_to_ostruct(contact) } | |
def display_org_contact(contact, index) | |
puts "* #{contact.name}" | |
puts " :PROPERTIES:" | |
puts " :EMAIL: #{contact.email}" unless contact.email.empty? | |
puts " :PHONE: #{contact.phone}" unless contact.phone.empty? | |
puts " :ADDRESS: #{contact.address}" unless contact.address.empty? | |
puts " :NOTE: #{contact.note}" unless contact.note.empty? | |
puts " :COMPANY: #{contact.company}" unless contact.company.empty? | |
puts " :BIRTHDAY: #{contact.birthday}" unless contact.birthday.empty? | |
puts " :WEBSITE: #{contact.website}" unless contact.website.empty? | |
puts " :CONTACTED: 1971-01-01" | |
puts " :END:" | |
puts "" | |
end | |
ostruct_contacts.each_with_index do |contact, index| | |
display_org_contact(contact, index) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment