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

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

  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. i = 0
  78. break
  79. end
  80. else
  81. if tmp[k] != nil
  82. puts "You have a new follower of id:#{tmp[k].to_i}. Let's follow."
  83. Twitter.friendship_create(tmp[k].to_i)
  84. i = 0
  85. break
  86. else
  87. break
  88. end
  89. end
  90. i += 1
  91. end
  92. k += 1
  93. end
  94. f1.close
  95.  
  96. print j
  97.  
  98. f4 = File::open("followers.txt", "w")
  99. i =0
  100. while i < j+1
  101. f4.puts "#{tmp[i]}"
  102. i += 1
  103. end
  104.  
  105. f4.close
  106. f3.close
  107.  

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

すべてのコメントを見る

タグ:

Ruby bot OK
最終更新:2011年07月06日 01:25