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

上記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