LoginSignup
7
6

More than 5 years have passed since last update.

Mechanizeで全フォロワーをフォロバ。

Posted at

Mechanizeで自動でフォローバックするスクリプト

TwitterAPIなどを使わずに、email, password, アカウント名(どのアカウント名でも可能)のみでそのアカウントをフォローしている全フォロワーをフォローすることができるプログラムです。

1.Twitterのアカウントにログイン
2.フォロワーページへ移動
3.フォローしてないユーザーをフォロー

フォロワーページは一度に15ユーザーくらいしか表示されないので、2と3を繰り返す感じになります。

コード

auto-follow.rb
#coding: utf-8
require 'rubygems'
require 'mechanize'


# Mechanizeクラスを継承するTwitterAgentクラスを定義
class TwitterAgent < Mechanize

  # 引数にログイン名(メアド)とパスワードを渡してログイン
  def login(user_name, password)
    uri = "https://mobile.twitter.com/session/new"
    page = self.get(uri)
    unless page.meta_refresh.empty?
      page = self.get(page.meta_refresh[0].uri.to_s)
    end
    forms = page.form[0]
    forms['session[username_or_email]'] = user_name
    forms['session[password]'] = password
    page = forms.submit
    return page
  end

  # アカウント名を渡してフォロワーページを取得
  def to_followers_page(account_name)
    uri = "https://mobile.twitter.com/#{account_name}/followers"
    return self.get(uri)
  end

  # フォロワー一覧ページを渡して次のフォロワーページへ移動
  def move_next_page(page)
    if page.link[-5].text.include?("ユーザーを表示")
      page = page.links[-5].click
    else
      page = nil
    end
    return page
  end

  # フォロワー一覧ページを渡して、そこにいるフォローしていないユーザーを全員フォローする
  def follow_back_all(page)
    page.forms.each do |form|
      if form.action.include("/follow")
        button = form.buttons[0]
        self.submit(form, button)
      end
    p "FOLLOW!!"
    end
  end
end

以上のコードでフォロバする準備が整いました。

コード解説

gem, class定義

require 'mechanize'

# Mechanizeクラスを継承するTwitterAgentクラスを定義
class TwitterAgent < Mechanize

自分の代わりにweb上を移動してくれるエージェントを作るためにMechanizeというgemを使います。
http://seesaawiki.jp/ruby_mechanize/
に細かなクラス、メソッドの解説がありますので参考にしてください。

今回はMechanizeクラスを継承したTwitterAgentクラスを定義していきます。

ログイン

# 引数にログイン名(メアド)とパスワードを渡してログイン
  def login(user_name, password)
    uri = "https://mobile.twitter.com/session/new"
    page = self.get(uri)
    unless page.meta_refresh.empty?
      page = self.get(page.meta_refresh[0].uri.to_s)
    end
    forms = page.form[0]
    forms['session[username_or_email]'] = user_name
    forms['session[password]'] = password
    page = forms.submit
    return page
  end

ユーザ名とパスワードを渡してエージェントをログインさせるloginメソッドを定義します。
Mechanizeクラスの、指定したurlの情報を取得するgetメソッドでtwitterのログイン画面のformなどを取得してpageに代入します。
取得したページにmeta_refreshが設定されている場合はそちらのページをpageに代入しましょう。*meta_refresh
page.form[0]でログインページのフォームを取得します。
formのemail,password欄にログイン名とパスワードを入力してsubmitでログインします。

フォロワー一覧ページ


 # アカウント名を渡してフォロワーページを取得
  def to_followers_page(account_name)
    uri = "https://mobile.twitter.com/#{account_name}/followers"
    return self.get(uri)
  end

 # フォロワー一覧ページを渡して次のフォロワーページへ移動
  def move_next_page(page)
    if page.link[-5].text.include?("ユーザーを表示")
      page = page.links[-5].click
    else
      page = nil
    end
    return page
  end

引数にアカウント名を渡して、フォロワー一覧ページに飛びます。
move_next_pageメソッドで、一覧ページを渡して次の一覧ページに飛びます。
下から5番目のリンクが次のユーザーページへのリンクなので、そこに"ユーザーを表示"という文字列が含まれる場合そのリンクをクリックして次のページを取得します。

フォローバック

 # フォロワー一覧ページを渡して、そこにいるフォローしていないユーザーを全員フォローする
  def follow_back_all(page)
    page.forms.each do |form|
      if form.action.include("/follow")
        button = form.buttons[0]
        self.submit(form, button)
      end
    p "FOLLOW!!"
    end
  end

フォロワー一覧ページを渡してそのページ上のフォローしていないユーザーをフォローします。
page.formsでフォームを取得して、/followという文字列を含む場合にsubmitしてフォローします。

実行

user = {
    :account   => "アカウント名",
    :email     => "ログイン名",
    :password  => "パスワード"
}


agent = TwitterAgent.new
agent.login(user[:email], user[:password])
page = agent.to_followers_page(user[:account])
while !page.nil?
    agent.follow_back_all(page)
    page = agent.move_next_page(page)   
end

以上で、あるアカウントをフォローしている全ユーザーをフォローし返すことができます。
自動的にこれを実行したい場合はcronで定期的に実行するなどの方法が考えられます。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6