はてダラ(ruby)をクッキー使うように改造した
Mechanize便利だな。
以下パッチ。(1/16 1時頃 ちょっと修正)
連投防止に、editページを日付引数付きで開く変更もまじってる。
あーもしかしたら編集時のあれこれは日記モードかブログモードかで変わってくるのかな。ブログモードでは駄目な変更なのかもしれない。あとログインが失敗した時のエラー処理もなんもしてない。失敗すると何が起こるか不明。
Index: hdw.rb =================================================================== RCS file: /cvsroot/hatenadiary/HatenaDiaryWriter/hdw.rb,v retrieving revision 1.8 diff -u -r1.8 hdw.rb --- hdw.rb 5 Dec 2007 20:41:10 -0000 1.8 +++ hdw.rb 15 Jan 2008 15:46:36 -0000 @@ -55,8 +55,19 @@ f1.date <=> f2.date end - writer = HatenaDiaryWriter.new(conf.id, conf.passwd, conf.group) - writer.login + writer = HatenaDiaryWriter.new(conf.id, conf.passwd, conf.group, + conf.cookie_file) + + unless writer.login_by_cookie + log.info("login by cookie failed") + if writer.login + log.info("login by password ok") + else + log.info("login failed") + end + else + log.info("login by cookie ok") + end log.debug('login finished.') dfiles.each do |dfile| Index: lib/hatena_diary_writer.rb =================================================================== RCS file: /cvsroot/hatenadiary/HatenaDiaryWriter/lib/hatena_diary_writer.rb,v retrieving revision 1.8 diff -u -r1.8 hatena_diary_writer.rb --- lib/hatena_diary_writer.rb 27 Dec 2007 22:23:58 -0000 1.8 +++ lib/hatena_diary_writer.rb 15 Jan 2008 15:46:37 -0000 @@ -11,11 +11,29 @@ require 'yaml' class HatenaDiaryWriter - def initialize(id, passwd, group) + def initialize(id, passwd, group,cookie_file) @id = id @passwd = passwd @group = group @agent = WWW::Mechanize.new + @cookie_file = cookie_file + end + + def login_by_cookie + return false if @cookie_file.nil? + if File.exist?(@cookie_file) + @agent.cookie_jar.load(@cookie_file) + if login_check + @agent.cookie_jar.save_as(@cookie_file) + return true + end + end + false + end + + def login_check + edit_page = @agent.get(hatena_url + 'edit') + edit_page.forms.name('edit').first != nil end def login @@ -25,11 +43,17 @@ login_form = login_page.forms.first login_form['name'] = @id login_form['password'] = @passwd + login_form['persistent'] = true @agent.submit(login_form) + login_flag = login_check + if (not @cookie_file.nil?) && login_flag + @agent.cookie_jar.save_as(@cookie_file) + end + login_flag end def edit(date, content) - edit_page = @agent.get(hatena_url + 'edit') + edit_page = @agent.get(hatena_url + 'edit?date=' + date) edit_form = edit_page.forms.name('edit').first edit_form['year'], edit_form['month'], edit_form['day'] = parse_date(date) edit_form['body'] = convert_char_code(content) @@ -90,9 +114,12 @@ @group = '' @diary_dir = '.' @upload_table_file = 'touch.txt' + @cookie_file = nil end - attr_accessor :id, :passwd, :group, :diary_dir, :upload_table_file + attr_accessor :id, :passwd, :group, :diary_dir, :upload_table_file, + :cookie_file + end class DiaryInvalidFileException < Exception; end