Created
August 27, 2010 07:53
-
-
Save halsk/553009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# 使い方 | |
# | |
# 1.CONSUMER_KEY と CONSUMER_SECRET を設定する | |
# CONSUMER_KEY と CONSUMER_SECRET は http://twitter.com/apps/new で新規アプリケーションを登録すると作れます | |
# | |
# 2. OAuth用のユーザトークン、シークレットを取得する。 | |
# get_usertokens.rb(http://gist.github.com/553012) で取得できます | |
# | |
# 3.autofollow_config ディレクトリを作り、その下に ログイン情報の書かれた yml ファイルを置く | |
# yml ファイルの例 | |
# | |
# mail: | |
# host: imap.gmail.com | |
# port: 993 | |
# ssl: true | |
# username: your user name | |
# password: your password | |
# | |
# twitter: | |
# screenname: your twitter screen name | |
# atoken: your user token # 手順2で取得した user token | |
# asecret: your user token secret # 手順2で取得した user token secret | |
# | |
# twittermail: | |
# from: from address from twitter # Twitter から来るフォロー通知メールの from アドレス | |
# | |
# options: | |
# manual_follow: true ※true にすると、ユーザのTweetを確認しながらマニュアルでフォロー判定ができます。 | |
# false にすると、そのユーザを無条件でフォローします。 | |
# | |
# 4. 実行する | |
# ruby twitter_autofollow.rb | |
require 'rubygems' | |
require 'optparse' | |
require 'pp' | |
require 'net/imap' | |
require 'kconv' | |
require 'tmail' | |
require 'twitter' | |
CONSUMER_KEY = 'your consumer key' | |
CONSUMER_SECRET = 'your consumer secret' | |
class AutoFollow | |
attr_accessor :config, :is_delete_all, :consumer_key, :consumer_secret | |
def initialize(config) | |
self.config = config | |
end | |
def run | |
p "login to IMAP server as user #{config.mail.username}" | |
# GmailにIMAPで接続、ログインする | |
@imap = Net::IMAP.new( config.mail.host, config.mail.port, config.mail.ssl ) | |
@imap.login( config.mail.username, config.mail.password ) | |
# 受信箱を開く | |
@imap.select('inbox') | |
# Twitter にログイン | |
p "login to Twitter as user #{config.twitter.screenname}" | |
token = [self.consumer_key, self.consumer_secret] | |
user_atoken = [config.twitter.atoken, config.twitter.asecret] | |
oauth = Twitter::OAuth.new(*token) | |
oauth.authorize_from_access(*user_atoken) | |
twit = Twitter::Base.new(oauth) | |
# Twitter からのメールを取得 | |
ret = @imap.search(['from',config.twittermail.from ]) | |
if ret.length == 0 | |
puts "no mail" #メールなし | |
return | |
end | |
@imap.fetch(ret , 'ENVELOPE').each do |f| | |
puts "==========================================================" | |
messageNo = f.seqno | |
date = f.attr["ENVELOPE"].date | |
from_name = f.attr["ENVELOPE"].from[0].name | |
subject = f.attr["ENVELOPE"].subject | |
subject = "" if subject == nil | |
print "#{messageNo}: #{date} #{from_name} #{Kconv.toutf8(subject)}\n" | |
@imap.fetch(messageNo, "RFC822").each do |m| | |
mail = TMail::Mail.parse(m.attr["RFC822"]) | |
body = NKF::nkf('--sjis', mail.body) | |
follower = '' | |
# follow したユーザの screenname はメールヘッダに入っている | |
if mail.header['x-twittersenderscreenname'] | |
follower = mail.header['x-twittersenderscreenname'].body | |
begin | |
user = twit.user(follower) | |
rescue | |
# ユーザ情報が取得できなかった | |
p "couldn't get user information #{follower}" | |
if self.is_delete_all # -d オプションで起動していたらメールを削除する | |
@imap.store(messageNo, '+FLAGS', [:Seen] ) | |
@imap.store(messageNo, "+FLAGS", [:Deleted]) | |
end | |
next | |
end | |
if user.following | |
# 既にフォローしている | |
p "you are already following #{user.screen_name}" | |
else | |
# マニュアルでフォローする場合 ユーザ情報を表示してからフォロー選択 | |
if config.options.manual_follow | |
puts "#{user.name}\n#{user.description}" | |
Twitter.timeline(user.screen_name).each { |t| | |
puts "[#{t.created_at}]:#{t.text}" | |
} | |
puts "follow #{user.screen_name} ? y/n" | |
isfollow = true if gets == 'y' | |
else | |
isfollow = true | |
end | |
if isfollow | |
p "follow #{user.screen_name}" | |
# フォローする | |
begin | |
twit.friendship_create(follower) | |
rescue | |
p "counldn't follow user" | |
next | |
end | |
end | |
sleep (1) | |
end | |
@imap.store(messageNo, '+FLAGS', [:Seen] ) | |
@imap.store(messageNo, "+FLAGS", [:Deleted]) | |
break | |
end | |
end | |
end | |
@imap.close | |
puts "operation completed" | |
end | |
end | |
# オプション取得 | |
OPTS = {} | |
opt = OptionParser.new | |
opt.on('-d') {|v| OPTS[:d] = true } | |
opt.on('-h') {|v| puts <<EOF | |
usage #{$0} [-d] | |
d : force to delete mails even if twiter infromation is not retrieved. | |
EOF | |
exit -1 | |
} | |
opt.parse!(ARGV) | |
# autofollow_config の下の設定ファイル分繰り返す | |
Dir::foreach(File.join(File.dirname(__FILE__), "autofollow_config")) do |c| | |
next if c == "." or c == ".." or c[0..1] == ".#" | |
config = File.open(File.join(File.join(File.dirname(__FILE__), "autofollow_config"),c)) | |
config = Hashie::Mash.new(YAML::load(config)) | |
# start following | |
a = AutoFollow.new(config) | |
a.consumer_key = CONSUMER_KEY | |
a.consumer_secret = CONSUMER_SECRET | |
a.is_delete_all = true if OPTS[:d] | |
a.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment