Importar endereços do Claws no Evolution (entre outros)

Publicado por Felipe Rafailov (última atualização em 24/11/2009)

[ Hits: 4.110 ]

Download xml2vcard.rb




Este script em Ruby lê o arquivo XML de endereços do Claws-Mail e os escreve em formato vCard, que pode ser facilmente importado pelo Evolution ou outro programa de e-mail que aceite este formato.

Serve também de estudo para manipulação de arquivos XML e vCard com Ruby.

Requer o "gem" vpim, que pode ser encontrado em: http://vpim.rubyforge.org/ (ou pelo comando "gem install vpim").

  



Esconder código-fonte

#!/usr/bin/ruby

require 'rubygems'
require 'rexml/document'

# O 'gem' vpim pode ser encontrado em http://vpim.rubyforge.org/
# ou instalado através do comando 'gem install vpim' (como root).
require 'vpim/vcard'

include REXML

file = File.new(ARGV.first)
doc = Document.new(file)
root = doc.root

root.each_element('//person') do |person|
    card = Vpim::Vcard::Maker.make2 do |maker|
        maker.add_name do |name|
            if person.attributes['first-name'].empty?
                name.given = person.attributes['cn'].split.first
            else
                name.given = person.attributes['first-name']
            end
            if person.attributes['last-name'].empty?
                name.family = person.attributes['cn'].split.last
            else
                name.family = person.attributes['last-name']
            end
            name.fullname = person.attributes['cn']
        end

        maker.nickname = person.attributes['nick-name']

        person.children[1].each_element do |emailaddr|
            maker.add_email(emailaddr.attributes['email'])
        end

        person.children[3].each_element do |property|
            case property.attributes['name']
            when /^endereço( do trabalho)?$/ then
                maker.add_addr do |addr|
                    addr.location = $1 ? 'work' : 'home'
                    addr.preferred = true unless $1
                    addr.street = property.text
                end
            when "data de nascimento" then
                maker.birthday = Time.parse(property.text)
            when "fax" then
                maker.add_tel(property.text) do |t|
                    t.location = 'work'
                    t.capability = 'fax'
                end
            when "organização" then
                # TODO: achar uma maneira de colocar o nome da empresa no Vcard
            when "sítio na Internet"
                # TODO: achar uma maneira de colocar o endereço de Internat da empresa no Vcard
            when /^telefone( celular| do trabalho)?/
                maker.add_tel(property.text) do |tel|
                    if $1 == " celular"
                       tel.location = 'mobile'
                    elsif $1 == " do trabalho"
                       tel.location = 'work'
                    else
                       tel.location = 'home'
                    end
                end
            end
        end
    end
    puts card
end

Scripts recomendados

Uso de if em Ruby (2)

Exportar endereços do Evolution para vCard

Algoritmo de Fatoração de Fermat (FFA) em Ruby

Uso simples de if e else em Ruby

Agenda telefônica em Ruby que grava os dados em um txt


  

Comentários
[1] Comentário enviado por glaucoperucchi em 06/12/2010 - 17:56h

Boa tarde!

Pode me ajudar?
Esta acontecendo este erro

./xml2vcard.rb:12:in `initialize': can't convert nil into String (TypeError)
from ./xml2vcard.rb:12:in `new'
from ./xml2vcard.rb:12


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts