This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
184 lines (167 loc) · 5.6 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
require 'twitter'
require 'user_stream'
require './markov'
def puts(*args)
STDERR.puts(*args)
end
def check_status(status)
status.source.gsub!(/<[^>]+>/, '')
status.text.gsub!(/&(?:amp|[gl]t);/, '&' => '&', '>' => '>', '<' => '<')
status.text.gsub!(/[@@##]\w+|殺|https?:\/\/t.co\/\w+|[rqrqRQ][ttT].*/im, '')
status.text.strip!
if status.user.protected ||
status.user.id == @user.id ||
status.text.empty? ||
(status.in_reply_to_user_id && status.in_reply_to_user_id != @user.id) ||
[
'twittbot.net',
'EasyBotter',
'ツイ助。',
'MySweetBot',
'BotMaker'
].include?(status.source)
return nil
end
[status.text, status.id]
end
options = {
:consumer_key => ENV['CONSUMER_KEY'],
:consumer_secret => ENV['CONSUMER_SECRET'],
:oauth_token => ENV['ACCESS_TOKEN'],
:oauth_token_secret => ENV['ACCESS_TOKEN_SECRET'],
}
twitter = Twitter::Client.new(options)
stream = UserStream::Client.new(options)
@user = twitter.verify_credentials
@markov = Markov.new(twitter.home_timeline(:count => 200).map { |status| check_status(status) }.compact.reverse)
@blocking = twitter.blocked_ids.ids
@following = []
puts "Ready (Bot: @#{@user.screen_name})"
Thread.abort_on_exception = true
Thread.new do
friends = []
cursor = -1
while cursor.nonzero?
result = twitter.friend_ids(@user, :cursor => cursor)
cursor = result.next_cursor
friends.push(*result.ids)
end
followers = []
cursor = -1
while cursor.nonzero?
result = twitter.follower_ids(@user, :cursor => cursor)
cursor = result.next_cursor
followers.push(*result.ids)
end
unfollowers = friends - followers
unless unfollowers.empty?
twitter.friendships(unfollowers).each do |user|
next if user[:connections].include?('followed_by')
puts "Unfollowed by @#{user.screen_name}. Unfollowing..."
begin
twitter.unfollow(user)
rescue => e
puts "#{e.class} unfollowing @#{user.screen_name}: #{e}"
else
puts "Unfollowed @#{user.screen_name}"
end
end
end
new_followers = followers - friends
unless new_followers.empty?
twitter.friendships(new_followers).each do |user|
next if user[:connections].include?('following')
next if user[:connections].include?('following_requested')
puts "Followed by @#{user.screen_name}. Following back..."
begin
twitter.follow!(user)
rescue => e
puts "#{e.class} following @#{user.screen_name}: #{e}"
else
puts "Followed @#{user.screen_name}"
end
end
end
end
Thread.new do
min_regexp = /#{ENV['MIN_REGEXP'] ? ENV['MIN_REGEXP'] : 25}/
last = nil
loop do
sleep(1)
now = Time.now
if min_regexp =~ now.min.to_s && (last.nil? || last.hour != now.hour || last.min != now.min)
begin
message = @markov.create
puts "Scheduled post at #{now.hour}:#{now.min}: #{message}"
result = twitter.update(message)
rescue => e
puts "#{e.class} posting: #{e}"
if e.to_s =~ /duplicate/
count ||= 0
count += 1
retry if count < 10
end
else
puts "Posted: #{result.text}"
last = now
end
end
end
end
callback = Proc.new do |status|
Thread.new do
if status.text && status.user.id != @user.id && !@blocking.include?(status.user.id) && @following.include?(status.user.id)
if status.text =~ /[@@]#{@user.screen_name}(?!\w)|\A#{@user.name}へ[$。、\s ]/ && status.text !~ /[rqrqRQ][ttT]/i
puts "Mention from @#{status.user.screen_name}: #{status.text}"
begin
message = "@#{status.user.screen_name} #{@markov.create}"[0...140]
puts "Sending reply: #{message}"
result = twitter.update(message, :in_reply_to_status_id => status.id)
rescue => e
puts "#{e.class} sending reply: #{e}"
if e.to_s =~ /duplicate|over capacity/
count ||= 0
count += 1
retry if count < 10
end
else
puts "Replied: #{result.text}"
end
end
text = check_status(status)
if text
puts "Adding to table: @#{status.user.screen_name}: #{text[0]}"
@markov.add(*text)
end
elsif status.friends
@following = status.friends
elsif status.event == 'follow' && status.target.id == @user.id
puts "Followed by @#{status.source.screen_name}. Following back..."
begin
result = twitter.follow!(status.source.screen_name)[0]
rescue => e
puts "#{e.class} following @#{status.source.screen_name}: #{e}"
else
puts "Followed @#{result.screen_name}"
@following.push(result.id)
end
elsif status.event == 'unfollow'
puts "Unfollowed @#{status.target.screen_name}"
@following.delete(status.target.id)
elsif status[:delete] && status[:delete].status
puts "Deleted from table: #{@markov.delete(status[:delete].status.id_str)}"
elsif status.event == 'block' && status.source.id == @user.id
@blocking.push(status.target.id)
@following.delete(status.target.id)
puts "Blocked @#{status.target.screen_name}"
elsif status.event == 'unblock' && status.source.id == @user.id
@blocking.delete(status.target.id)
puts "Unblocked @#{status.target.screen_name}"
end
end
end
loop do
stream.user(:replies => :all, &callback)
sleep(300)
end