Zėŋōfōbìå

15 gennaio 2011

Converting Mac Mail Notes to Evernote

Filed under: informatica — Zeno @ 8:12

If you want to export some notes from Mac Mail to Evernote, you could select all of them and export as plain text in notes/exported plain text.txt.
Then run the following script.
For every note it will create a evernote enex file in the notes directory. Just drag those files in the evernote application and the import is done.

require 'Time'

class Debug
  @@debug = false
  def self.trace(str)
    if @@debug then
      print str
    end 
  end
end

class Evernote
  attr_accessor :export_date, :creation_date, :update_date, :version, :xml_content, :title
  def initialize
    time=Time.new
    @export_date=Evernote.format_time(time);
    @creation_date=Evernote.format_time(time);
    @update_date=Evernote.format_time(time);
    @version="NoteConverter";
    @xml_content="<div>error converting</div>"
    @title="no title"
  end
  
  def from_eml(eml)
    @creation_date=Evernote.format_time eml.creation_date
    @update_date=Evernote.format_time eml.update_date
    @xml_content= eml.to_xml
    @title=eml.subject
    
    Debug.trace "Xml content: #{@xml_content}\n"
  end
  
  def from_text(text)
    @creation_date=Evernote.format_time text.creation_date
    @xml_content= text.to_xml
    @title=text.subject
    
    Debug.trace "Xml content: #{@xml_content}\n"
  end
  
  def self.format_time(time)
    time.utc.strftime("%Y%m%dT%H%m%SZ")
  end

  def to_s
    evernoteSchema=%Q(<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export.dtd">
<en-export export-date="#{@export_date}" application="Evernote" version="#{@version}">
<note><title>#{@title}</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
#{@xml_content}
</en-note>
]]></content><created>#{@creation_date}</created><updated>#{@update_date}</updated><note-attributes/></note>
</en-export>)
    return evernoteSchema
  end
  
  def to_file(filename)
  
  end

end

class Textnote
  attr_accessor :content, :creation_date, :payload, :subject
  
  def initialize(content)
    @content=content
    @last_line=false
    @payload=""
    content.each{ |line|
      if line =~ /^Date: (.*)/ then
        t=Time.parse($1)
        @creation_date = t
        Debug.trace "Creation date: #{@creation_date}\n"
      elsif line =~ /^Subject: (.*)/ then
          @subject = $1
          Debug.trace "Subject: #{@subject}\n"
      elsif line=~/^$/ then
        Debug.trace "empty\n"
        @last_line = true
      else
        if @last_line then
          @payload+="#{line}"
          Debug.trace "partial: #{line}\n"
        end         
      end
    } 
  end
  
  def to_xml()
    ret=""
    payload.each{|line|
      ret+="<div>#{line}</div>\n"
    }
    ret
    
  end

end

class Emlnote
  attr_accessor :content, :export_date, :creation_date, :update_date, :payload, :subject
  
  def initialize(content)
    @content=content
    @last_line=false
    @payload=""
    content.each{ |line|
      if line =~ /^X-Mail-Created-Date: (.*)/ then
        t=Time.parse($1)
        @creation_date = t
        Debug.trace "Creation date: #{@creation_date}\n"
      elsif line =~ /^Date: (.*)/ then
        t=Time.parse($1)
        @update_date = t
        Debug.trace "Update date: #{@update_date}\n"
      elsif line =~ /^Subject: (.*)/ then
          @subject = $1
          Debug.trace "Subject: #{@subject}\n"
      elsif line =~ /^Content-Type: (.*);/ then
              @content_type = $1
              Debug.trace "Content_type: #{@content_type}\n"
      elsif line=~/^$/ then
        Debug.trace "empty\n"
        @last_line = true
      else
        if @last_line then
          @payload+="#{line}"
          Debug.trace "partial: #{line}\n"
        end         
      end
    } 
  end
  
  def to_xml()
    if @content_type=="text/html"
      Debug.trace("==== html ====\n")
      return payload
    else
      ret=""
      payload.each{|line|
        ret+="<div>#{line}</div>\n"
      }
      return ret
    ret
    end
  end

end


filename="notes/exported plain text.txt"
file=File.open(filename)

text=nil
num=1
file.each{|line|
  if line=~/^\s?From: / then
    if text!=nil then
      textnote=Textnote.new(text)
      evernote=Evernote.new
      evernote.from_text(textnote)

      print "#{num}: #{evernote.title}\n"
      File.open("notes/#{num}.enex", 'w') {|f| f.write(evernote.to_s) }
      
      num+=1;
      
    end
    text=line
  else
    text+=line
  end
  
}

Annunci

Lascia un commento »

Non c'è ancora nessun commento.

RSS feed for comments on this post. TrackBack URI

Rispondi

Effettua il login con uno di questi metodi per inviare il tuo commento:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

Crea un sito o un blog gratuitamente presso WordPress.com.

%d blogger hanno fatto clic su Mi Piace per questo: