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

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

はてなブックマークに投稿するプラグイン:publish::hatena_bookmark

Nowhere Near - はてなブックマークAtomAPI バインディング for Rubyのライブラリを用いて作成。
事前にgemをインストール。

# gem install hatenabm

publish/hatena_bookmark.rb

## author "emergent"
## description  "post feeds to hatena bookmark"
## this requires hatenabm
## -----
## $ gem install hatenabm
## -----
## example <<EOE
## - module: Publish::hatena_bookmark
##   config:
##     username: your_username
##     password: your_password
##     opt_tag: "imported"
##     no_comment: 1
## EOE
## config({ :username => Field.new("username to login", String, true),
##          :password => Field.new("password to login", String, true),
##          :opt_tag => Field.new("optional tag(s)", String) })
require 'rubygems'
require 'hatenabm'

def hatena_bookmark(config, data)
  sleeptime = config["sleep"] ? config["sleep"].to_i : 2

  opt_tag = config[:opt_tag] || ''
  hbm = HatenaBM.new(:user => config['username'],:pass => config['password'])

  data.each {|entry|
    puts 'posting ' + entry.title + ': '

    tags = entry.dc_subjects.map do |s| s.content end.join(' ') rescue ''    
    if config['opt_tag']
      tags = [tags, config['opt_tag']].select{|t| t.length > 0}.join(' ')
    end

    summary = config['no_comment'] ? entry.description : ''

    hbm.post(
             :title => entry.title,
             :link  => entry.link, 
             :tags  => tags,
             :summary => summary
             )
    sleep sleeptime
  }
  return data
end

ライブラリのおかげで意外とあっさり出来た感じです。
使い方は、

- module: RSS::load
  config:
    url: http://del.icio.us/rss/hogege
- module: Publish::hatena_bookmark
  config:
    username: emergent
    password: **********
    opt_tag: "imported"

みたいな感じで。opt_tagで追加のタグを記述。(スペース区切り)

変更履歴

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