自動フォロー返し(Ruby bot)2

「自動フォロー返し(Ruby bot)2」の編集履歴(バックアップ)一覧に戻る

自動フォロー返し(Ruby bot)2 - (2011/06/26 (日) 09:51:14) のソース

フォロー通知が何度も行くという問題が発生したので、それを回避するのを新しく作った。特に制御構造のところを頑張りました。以前と同様にフォロワー取得の部分は[[http://d.hatena.ne.jp/tondol/20100412/1271016565>こちら]]より頂いてきましたが、制御構造の部分は自前です! 今は眠いのでそのうちちゃんとコメントを書くかも。正直汚くてネット上にあげれるようなもんじゃないけどね。もっと簡単にできると思うんだけど、こんなになってしまった。

#highlight(linenumber,ruby){{
#! /usr/bin/ruby
# encoding: utf-8

# http://route477.net/w/?RubyTwitterJa

require "time"
require "rubygems"
require 'twitter'

#http://d.hatena.ne.jp/tondol/20100412/1271016565

def fetch(instance, method, array_name, query)
	next_cursor = -1
	begin
		query['cursor'] = next_cursor
		instance.__send__(method, query).each{|a|
			value = a.pop
			key = a.pop
			if key == array_name then
				value.each{|v|
					yield(v)
				}
			elsif key == "next_cursor" then
				next_cursor = value.to_i
			end
		}
	end while next_cursor > 0
end
 



# ログイン
Twitter.configure do |config|
  config.consumer_key = '???????????????????????'
  config.consumer_secret = 'あばばっばばばばっb'
  config.oauth_token = 'くぁwさえらsdfふじこあsdkふぁks'
  config.oauth_token_secret = 'ajgiswejfij'
end 

f1 = File::open("followers.txt", "r") # http://goo.gl/k0ZNZ
f2 = File::open("followers_temp.txt", "w")


# 自身のfollowersを全件取得して表示
  fetch(Twitter, :followers, "users", {})   {|v|
    f2.puts "#{v.id}"
 }

#followers = Twitter.follower_ids(query={})
#f2.print("#{followers.ids}") #この方法でフォロワー取得出来ればいいんだけど、id がつながってしまう。
f2.close

f3 = File::open("followers_temp.txt", "r")

j = 0
tmp = []
while tmp[j] = f3.gets
#  print tmp[j]
  j += 1
end

followers = []
x = 0
while followers[x] = f1.gets
#  print followers[x]
  x += 1
end
#tmp[]は現在のフォロワー、followers[]は前回のフォロワーが入った配列。
#それらを比較してtmpだけに入っているIDをフォローする。
k=0
i = 0
q = 9999

while k < j+1
  while i < q
    if followers[i] != nil
      if tmp[k] == followers[i]
        puts "You already follow the user of id: #{tmp[k].to_i}."
        break
      end 
    else
      if tmp[k] != nil
        puts "You have a new follower of id:#{tmp[k].to_i}. Let's follow."
        Twitter.friendship_create(tmp[k].to_i)
        break
      else
        break
      end
    end
    i += 1 
  end
  k += 1
end
f1.close

print j

f4 = File::open("followers.txt", "w")
i =0
while i < j+1
  f4.puts "#{tmp[i]}"
  i += 1
end

f4.close
f3.close
}}