Class: Retrobot
- Inherits:
-
Object
show all
- Defined in:
- lib/retrobot.rb,
lib/retrobot/tweet.rb,
lib/retrobot/config.rb,
lib/retrobot/version.rb,
lib/retrobot/tweet_filters.rb,
lib/retrobot/tweet_filters/base.rb,
lib/retrobot/tweet_filters/tweet.rb,
lib/retrobot/tweet_filters/retweet.rb,
lib/retrobot/tweet_filters/unescape.rb,
lib/retrobot/tweet_filters/retro_days.rb,
lib/retrobot/tweet_filters/remove_atmark.rb,
lib/retrobot/tweet_filters/remove_hashtag.rb,
lib/retrobot/tweet_filters/suppress_pattern.rb,
lib/retrobot/tweet_filters/add_in_reply_to_url.rb
Defined Under Namespace
Modules: TweetFilters
Classes: Config, Tweet
Constant Summary
collapse
- GEM_ROOT =
Pathname.new('..').expand_path(__dir__)
- VERSION =
'0.3.5'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(argv) ⇒ Retrobot
Returns a new instance of Retrobot.
23
24
25
|
# File 'lib/retrobot.rb', line 23
def initialize(argv)
@argv = argv
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
21
22
23
|
# File 'lib/retrobot.rb', line 21
def config
@config
end
|
Instance Method Details
#client ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/retrobot.rb', line 27
def client
@client ||= Twitter::REST::Client.new do |config|
config.consumer_key = @config.consumer_key
config.consumer_secret = @config.consumer_secret
config.access_token = @config.access_token
config.access_token_secret = @config.access_secret
end
end
|
#csv ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/retrobot.rb', line 44
def csv
@csv ||= begin
= file_from_candidates(
@config.,
GEM_ROOT.join('tweets', 'tweets.csv'),
)
CSV.parse File.read()
end
end
|
#dying_message ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/retrobot.rb', line 91
def dying_message
message = "No data is left. Please update my tweets.csv. Pee.. Gaa..."
= if mention = @config.dying_mention_to
"#{mention} #{message}"
else
message
end
= TweetFilters::Tweet.new(self)
.()
logger.fatal message
end
|
#init_configuration ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/retrobot.rb', line 133
def init_configuration
options = parse_options()
@config = Config.new
config_yml = file_from_candidates(
options[:config], './retrobot.yml',
GEM_ROOT.join('retrobot.yml')
)
@config.load_yaml_file!(config_yml) if config_yml
@config.merge!(options)
client.current_user end
|
#init_csv ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/retrobot.rb', line 54
def init_csv
csv.slice! 0
last_index = nil
csv.each_with_index.each do |line, i|
time = Time.parse line[3]
if time < @config.retro_days.ago
last_index = i
break;
end
end
csv.slice! last_index..-1 if last_index
if csv.empty?
logger.fatal "No data is left. Please update the tweets.csv"
false
else
logger.info "Next update: \"#{csv.last[5]}\" at #{@config.retro_days.since(Time.parse(csv.last[3]))}"
true
end
end
|
#logger ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/retrobot.rb', line 36
def logger
@logger ||= begin
l = Logger.new($stdout)
l.level = @config.debug ? Logger::DEBUG : Logger::INFO
l
end
end
|
#main ⇒ Object
163
164
165
166
167
168
|
# File 'lib/retrobot.rb', line 163
def main
init_configuration
logger.info "Starting retrobot-#{Retrobot::VERSION}"
exit 1 unless init_csv
exit 1 unless
end
|
#parse_options ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/retrobot.rb', line 148
def parse_options
options = {}
opt = OptionParser.new @argv
opt.banner = "Usage: #{$0} [OPTIONS]"
opt.on('--debug') { options[:debug] = true }
opt.on('--dryrun') { options[:dryrun] = true }
opt.on('--config file') {|v| options[:config] = v }
opt.on('--retro-days days') {|v| options[:retro_days] = v }
opt.on('--tweets-csv path') {|v| options[:tweets_csv] = v }
opt.parse!
options
end
|
#process_line(line) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/retrobot.rb', line 103
def process_line(line)
= Tweet.parse_line(line)
.each do |filter|
= filter.filter()
break unless
end
true
rescue Twitter::Error
logger.error "#{$!} (#{$!.class})\n #{$@.join("\n ")}"
true
rescue Retrobot::TweetFilters::RetryLater
false
end
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/retrobot.rb', line 74
def
logger.info 'start'
loop do
line = csv.last
unless line
dying_message
return false
end
if process_line(line)
csv.pop
end
sleep @config.loop_interval
logger.debug '.'
end
true
end
|