twitterで自分のフォロワーが変化したらDMで知らせるようにするRubyスクリプト(OAuth対応)

twitterで自分のフォロワーが変化したらDMで知らせるようにするPythonスクリプト - phithonの日記
OAuth対応させたらRubyになってた。
以下ソース
APIのラッパー

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'oauth'
require 'json'

API_URI = 'http://twitter.com/'

class TwitterAPI
  def initialize(consumerKey, consumerSecret, accessToken, accessTokenSecret, debug = false)
    @debug = debug
    consumer = OAuth::Consumer.new(consumerKey,consumerSecret,:site => API_URI)
    @token = OAuth::AccessToken.new(consumer,accessToken,accessTokenSecret)
    profile = get('account/verify_credentials.json')
    @screenName = profile['screen_name']
  end
  def get(url, params={})
    url = API_URI + url
    if !params.empty?
      url += '?' + params.keys.map{
        |key| URI.escape(key.to_s) + '=' + URI.escape(params[key])
      }.join('&')
    end
    puts "Request: " + url if @debug
    response = JSON.parse(@token.get(url).body)
    showRateLimit if @debug
    return response
  end
  def post(url, params={})
    url = API_URI + url
    if @debug
      puts "Request: " + url
      puts "Post data: " + params.inspect
    end
    response = JSON.parse(@token.post(url, params).body)
    showRateLimit if @debug
    return response
  end
  def updateStatus(status, inReplyToStatusId = nil)
    return post('statuses/update.json', :status => status, :in_reply_to_status_id => inReplyToStatusId)
  end
  def sendDM(user, text)
    return post('direct_messages/new.json', :user => user, :text => text)
  end
  def getFollowers(target = @screenName)
    cursor = '-1'
    users = []
    while cursor != '0':
      response = get('statuses/followers/' + target + '.json', :cursor => cursor)
      users.concat(response['users'])
      cursor = response['next_cursor_str']
    end
    return users
  end
  def showRateLimit()
    hash = JSON.parse(@token.get(API_URI + 'account/rate_limit_status.json').body)
    seconds = hash['reset_time_in_seconds'] - Time.now.to_i
    hours = seconds / 3600
    seconds = seconds % 3600
    minutes = seconds / 60
    seconds = seconds % 60
    puts 'Remaining: ' + hash['remaining_hits'].to_s + ' / ' + hours.to_s + ':' + minutes.to_s + ':' + seconds.to_s
  end
end

実際にフォロワーの変化確認する部分。CONSUMER_KEYとかの取得方法は
さくらレンタルサーバでRubyでOAuthでTwitterBot - phithonの日記
からリンク辿って確認して下さい。

#!/usr/bin/env ruby
# -*- coding:utf8 -*-
$:.unshift(File.dirname(__FILE__))
require 'twitterapi'
require 'set'

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

OWNER_NAME = '' # DM送信先ユーザ
if (ARGV.length != 1)
  puts "usage: " + $0 + " logFilePath"
  exit
end

LOG_PATH = ARGV[0]

twitter = TwitterAPI.new(CONSUMER_KEY,CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET, true)

newFollowers = Set.new(twitter.getFollowers(OWNER_NAME).map{|user| user['screen_name']})

if FileTest.exists?(LOG_PATH)
  lines = 
  oldFollowers = Set.new(File::open(LOG_PATH) {|f|
    f.each.map{|l| l.strip}
  })
  comings = newFollowers - oldFollowers
  leavings = oldFollowers - newFollowers
  exit if (comings.empty? and leavings.empty?)
  message = 'フォロワーが変化しました。'
  if (!comings.empty?)
    message += 'new: @' + comings.to_a.join(', @')
    message += ', ' if !leavings.empty?
  end
  if (!leavings.empty?)
    message += 'removed: @' + leavings.to_a.join(', @')
  end
  twitter.sendDM(OWNER_NAME, message)
  File::open(LOG_PATH, 'w') {|f| newFollowers.each{|u| f.puts(u)}}
else
  File::open(LOG_PATH, 'w') {|f| newFollowers.each{|u| f.puts(u)}}
  twitter.sendDM(OWNER_NAME, "フォロワー数変化の記録を開始します。")
end