にたまごほうれん草アーカイブ

はてなダイアリーで書いてた「にたまごほうれん草」という日記のアーカイブです。現在は「にたまごほうれん草ブログ」を運営中です。

PRaggerでmixiに投稿するプラグイン

まだちゃんとテストも出来てないですが…。
ブログに書いた内容をmixiにも投稿したい場合に使うことを想定しています。
仕様は以下の通り。

  • RSSフィード最初の一つを投稿する一個ずつ投稿する(たくさん連続投稿するとはじかれる)
  • titleはYAMLで指定しない場合は一つ目のフィードのtitle
  • merge_feedsを指定した場合は、フィードの一覧を投稿
  • 画像投稿には未対応

YAMLの記述例はこんな感じ。

- module: RSS::load
  config:
    url: http://d.hatena.ne.jp/emergent/rss
- module: first
- module: Publish::mixi_diary_writer
  config:
    username: test@test.com
    password: ********
    title: "[Ruby][あとで消す] はてなからやってみる"

以下にソースをば。
mecanize.rb で mixi diary を自動取得する - World Wide Walkerpublish::hatena_diary_writer.rbのソースを参考にさせていただきました。ありがとうございます。

## author "emergent"
## descr  "post feed(s) to mixi diary"
##
## example <<EOE
## when posting one diary per one feed
## - module: Publish::mixi_diary_writer
##   config:
##     username: test@test.com
##     password: ********
##
## when posting merged feeds to one diary
## - module: Publish::mixi_diary_writer
##   config:
##     username: test@test.com
##     password: ********
##     title: "フィード一覧"
##     merge_feeds: 1
## EOE
## config({ :username => Field.new("username to login", String, true),
##          :password => Field.new("password to login", String, true),
##          :merge_feeds => Field.new("aggregate feeds to one diary", String),
##          :title => Field.new("title when merge_feeds is on", String) })

require 'rubygems'
require 'mechanize'
require 'kconv'

class MixiDiaryWriter

  def initialize username=nil, password=nil
    @id = nil
    @username = username
    @password = password
    @agent = WWW::Mechanize.new
  end

  def login username=@username, password=@password
    if username == nil || password == nil
      return
    else
      @username = username
      @password = password
    end
    @agent.post('http://mixi.jp/login.pl',
                { 'email' => @username,
                  'password' => @password,
                  'next_url' => '/home.pl' })
    @home_page = @agent.get('http://mixi.jp/home.pl')
  end

  def edit title, content
    if /add_diary\.pl\?id=(\d+)/ =~ @home_page.body
      @id = $1
    end

    @edit_page = @agent.get('http://mixi.jp/add_diary.pl?id='+@id)

    begin
      edit_form = @edit_page.forms.name("diary").first
      edit_form['diary_title'] = title.toeuc
      edit_form['diary_body'] = content.toeuc
      confirm_page = @agent.submit(edit_form)

      conf_form = confirm_page.forms[0] # select 'hai'
      @agent.submit(conf_form)
    rescue
      puts "unknown error"
    end
  end

  OK_TAGS = 'a|p|strong|em|u|del|blockquote'
  def striptags str
    str.gsub!(/<br.*?>/, "\n")
    str.gsub!(/<[\/]{0,1}(?!(?:(?:#{OK_TAGS})(?:\s|>)))\w+[\s]{0,1}.*?>/, '')
    str
  end
end

def mixi_diary_writer(config, data)
  title = config['title'] || data[0].title
  content = []

  if config['merge_feeds'] #.to_i == 1
    data.each {|item|
      content << {:title => title, :body => ('- <a href="'+item.link+'">'+item.title+"</a>\n"+item.\
description+"...\n\n" rescue item.to_s)}
    }
  else
    data.each {|d|
      # delete line feed and space at the top of content
      content << {:title => d.title, :body => d.content_encoded.to_s.sub(/^(?:\s+)/, '')}
    }
  end

  mdw = MixiDiaryWriter.new(config['username'], config['password'])

  content.each {|entry|
    mdw.login
    mdw.edit entry[:title], mdw.striptags(entry[:body])
  }
end

で、思った。mixiにログインするところとかモジュールにすればよかった。

変更履歴

  • 2007/05/20 rev.74の仕様に合わせてYAMLの記述を修正