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

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

自動フォロー返し(Ruby bot)2 - (2011/07/06 (水) 00:39:41) の編集履歴(バックアップ)


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

  1. #! /usr/bin/ruby
  2. # encoding: utf-8
  3.  
  4. # http://route477.net/w/?RubyTwitterJa
  5.  
  6. require "time"
  7. require "rubygems"
  8. require 'twitter'
  9.  
  10. #http://d.hatena.ne.jp/tondol/20100412/1271016565
  11.  
  12. def fetch(instance, method, array_name, query)
  13. next_cursor = -1
  14. begin
  15. query['cursor'] = next_cursor
  16. instance.__send__(method, query).each{|a|
  17. value = a.pop
  18. key = a.pop
  19. if key == array_name then
  20. value.each{|v|
  21. yield(v)
  22. }
  23. elsif key == "next_cursor" then
  24. next_cursor = value.to_i
  25. end
  26. }
  27. end while next_cursor > 0
  28. end
  29.  
  30. # ログイン
  31. Twitter.configure do |config|
  32. config.consumer_key = '???????????????????????'
  33. config.consumer_secret = 'あばばっばばばばっb'
  34. config.oauth_token = 'くぁwさえらsdfふじこあsdkふぁks'
  35. config.oauth_token_secret = 'ajgiswejfij'
  36. end
  37.  
  38. f1 = File::open("followers.txt", "r") # http://goo.gl/k0ZNZ
  39. f2 = File::open("followers_temp.txt", "w")
  40.  
  41.  
  42. # 自身のfollowersを全件取得して表示
  43. fetch(Twitter, :followers, "users", {}) {|v|
  44. f2.puts "#{v.id}"
  45. }
  46.  
  47. #followers = Twitter.follower_ids(query={})
  48. #f2.print("#{followers.ids}") #この方法でフォロワー取得出来ればいいんだけど、id がつながってしまう。
  49. f2.close
  50.  
  51. f3 = File::open("followers_temp.txt", "r")
  52.  
  53. j = 0
  54. tmp = []
  55. while tmp[j] = f3.gets
  56. # print tmp[j]
  57. j += 1
  58. end
  59.  
  60. followers = []
  61. x = 0
  62. while followers[x] = f1.gets
  63. # print followers[x]
  64. x += 1
  65. end
  66. #tmp[]は現在のフォロワー、followers[]は前回のフォロワーが入った配列。
  67. #それらを比較してtmpだけに入っているIDをフォローする。
  68. k=0
  69. i = 0
  70. q = 9999
  71.  
  72. while k < j+1
  73. while i < q
  74. if followers[i] != nil
  75. if tmp[k] == followers[i]
  76. puts "You already follow the user of id: #{tmp[k].to_i}."
  77. puts "This is in if tmp[k] == followers[i] i=#{i}, k=#{k}"
  78. i = 0
  79. break
  80. end
  81. else
  82. if tmp[k] != nil
  83. puts "You have a new follower of id:#{tmp[k].to_i}. Let's follow."
  84. puts "This is in if tmp[k] != nil i=#{i}, k=#{k}"
  85. Twitter.friendship_create(tmp[k].to_i)
  86. i = 0
  87. break
  88. else
  89. break
  90. end
  91. end
  92. i += 1
  93. end
  94. k += 1
  95. end
  96. f1.close
  97.  
  98. print j
  99.  
  100. f4 = File::open("followers.txt", "w")
  101. i =0
  102. while i < j+1
  103. f4.puts "#{tmp[i]}"
  104. i += 1
  105. end
  106.  
  107. f4.close
  108. f3.close
  109.  

コメント募集中
  • 正しく動いてなかったので直しました。whileをbreakするときに変数(i)を初期化するのを忘れてた。 -- (OK) 2011-07-06 00:40:34
名前:
コメント:

すべてのコメントを見る