マイペースなプログラミング日記

x86エミュレータやFPGA、WebGLにお熱なd-kamiがマイペースに書くブログ

はてなハイクで☆をつけるスクリプト

はてなハイクで☆をつけるスクリプトをつけるスクリプトを作った。例としておはようキーワードの新着20件に対して☆を付けるスクリプトを書いた。パスワードは通常使ってるものではなくhttp://h.hatena.ne.jp/apiに書かれてるものを使うことに注意

#! /usr/bin/ruby -Ku

require 'net/http'
require 'rexml/document'
require 'uri'
require 'kconv'

username = 'hoge'
password = 'hoge'

class User
    attr_reader :id
    attr_reader :name
    attr_reader :url
    attr_reader :followers_count
    attr_reader :profile_image_url

    def initialize(element)
        @id = element.elements['id'].text()
        @name = element.elements['name'].text()
        @url = element.elements['url'].text()
        @followes_count = element.elements['followers_count'].text()
        @profile_image_url = element.elements['profile_image_url'].text()
    end
end

class Status
    attr_reader :id
    attr_reader :text
    attr_reader :user
    attr_reader :source
    attr_reader :created_at
    attr_reader :keyword

    def initialize(element)
        @id = element.elements['id'].text()
        @user = User.new(element.elements['user'])
        @keyword = element.elements['keyword'].text()
        @text = element.elements['text'].text().split(/=/)[1]
        @user = User.new(element.elements['user'])
        @source = element.elements['source'].text()
        @created_at = element.elements['created_at'].text
    end
end

class Haiku
    HaikuServer = 'h.hatena.ne.jp'

    def initialize(username, password)
        @username = username
        @password = password
    end

    def friends_timeline(count = 20, since = '')
        return timeline("/api/statuses/friends_timeline/#{@username}.xml", count, since)
    end

    def user_timeline(user = @username, count = 20, since = '')
        return timeline("/api/statuses/user_timeline/#{user}.xml", count, since)
    end

    def keyword_timeline(keyword, count = 20, since = '')
        return timeline("/api/statuses/keyword_timeline/#{keyword}.xml", count, since)
    end

    def timeline(path, count, since)
        data = "count=#{count}"
        if(since != '')
            data = "&since=#{since}"
        end

        request = Net::HTTP::Get.new(path + "?#{data}")
        request.basic_auth(@username, @password)
        http = Net::HTTP.start(HaikuServer)

        response = http.request(request)

        document = REXML::Document.new(response.body)

        return get_statuses(document)
    end

    def get_statuses(document)
         statuses = Array.new

        document.get_elements('/statuses/status').each{|element|
            status = Status.new(element)
            statuses << status
        }

        return statuses
    end

    def update(keyword, text)
        request = Net::HTTP::Post.new('/api/statuses/update.xml')
        submit_text = URI.escape(text.toutf8)
        submit_keyword = URI.escape(keyword.toutf8)

        request.basic_auth(@username, @password)
        http = Net::HTTP.start(HaikuServer)
        response = http.request(request, "status=#{submit_text}&keyword=#{submit_keyword}&source=Let's Haiku!")

        doc = REXML::Document.new(response.body)
        status = Status.new(doc.elements['/status'])

        return status
    end

    def create(id)
        request = Net::HTTP::Post.new("/api/favorites/create/#{id}.xml")

        request.basic_auth(@username, @password)
        http = Net::HTTP.start(HaikuServer)
        response = http.request(request)

        doc = REXML::Document.new(response.body)
        status = Status.new(doc.elements['/status'])
    end
end

haiku = Haiku.new(username, password)
haiku.keyword_timeline('おはよう', 20).each{|status|
    status = haiku.create(status.id)
    puts status.user.name + 'さんに☆をつけました'
}