-
-
Save rummelonp/907117 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
=begin | |
必要なもの | |
$ gem install rubytter | |
../oauth_yaml/#{Client}-#{App}.yaml ( TwitterOAuthの情報を格納したもの ) | |
最初の方に定義してある定数をちょこちょこ書き換えるスキル | |
これはなに | |
指定した人の最新200件のtweetを取得して、既にふぁぼってるの以外をふぁぼります。 | |
このscriptでふぁぼったものだけを #{@screen_name}-favterro-#{日付}.yaml に書き出すので、簡単にリセットできます。 | |
つかいかた | |
ruby favterro.rb TARGET_Screen_name | |
戻し方 | |
ruby favterro.rb (適当な単語) #{@screen_name}-favterro-#{日付}.yaml | |
=end | |
require 'rubygems' | |
require 'rubytter' | |
require 'yaml' | |
require 'date' | |
#設定用の定数など | |
DEBUG = false | |
Client = 'DNPP' | |
App = 'Ofuton' | |
CDIR = File.dirname(File.expand_path(__FILE__)) | |
OAuth_YAML = CDIR+'/../oauth_yaml/'+Client+'-'+App+'.yaml' | |
if ARGV.size==2 | |
Destroy = true | |
Destroy_YAML = CDIR+'/'+ARGV[1].to_s | |
Target = '' | |
elsif ARGV.size == 1 | |
Destroy = false | |
Destroy_YAML = "" | |
Target = ARGV[0].to_s | |
else | |
puts "使い方読め。" | |
exit | |
end | |
if DEBUG | |
Log = true | |
RetryWait = 1 | |
SleepWait = 0 | |
Count = 3 | |
puts "---> DEBUG mode" | |
puts "---> RetryWait = #{RetryWait}" | |
puts "---> SleepWait = #{SleepWait}" | |
puts "---> Count = #{Count}" | |
else | |
Log = true | |
RetryWait = 5 | |
SleepWait = 2 | |
Count = 200 | |
end | |
#YAMLへ保存しやすいように | |
def yaml_dump(contents,path) | |
open(path,'w') { |i| i << YAML.dump(contents) } | |
end | |
#処理をまとめたclass | |
class FavTerro | |
def initialize(screen_name="") | |
@screen_name = screen_name | |
@cancel = false | |
@terro_status = Array.new | |
@now = DateTime.now.strftime("%Y%m%d%H%M") | |
@terro_yaml = CDIR+'/'+@screen_name+'-favterro-'+@now+'.yaml' | |
#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から諸情報を取得と格納と、失敗した場合のフラグ | |
if !Destroy | |
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}\")" | |
@cancel = true | |
end | |
end | |
end | |
end | |
#user_timelineの取得と格納と、失敗した場合のフラグ | |
def get_user_timeline | |
begin | |
puts "---> fetching @#{@screen_name}'s user_timeline" if Log | |
@target_tl = @t.user_timeline(@user[:id], :count => Count) | |
rescue | |
puts "---! Error in @t.user_timeline(#{@user[:id]}, :count => Count) retry after #{RetryWait} seconds..." if Log | |
sleep RetryWait | |
begin | |
@target_tl = @t.user_timeline(@user[:id], :count => Count) | |
rescue | |
puts "---! Error in @t.user_timeline(#{@user[:id]}, :count => Count)" | |
@cancel = true | |
end | |
end | |
yaml_dump(@target_tl,CDIR+'/'+@screen_name+'-user_timeline-'+@now+'.yaml') if DEBUG | |
end | |
#fav | |
def fav | |
@target_tl.each do |t| | |
if !t[:favorited] | |
begin | |
@t.favorite(t[:id]) | |
puts "---> favoited @#{@screen_name}'s status ( id = #{t[:id]} )" if Log | |
@terro_status << t[:id] | |
rescue | |
puts "---! Error in @t.favorite(#{t[:id]}) retry after #{RetryWait} seconds..." if Log | |
sleep RetryWait | |
begin | |
@t.favorite(t[:id]) | |
puts "---> favoited @#{@screen_name}'s status ( id = #{t[:id]} )" if Log | |
@terro_status << t[:id] | |
rescue | |
puts "---! Error in @t.favorite(#{t[:id]})" | |
end | |
end | |
yaml_dump(@terro_status,@terro_yaml) | |
s = rand(5)*SleepWait | |
puts "---> sleep #{s}" if Log | |
sleep s | |
end | |
end | |
end | |
#destroy | |
def destroy | |
failure = Array.new | |
YAML.load_file(Destroy_YAML).each do |d| | |
begin | |
@t.remove_favorite(d) | |
puts "---> remove_favorite(#{d})" if Log | |
rescue | |
puts "---! Error in @t.remove_favorite(#{d}) retry after #{RetryWait} seconds..." if Log | |
sleep RetryWait | |
begin | |
@t.remove_favorite(d) | |
puts "---> remove_favorite(#{d})" if Log | |
rescue | |
puts "---! Error in @t.remove_favorite(#{d})" | |
failure << d | |
yaml_dump(failure,CDIR+'/destroy-failure-'+@now+'.yaml') if failure.size!=0 | |
end | |
end | |
end | |
puts "---> finish" if failure.size==0 | |
puts "---! some status are still favorited. please try to 'ruby favterro.rb destroy destroy-failure-#{@now}.yaml'" if failure.size!=0 | |
end | |
#処理まとめ。デバッグの時は先頭に#とか付けてコメントアウト出来て便利 | |
def run | |
self.get_user_timeline | |
self.fav if !@cancel | |
puts "---> no favorite to @#{@screen_name}" if ( Log && @terro_status.size == 0 ) | |
end | |
#initializeで調べた感じ、無効なscreen_nameでないっぽいなら run | |
def main | |
self.run if !@cancel | |
end | |
end | |
#実行 | |
if !Destroy | |
puts "---> --- @#{Target} ---" if Log | |
FavTerro.new(Target).main | |
elsif ( Destroy && File.exist?(Destroy_YAML) ) | |
puts "---> --- #{Destroy_YAML} ---" if Log | |
FavTerro.new.destroy | |
elsif ( Destroy && !File.exist?(Destroy_YAML) ) | |
puts "---! Destroy_YAML File not found" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment