-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.rb
72 lines (60 loc) · 1.8 KB
/
watch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!ruby -Ku
require 'tweetstream'
require 'uri'
require './search'
require './tweet'
require './bot'
class Watch
def initialize(settings)
TweetStream.configure do |config|
config.consumer_key = settings['consumer_key']
config.consumer_secret = settings['consumer_secret']
config.oauth_token = settings['oauth_token']
config.oauth_token_secret = settings['oauth_token_secret']
config.auth_method = :oauth
end
@bots = Array.new
end
def add_bot(bot)
@bots << bot
end
def start
client = TweetStream::Client.new
client.on_timeline_status do |status|
on_new_status(status)
end
target_ids = @bots.map do |e| e.target_id end
client.follow(target_ids)
end
def on_new_status(status)
begin
# followは、その人への in-reply-to や retweet も飛んでくる
# 本人の発言以外を除外
bot = @bots.find {|e| e.target_id == status.user.id}
return if !bot
# puts "#{status.user.id} #{status.user.screen_name} #{status.text}"
# pp status
original_id = bot.find_original_id(status)
if original_id
bot.retweet(original_id)
puts "#{bot.target}: ok: #{status.id} -> #{original_id}"
end
rescue Twitter::Error::BadRequest => e
# よくあるのはRate limit
puts "#{e} (#{e.class})"
puts e.backtrace
rescue Twitter::Error::Forbidden => e
# 鍵アカの場合かな
puts "#{e} (#{e.class})"
puts e.backtrace
rescue Timeout::Error => e
# httpのタイムアウト。検索で起きた場合はリトライしたいかも
puts "#{e} (#{e.class})"
puts e.backtrace
rescue => e
# 不明なエラーのときも、とりあえず動き続ける
puts "#{e} (#{e.class})"
puts e.backtrace
end
end
end