gmailからhowmへもっていく
ruby1.9はよくわからん。。。
なんかまだまだ凶暴な気がする。
cronで
#! /usr/bin/ruby # -*- mode:ruby;coding:utf-8 -*- require 'rubygems' require 'net/imap' require 'yaml' require 'kconv' require 'pp' puts "ruby 1.8世代しかみとめませn" $KCODE = "utf8" $keyword = ARGV[0] $yamlpath = "/path/to/yaml" $filename = "/path/to/file" class GmailHundle def getInitConfig() config = YAML.load_file($yamlpath) username = config["user"] password = config["pass"] return { :username => username, :password => password, :port => 993, :usessl => true, :server => "imap.gmail.com" } end def connImap(conf) imap = Net::IMAP.new(conf[:server],conf[:port],conf[:usessl]) imap.login(conf[:username],conf[:password]) return imap end #使ってみたかっただけのyield def yiel(imap,ids) imap.fetch(ids,["ENVELOPE","BODY","BODY[TEXT]","UID"]).each {|mail| subt = mail.attr["ENVELOPE"]["subject"].toutf8 ddat = mail.attr["ENVELOPE"]["date"].toutf8 yield({:subject => subt, :date => ddat, :body => mail.attr["BODY[TEXT]"].toutf8, :id => mail.attr["UID"].to_s}) } end def getKakeibo(query) mails = Array.new imap = connImap(getInitConfig()) imap.select(Net::IMAP.encode_utf7("[Gmail]/All Mail")) ids = imap.search(query,"utf-8") yiel(imap,ids) { |f| mails << f } return mails.reverse end end #ここでローカルにあるimapHowmファイルをよんできてmessageIDで新しいものだけを持ってくる class FileDB @localfile @mails @mails2 def initialize(mails) @localfile = Array.new @mails2 = Array.new @mails = mails #ファイル読んでくる File.open($filename,"r"){|io| io.each{|line| @localfile << line.chomp if(/^Gmail2howm:id:/ =~ line) } } pp @localfile getUniq() end #mails2に新規に書き込むべきデータを入れていく def getUniq #ファイルと重複していないデータをmail2にためていく @mails.each{|m| str = "Gmail2local:id:" + m[:id] if(@localfile.index(str)) # puts "データ存在" # pp m[:subject] else # puts "データ不存在" @mails2 << m end } end def mapAndEmit #map and emit count = -1 File.open($filename,"a"){|io| count = count + 1 @mails2.each{|mail| count = count + 1 io.puts "" io.puts "=" + mail[:subject].chomp io.puts "date:" + mail[:date] io.puts "Gmail2local:id:" + mail[:id] io.puts mail[:body] io.puts "" } } return count end #めも: yield({:subject => subt, # :date => ddat, # :body => mail.attr["BODY[TEXT]"].toutf8, # :id => mail.attr["ENVELOPE"]["message_id"]}) # } end ########################################################## ##main的 ma = GmailHundle.new mails = ma.getKakeibo(["SUBJECT",$keyword]) hu = FileDB.new(mails) count = hu.mapAndEmit(); =begin #notify meowおそい require "meow" meep = Meow.new('Gmail2howm') meep.notify('mail取り込み処理', count.to_s + '件を新規投入しました。') =end #macなら system("growlnotify","-m" ,"Gmail2howm:"+ count.to_s + "件新規投入しました") __END__ usage: 1.configfileを用意 user: "アカウント@gmail.com" pass: "パスワード" なファイル名「config.yaml」を用意してスクリプトと同じフォルダに 2.ruby スクリプト名 > 出力ファイル でOK。 メモ: ・メールボックス INBOXの他に [Gmail]/All Mail=>All Mail てな具合にINBOX以外にも進めること。 wlの.folderを参考にした。 ・検索 ids = imap.search(["SUBJECT",('key')],"utf-8") てな具合に結構うれしい感じに検索が効く usage: chmodしたら scriptname.rb keyword でOK