Created
February 16, 2011 12:38
-
-
Save dnpp73/829302 to your computer and use it in GitHub Desktop.
指定したscreen_nameのfollowingのscreen_nameの変更をチェックする感じ。
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
=begin | |
必要なもの | |
$ gem install rubytter | |
./target.yaml ( screen_nameの配列の形になってるもの ) | |
../oauth_yaml/#{Client}-#{App}.yaml ( TwitterOAuthの情報を格納したもの ) | |
これはなに | |
指定したscreen_nameのfollowingのscreen_nameの変更をチェックする感じ。 | |
つかいかた | |
$ crontab -e | |
で、こんな感じの追記をして | |
0 0 * * * /path/to/ruby /path/to/screen_name-check.rb | |
後は適当な画面で | |
$ tail -f /path/to/log/*-screen_name-log.txt | |
tail -f で気付けなくても、cronからのメールで気付けるかもしれないってときは CronMail を true にすればいいと思う。 | |
=end | |
require 'rubygems' | |
require 'rubytter' | |
require 'yaml' | |
require 'date' | |
#設定用の定数など | |
DEBUG = false | |
Client = 'DNPP' | |
App = 'Ofuton' | |
Target = 'DNPP' | |
CDIR = File.dirname(File.expand_path(__FILE__)) | |
OAuth_YAML = CDIR+'/../oauth_yaml/'+Client+'-'+App+'.yaml' | |
if DEBUG | |
Log = true | |
CronMail = false | |
Sleep = false | |
SleepWait = 0 | |
Log_DIR = CDIR+'/'+'log-dev' | |
Data_DIR = CDIR+'/'+'data-dev' | |
RetryWait = 0 | |
puts "---> DEBUG mode" | |
puts "---> Log = #{Log}" | |
puts "---> CronMail = #{CronMail}" | |
puts "---> Sleep = #{Sleep}" | |
puts "---> SleepWait = #{SleepWait}" | |
puts "---> Log_DIR = #{Log_DIR}" | |
puts "---> Data_DIR = #{Data_DIR}" | |
puts "---> RetryWait = #{RetryWait}" | |
puts "" | |
else | |
Log = false | |
CronMail = true | |
Sleep = true | |
SleepWait = 3 | |
Log_DIR = CDIR+'/'+'log' | |
Data_DIR = CDIR+'/'+'data' | |
RetryWait = 10 | |
end | |
#YAMLへ保存しやすいように | |
def yaml_dump(contents,path) | |
open(path,'w') { |i| i << YAML.dump(contents) } | |
end | |
#ログ保存しやすいように | |
def log_dump(contents,path) | |
open(path,'a') { |i| i.puts contents } | |
end | |
#処理をまとめたclass | |
class ScreenNameCheck | |
def initialize(screen_name) | |
#重要っぽい変数など | |
@screen_name = screen_name | |
@user = Hash.new | |
@following = Array.new | |
@screen_names = Hash.new | |
@screen_names_yaml = Data_DIR+'/'+@screen_name+'-'+'screen_names.yaml' | |
@screen_names_yaml_now = Data_DIR+'/'+@screen_name+'-'+'screen_names'+'-'+DateTime.now.strftime("%Y%m%d%H%M")+'.yaml' | |
@screen_names_before = Hash.new | |
@log_txt = Log_DIR+'/'+@screen_name+'-'+'screen_name-log.txt' | |
@now = DateTime.now.strftime("%Y-%m-%d %a %H:%M") | |
@changes = false | |
#OAuth周りの認証処理 | |
begin | |
oauth = YAML.load_file(OAuth_YAML) | |
rescue | |
puts "---! Can't open #{OAuth_YAML}" | |
exit | |
end | |
consumer = OAuth::Consumer.new(oauth['consumer_key'], oauth['consumer_secret'], :site => 'https://api.twitter.com') | |
token = OAuth::AccessToken.new(consumer, oauth['oauth_token'], oauth['oauth_token_secret']) | |
@t = OAuthRubytter.new(token) | |
#screen_nameから諸情報を取得 | |
begin | |
puts "---> fetching @#{@screen_name} user info" if Log | |
@user = @t.user(@screen_name) | |
rescue | |
puts "---! Error in @t.user(\"#{@screen_name}\") retry after #{RetryWait} seconds..." if Log | |
sleep RetryWait | |
begin | |
@user = @t.user(@screen_name) | |
rescue | |
puts "---! Error in @t.user(\"#{@screen_name}\")" | |
exit | |
end | |
end | |
end | |
#followingの取得 | |
def get_following | |
puts "---> fetching @#{@screen_name}'s following" if Log | |
next_cursor = -1 | |
while next_cursor != 0 | |
begin | |
puts "---> fetching @#{@screen_name}'s following cursor #{next_cursor}" if Log | |
tmp = @t.friends(@user[:id],:cursor=>next_cursor) | |
rescue | |
puts "---! Error in @t.friends(#{@user[:id]},:cursor=>#{next_cursor}) ( #{@user[:id]} = #{@screen_name} ) retry after #{RetryWait} seconds..." if Log | |
sleep RetryWait | |
begin | |
tmp = @t.friends(@user[:id],:cursor=>next_cursor) | |
rescue | |
puts "---! Error in @t.friends(#{@user[:id]},:cursor=>#{next_cursor}) ( #{@user[:id]} = #{@screen_name} )" | |
exit | |
end | |
end | |
@following.concat [tmp] | |
next_cursor = tmp[:next_cursor] | |
sleep SleepWait | |
end | |
end | |
#screen_namesの取得 | |
def get_screen_names | |
@following.each do |i| | |
i[:users].each { |j| @screen_names[j[:id]] = j[:screen_name] } | |
end | |
#screen_namesをYAMLにdumpしたり、前回取得分をロードする | |
if File.exist?(@screen_names_yaml) | |
puts "---> load screen_names_yaml into screen_names_before" if Log | |
@screen_names_before = YAML.load_file(@screen_names_yaml) | |
puts "---> dump following in yaml" if Log | |
yaml_dump(@screen_names,@screen_names_yaml) | |
yaml_dump(@screen_names,@screen_names_yaml_now) | |
elsif !File.exist?(@screen_names_yaml) | |
puts "---> substitute screen_names for screen_names_before" if Log | |
@screen_names_before = @screen_names | |
puts "---> dump screen_names in yaml" if Log | |
yaml_dump(@screen_names,@screen_names_yaml) | |
yaml_dump(@screen_names,@screen_names_yaml_now) | |
end | |
end | |
#screen_namesのdiffを取る | |
def diff_screen_names | |
@screen_names.each do |id,screen_name| | |
screen_name_before = @screen_names_before[id] | |
if ( screen_name_before && screen_name != screen_name_before ) | |
contents = "< screen_name change > @#{screen_name_before} -> @#{screen_name} http://twitter.com/#{screen_name}" | |
puts "---> #{contents} " if Log | |
puts "#{contents} " if CronMail | |
log_dump(@now+' '+contents,@log_txt) | |
@changes = true | |
end | |
end | |
@screen_names_before.each do |id,screen_name_before| | |
screen_name = @screen_names[id] | |
if !screen_name | |
contents = "< Account deleted ? > @#{screen_name_before} is gone... " | |
puts "---> #{contents} " if Log | |
puts "#{contents} " if CronMail | |
log_dump(@now+' '+contents,@log_txt) | |
@changes = true | |
end | |
end | |
end | |
#処理まとめ | |
def main | |
log_dump('',@log_txt) if !File.exists?(@log_txt) | |
self.get_following | |
self.get_screen_names | |
self.diff_screen_names | |
puts "---> no changes detect on @#{@screen_name}'s following screen_names" if ( !@changes && Log ) | |
end | |
end | |
#出力先ディレクトリの確認して無いなら作成 | |
Dir.mkdir(Log_DIR) if !File.exist?(Log_DIR) | |
Dir.mkdir(Data_DIR) if !File.exist?(Data_DIR) | |
#実行 | |
puts "---> --- @#{Target} ---" if Log | |
ScreenNameCheck.new(Target).main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment