PRagger その2

下のエントリをポストするのに使った publish::hatena_diary_writer.rb プラグインがちょっと使いにくい。
今日の日記にしかポストできないのはとりあえずおいとくとしても,ポストするたびに下に追加ってのは1日1ファイルにしてる俺のやり方とは違う。
ここはやっぱり全面的に置き換えるのがいい。


というわけでちょっと手を入れてみた。
config["mode"] が replace なら上書き,add なら下に追記,insert なら上に挿入だ(文字化けしてるのはsvnのせいみたい)。
ついでに proxy にも対応。環境変数 http_proxy が設定されていればそれを使う。

追記:おっと。http_proxy の文字列にはスキーマも含むのか。というわけで以下のコードは差し替えた。

 Index: hatena_diary_writer.rb
 ===================================================================
 --- hatena_diary_writer.rb	(リビジョン 57)
 +++ hatena_diary_writer.rb	(作業コピー)
 @@ -1,12 +1,19 @@
  #!/usr/bin/env ruby
  # hatena_diary_writer.rb
  #
 +require 'uri'
  
  class HatenaDiaryWriter
    def initialize(id,password)
      @id = id
      @password = password
      @agent = WWW::Mechanize.new
 +    if proxy = ENV['http_proxy']
 +      proxy = URI.parse(proxy)
 +      proxy_addr = proxy.host
 +      proxy_port = proxy.port
 +      @agent.set_proxy(proxy_addr, proxy_port)
 +    end
      @diary = @agent.get("http://d.hatena.ne.jp/#{id}/")
    end
  
 @@ -21,11 +28,18 @@
      @diary_page = @agent.get(@diary_link.href)
    end
  
 -  def edit(content)
 +  def edit(content, mode)
      edit_link = @diary_page.links.text("譌・險倥r譖ク縺・.toeuc)
      edit_page = @agent.get(edit_link.href)
      edit_form = edit_page.forms.name("edit").first
 -    edit_form["body"] += content
 +    case mode
 +    when 'add'
 +      edit_form["body"] += content
 +    when 'insert'
 +      edit_form["body"] = content + edit_form["body"]
 +    when 'replace'
 +      edit_form["body"] = content
 +    end
      ok_button = edit_form.buttons.name("edit")
      @agent.submit(edit_form, ok_button)
    end
 @@ -42,5 +56,5 @@
      content << ("* "+line.title+"\n"+line.link+"\n"+line.description rescue line.to_s)
    end
    diary.login
 -  diary.edit(content.toeuc)
 +  diary.edit(content.toeuc, config['mode'])
  end


設定ファイルはこんな感じ。

 - module: stdin
   config:
     input: nothing
 - module: publish::hatena_diary_writer
   config:
     user_id: takatoh
     password: xxxxxxxx
     mode: replace


日記のファイルはコマンドラインで指定する。

 ^o^>pragger -c hatena.yaml 2007-02-24.txt
 exec plugin stdin
 exec plugin publish::hatena_diary_writer


ところで

   data.each do |line|
     content << ("* "+line.title+"\n"+line.link+"\n"+line.description rescue line.to_s)
   end

の line がどんなデータを想定してるのかわからないんですが。