読者です 読者をやめる 読者になる 読者になる

まめ畑

ゆるゆると書いていきます

fuetter直しました

Ruby Twitter

かなり前に作ったTwitterのフォローの増減をお知らせするスクリプトですが、使えなくなっていたので直しました。ついでに少しコードも変更しました。
Followersのページのパラメータがページ数でなくなった模様です。


tmailとmechanizeが必要です。
Twitterの言語を英語にしている方は、「次へ »」を「Next »」に変更して下さい。
(コード中の>>を»に変更して下さい)

# -*- coding: utf-8 -*-

require 'open-uri'
require 'rubygems'
require 'mechanize'
require 'tmail'
require 'net/smtp'
require 'date'
require 'time'
require 'yaml'


LOGIN_PAGE_URL = "http://twitter.com/login"
FOLLOWERS_PAGE_URL = "http://twitter.com/followers"
FOLLOWERS_FILE = "followers.txt"


def read_yaml
    settings = YAML::load(File.open('fuetter.yaml'))
    $id = settings['user']['id']
    $pass = settings['user']['pass']
    $smtp_server = settings['mail']['server']
    $mail_to = settings['mail']['to']
    $mail_from = settings['mail']['from']
end

def login
    $agent = WWW::Mechanize.new
    $agent.max_history = 1
    $agent.user_agent_alias = "Windows IE 7"
    login_page = $agent.get(LOGIN_PAGE_URL)
    login_form = login_page.forms[1]
    login_form["session[username_or_email]"] = $id
    login_form["session[password]"] = $pass
    my_home = $agent.submit(login_form)
end

def get_followers_data
    followers = []

    my_follower_page = $agent.get(FOLLOWERS_PAGE_URL)
    loop do
        follower = (my_follower_page/"//span[@class=\"label screenname\"]/a")
        follower.each { |f| followers << f.inner_text }
        next_page = my_follower_page.link_with(:text => "次へ >>")
        break if next_page.nil?
        my_follower_page = next_page.click
    end
    followers
end

def get_my_followers(is_file_exist = false)
    begin
        before_followers = []
        now_followers = []

        now_followers = get_followers_data

        if is_file_exist
            open(FOLLOWERS_FILE, "r") do |f|
                while line = f.gets
                    before_followers << line.chomp
                end
            end

            new_follower = now_followers - before_followers
            remove = before_followers - now_followers
            make_body(new_follower, remove)
        end

        open(FOLLOWERS_FILE, "w") do |f|
            now_followers.each { |fr| f.puts(fr) }
        end
    rescue =>ex
        p ex
    end
end

def make_body(add_follower, bye_follower)
    mail_body = "*Your Followers Report*\n\n"
    mail_body << "*** New Follower(s) ***\n"
    if add_follower.length.zero?
        mail_body << "No New Follower\n"
    else
        mail_body << add_follower.join("\n")
    end

    mail_body << "\n\n"
    mail_body << "*** Remove Follower(s) ***\n"
    if bye_follower.length.zero?
        mail_body << "No Bye Follower\n\n"
    else
        bye_follower = check_user(bye_follower)
        mail_body << bye_follower.join("\n")
    end
    send_mail(mail_body.chomp)
end

def check_user(bye_follower)
    tmp = []
    bye_follower.each do |id|
        begin
            open("http://twitter.com/#{id}")
            tmp << id.to_s + " [Exist]"
        rescue OpenURI::HTTPError
            tmp << id.to_s + " [Del]"
        end
    end
    tmp
end

def send_mail(body)
    today = Date.today
    mail = TMail::Mail.new
    mail.to = $mail_to
    mail.from = $mail_from
    mail.reply_to = $mail_from
    mail.subject = "Fuetter Report "+ today.strftime("%Y/%m/%d")
    mail.body = body

    mail.date = Time.now
    mail.mime_version = "1.0"
    mail.set_content_type('text', 'plain', {'charset'=>'iso-2022-jp'})

    mail.write_back

    Net::SMTP.start($smtp_server) do |smtp|
        smtp.sendmail(mail.encoded, mail.from, mail.to)
    end
end

read_yaml
login
get_my_followers(File.exists?(FOLLOWERS_FILE))


設定など(fuetter.yaml)

user:
   id: ID
   pass: PASS

mail:
   server: SMTP Server
   to: メール送信先アドレス
   from: メール送信元アドレス

cronなどで定期的に実行すれば完了です。
500エラーなどで停止したときなどの処理は無いので、お好みでお付け下さい。