Created
August 8, 2012 00:40
-
-
Save tekei/3290956 to your computer and use it in GitHub Desktop.
icloudに格納したメモ帳をテキストファイルとしてダウンロードする
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
# -*- 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(" ", "") | |
end | |
} |
現時点でのメモの保存のされ方。
・ HTML形式でbody部分のみの形式で保存されている
・ 1行毎に
~で囲まれている。空行は、
・ 行先頭の場合、半角スペース1個は" "と出力されている
→ 変換時は「 」をスペースにすると位置づれをおこすため、単純な削除
・ 行先頭の場合、半角スペース1個は" "と出力されている
→ 変換時は「 」をスペースにすると位置づれをおこすため、単純な削除
IMAPについて
・IMAPの"Notes"フォルダに保存
・削除したメモも格納されて残っている
→ 間違って消した物も取り出せるが、どの程度昔のものまで保存されているか不明。
→ 自分の環境では、少なくとも1ヶ月前のものは残っていた。
上記2, 3行目うまく書けてないので、修正
・ 1行毎に<div>~</div>で囲まれている。空行は、 <div><br></div>
・ 行先頭の場合、半角スペース1個は" "と出力されている
実行する際のメモ
- 最初に gem install mail してください
- windows上で実行する事を念頭にしています。
もし、windows上で無い場合は、24行目を削除して実行してください。 - 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
変更
→ keys指定を追加して削除済みメモは取得しないように変更
(自分のマシンでsanitaizeを利用すると、core dumpするため自前処理で対処)