あとで読む + GmailをiPodで読む

ということで、次の使い方を想定しています。


あとで読む → Gmailに送信 → Gmailラベル付与 → スクリプトiPod


かなり思い付きです。スクリプトも汚いですけど、一応動いてます。ちなみに動かすにはRAAに登録されているgmailerというライブラリが必要です。rubygemsでも入ります。

ここに書いてあるスクリプトを使用する場合はAt Your Own Riskで使用してください。



require 'gmailer'
require 'kconv'

name = 'username' #ユーザ名
pwd = 'password' #パスワード
notes = 'E:\\Notes\\' #ノートのフォルダ。最後に\をつけてください
folder = 'articles' #ノートフォルダの記事を保存するフォルダ
label = "atode" #Gmailのラベル

def split_body(body)
max = 3000
count = 0
tmp = []
sentence = ''
body.each do |line|
count += line.size
if count > max
tmp.push sentence
sentence = ''
count = line.size
end
sentence += line
end
tmp
end

def short_subject(subject)
str = subject.sub(/\(http.+\)/, '')
str.sub(/^\[.*?\]/, '')
end

GMailer.connect(name, pwd) do |g|
index_filename = notes + Time.now.strftime('%y%m%d_%H%M') + '.txt'
open(index_filename, 'w') do |index|
g.messages(:label=>label, :read=>false).each_msg {|msg|
subject = short_subject(msg.subject.tosjis)
body = msg.body.tosjis
body.gsub!(/<.*?>/m, '')
body.gsub!(/\t/, ' ')
body.squeeze!(" \n")
body.gsub!(/ \n/, '')
ar = split_body(body)
prefix = notes + folder + '\\' + msg.id + '_'
ar.each_with_index do |sentence, i|
fname = prefix + i.to_s + '.txt'
print "Writing #{subject}\n\tto #{fname} ... "
open(fname, 'w') do |f|
f.print "<a href=\"/#{folder}/#{msg.id}_#{(i - 1).to_s}.txt\">Previous</a>\n\n" if i > 0
f.puts sentence
f.print "\n\n<a href=\"/#{folder}/#{msg.id}_#{(i + 1).to_s}.txt\">Next</a>" if i != (ar.size - 1)
end
print "Done\n\n"
$stdout.flush
end
index.puts "<a href=\"/#{folder}/#{msg.id}_0.txt\">#{subject}</a>"
}
end
end