「follotter.rb」を改造してFollowing/Followersの両方の変動を表示するようにしてみた

最近Twitterの不具合で勝手にFollowが外れたりすることがあるので,id:beatinaniwaのfollotter.rbを少し改造してFollowing/Followersの両方の変動を簡単に確認できるようにしてみた.


元のfollotter.rbはこちら.id:beatinaniwa++
http://d.hatena.ne.jp/beatinaniwa/20080430

ソース

follotter.rb

require 'rubygems'
require 'mechanize'

class Follotter
  def initialize(user, pass)
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = 'Mac Safari'
    @agent.max_history = 1
    login_form = @agent.get('http://twitter.com/').forms.action('https://twitter.com/sessions').first
    login_form['session[username_or_email]'] = user
    login_form['session[password]'] = pass
    @agent.submit(login_form)
  end

  def run(command = 'check')
    case command
    when 'update'
      File.open("following.txt", "w") do |io|
        num = following_count
        puts "Following : #{num}"
        idx = (num%20 != 0) ? (num/20 + 1) : (num/20)
        (1..idx).each do |n|
          io.puts((@agent.get("http://twitter.com/friends?page=#{n}")/"a.url").map {|i| i.inner_text})
        end
      end
      File.open("followers.txt", "w") do |io|
        num = followers_count
        puts "Followers : #{num}"
        idx = (num%20 != 0) ? (num/20 + 1) : (num/20)
        (1..idx).each do |n|
          io.puts((@agent.get("http://twitter.com/followers?page=#{n}")/"a.url").map {|i| i.inner_text})
        end
      end
    when 'check'
      num = following_count
      puts "Following : #{num}"
      idx = (num%20 != 0) ? (num/20 + 1) : (num/20)
      following = (1..idx).inject([]) {|memo, n|
        memo + (@agent.get("http://twitter.com/friends?page=#{n}")/"a.url").map {|fr| fr.inner_text}
      }

      old_following = File.open("following.txt", "r") {|io| io.readlines}.map {|l| l.chomp}
      (old_following - following).each do |i|
        puts "- #{i}"
      end
      (following - old_following).each do |i|
        puts "+ #{i}"
      end

      puts "========================================="

      num = followers_count
      puts "Followers : #{num}"
      idx = (num%20 != 0) ? (num/20 + 1) : (num/20)
      followers = (1..idx).inject([]) {|memo, n|
        memo + (@agent.get("http://twitter.com/followers?page=#{n}")/"a.url").map {|fr| fr.inner_text}
      }
      old_followers = File.open("followers.txt", "r") {|io| io.readlines}.map {|l| l.chomp}

      (old_followers - followers).each do |i|
        puts "- #{i}"
      end
      (followers - old_followers).each do |i|
        puts "+ #{i}"
      end
    else
      raise 'invalid arguments'
    end
  end

  private
  def following_count
    (@agent.get("http://twitter.com/friends")/:h2).inner_text.match(/\d+/).to_s.to_i
  end

  def followers_count
    (@agent.get("http://twitter.com/followers")/:h2).inner_text.match(/\d+/).to_s.to_i
  end
end

if __FILE__ == $0
  user = 'username' #自分のusernameに変更
  pass = 'password' #自分のpasswordに変更
  Follotter.new(user, pass).run(*ARGV)
end

FollowersだけでなくFollowingのデータも取得するようにしたり,Bioの出力部分を削ったりしている.

実行方法

実行方法はid:beatinaniwaのfollotter.rbと同じ.まず,

$ ruby follotter.rb update

でFollowing/Followers一覧を取得してfollowing.txtとfollowers.txtに保存しておく.

次に,Following/Followersの数が変動した後に,

$ ruby follotter.rb check
または
$ ruby follotter.rb

を実行することで,updateを実行したときと現在のFollowing/Followersの差分を取ることができる.

以下は出力例.

Following : 497
- syou6162
+ beatinaniwa
=========================================
Followers : 449
+ BarackObama

ここで,removeしてないのにFollowingで"-"の人が表示された場合はFollowが勝手に外れたかその人からブロックされたかのどちらかということになる.コマンド一発で差分を確認できるので楽々.


それにしても,勝手にFollowが外れる不具合は本当に困るので,Twitterには早く解決してもらいたいところだ.