Skip to content

Instantly share code, notes, and snippets.

@tekei
Created August 8, 2012 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tekei/3290956 to your computer and use it in GitHub Desktop.
Save tekei/3290956 to your computer and use it in GitHub Desktop.
icloudに格納したメモ帳をテキストファイルとしてダウンロードする
# -*- coding: utf-8 -*-
# icloudに格納したメモ帳をテキストファイルとしてダウンロードする
# ファイル名は、[更新日付-タイトル.txt]
require 'mail'
require 'cgi'
account = '(ユーザ名)@me.com'
pass = '(パスワード)'
Mail.defaults do
retriever_method :imap, :address => "imap.mail.me.com",
:port => 993,
:user_name => account,
:password => pass,
:enable_ssl => true
end
c_before = "\\/:*?\"<>|"
c_after = "\/:*?"<>|"
Mail.find(:mailbox => 'Notes', :keys => 'undeleted', :count => 500) { |email|
file_name = "#{email.date.strftime("%Y%m%d")}-#{email.subject}.txt"
file_name.gsub!(/([#{c_before}])/) { c_after[c_before.index($1)] }
file_name.encode!("windows-31j", :undef => :replace, :replace => '#')
open(file_name, "w:utf-8") do |f|
body = email.body.decoded.encode("utf-8", email.charset)
body.gsub!('</div>', "\n")
body.gsub!(/<[^>]*>/, "")
f << CGI.unescapeHTML(body).gsub("&nbsp;", "")
end
}
@tekei
Copy link
Author

tekei commented Aug 8, 2012

変更

  1. mikel/mailパッケージのfindはデフォルト SELECT ALLなので、削除済みメモも取得していまう。
    → keys指定を追加して削除済みメモは取得しないように変更
  2. メモの中身はHTMLで出力されるため、文字参照の変換追加
    (自分のマシンでsanitaizeを利用すると、core dumpするため自前処理で対処)

@tekei
Copy link
Author

tekei commented Aug 8, 2012

現時点でのメモの保存のされ方。
・ HTML形式でbody部分のみの形式で保存されている
・ 1行毎に

~で囲まれている。空行は、


・ 行先頭の場合、半角スペース1個は"  "と出力されている
  → 変換時は「 」をスペースにすると位置づれをおこすため、単純な削除

IMAPについて
・IMAPの"Notes"フォルダに保存
・削除したメモも格納されて残っている
→ 間違って消した物も取り出せるが、どの程度昔のものまで保存されているか不明。
→ 自分の環境では、少なくとも1ヶ月前のものは残っていた。

@tekei
Copy link
Author

tekei commented Aug 8, 2012

上記2, 3行目うまく書けてないので、修正
・ 1行毎に<div>~</div>で囲まれている。空行は、 <div><br></div>
・ 行先頭の場合、半角スペース1個は"&nbsp; "と出力されている

@tekei
Copy link
Author

tekei commented Aug 17, 2012

実行する際のメモ

  1. 最初に gem install mail してください
  2. windows上で実行する事を念頭にしています。
     もし、windows上で無い場合は、24行目を削除して実行してください。
  3. accountには、appleIDではなくme.comのメールアドレスを入力してください。
     「me.comのメールアドレス」はhttps://www.icloud.com/#mailから環境設定→アカウントで確認できます。
     passは、appleIDのパスワードと同じです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment