Class: Retrobot

Inherits:
Object
  • 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

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/retrobot.rb', line 21

def config
  @config
end

Instance Method Details

#clientObject



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

#csvObject



44
45
46
47
48
49
50
51
52
# File 'lib/retrobot.rb', line 44

def csv
  @csv ||= begin
             tweets_csv = file_from_candidates(
               @config.tweets_csv,
               GEM_ROOT.join('tweets', 'tweets.csv'),
             )
             CSV.parse File.read(tweets_csv)
           end
end

#dying_messageObject



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..."
  tweet_text = if mention = @config.dying_mention_to
                 "#{mention} #{message}"
               else
                 message
               end
  twitter = TweetFilters::Tweet.new(self)
  twitter.tweet(tweet_text)
  logger.fatal message
end

#init_configurationObject



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 # for faster fail (e.g. wrong credentials given)
end

#init_csvObject



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

#loggerObject



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

#mainObject



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 tweet_loop
end

#parse_optionsObject



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 = Tweet.parse_line(line)

  tweet_filters.each do |filter|
    tweet = filter.filter(tweet)
    break unless tweet
  end

  true
rescue Twitter::Error
  logger.error "#{$!} (#{$!.class})\n  #{$@.join("\n  ")}"
  true
rescue Retrobot::TweetFilters::RetryLater
  false
end

#tweet_filtersObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/retrobot.rb', line 119

def tweet_filters
  return @tweet_filters if @tweet_filters
  @tweet_filters = []
  @tweet_filters << TweetFilters::RetroDays.new(self)
  @tweet_filters << TweetFilters::SuppressPattern.new(self) if @config.suppress_pattern
  @tweet_filters << TweetFilters::AddInReplyToUrl.new(self) if @config.add_in_reply_to_url
  @tweet_filters << TweetFilters::RemoveHashtag.new(self)   if @config.remove_hashtag
  @tweet_filters << TweetFilters::Retweet.new(self)
  @tweet_filters << TweetFilters::RemoveAtmark.new(self)
  @tweet_filters << TweetFilters::Unescape.new(self)
  @tweet_filters << TweetFilters::Tweet.new(self)
  @tweet_filters
end

#tweet_loopObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/retrobot.rb', line 74

def tweet_loop
  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