keitai.rb変更

クリップボードの中身を自分の携帯電話に送るスクリプトid:tamago_girai:20070720)で送信できなくなってしまったので原因を調べて修正した。おまけにExerbでexe化。以下はスクリプトのソース。

#!/usr/bin/env ruby
# keitai.rb
# -*- Mode: Ruby; coding: shift_jis -*-
#
#
#
#
$KCODE = "SJIS"

require 'kconv'
require 'fileutils'
require "win32/clipboard"
include Win32
require 'rubygems'
require 'mechanize'

@DIRECTORY = "mail"                             # あらかじめ作成しておく
@TO = 'fugafugahogehoge@hogomo.ne.jp'           # 自分のケータイアドレス
@GMAILUSERNAME = 'fugafugahogehoge'
@GMAILPASSWORD = 'fugafugagehogeho'

def make_filename()
   return Time.now.strftime("%Y_%m_%d_%H_%M_%S.txt")
end

def write_file(data)
   path = @DIRECTORY + '/' + make_filename()
   open(path, "w+") do |output|
      output.print data
   end
   return path
end

def send_gmail(filename)

   body = open(filename).read.to_a
   subject = body.shift.chomp
   body = body.join

   agent = WWW::Mechanize.new
   agent.user_agent_alias = 'Windows Mozilla'

   gmail_page = agent.get("http://mail.google.com/mail/h/")

   login_form = gmail_page.forms.first

   login_form['Email'] = @GMAILUSERNAME
   login_form['Passwd'] = @GMAILPASSWORD

   agent.submit(login_form)
   gmail_page = agent.get('http://mail.google.com/mail/?ui=html&zy=a')      # 2007 12/2 追加
   gmail_page = agent.get('http://mail.google.com/mail/h/?v=b&pv=tl&cs=b')
   mail_form = gmail_page.forms.with.name('f').first  #  "f"という名前のフォームを探す
   if mail_form then
      mail_form['to'] = @TO
      mail_form['cc'] = nil
      mail_form['bcc'] = nil
      mail_form['subject'] = subject.toutf8
      mail_form['body'] = body.toutf8
      send_button = mail_form.buttons.name('nvp_bu_send')
      agent.submit(mail_form, send_button)
   else
      puts "Error : Cant' send gmail."
   end
end

file = write_file(Clipboard.data)
send_gmail(file)
# keitai.rb ends here.

http://mail.google.com/mail/?ui=html&zy=a というURLに移行後、http://mail.google.com/mail/h/?v=b&pv=tl&cs=b でメール作成の画面に行かないとエラーになるようなので主にその点を修正。他にもExerb化するために少し変更したけど、機能は前のと全く同じ。(mailディレクトリが無いとエラーになる等、ちょっとしょぼい)