Class: Retrobot
- Inherits:
-
Object
- Object
- Retrobot
- Defined in:
- lib/retrobot.rb,
lib/retrobot/config.rb,
lib/retrobot/version.rb
Defined Under Namespace
Classes: Config
Constant Summary collapse
- GEM_ROOT =
Pathname.new('..').(__dir__)
- VERSION =
'0.2.0'
Instance Method Summary collapse
- #client ⇒ Object
- #csv ⇒ Object
- #init_configuration ⇒ Object
- #init_csv ⇒ Object
-
#initialize(argv) ⇒ Retrobot
constructor
A new instance of Retrobot.
- #logger ⇒ Object
- #main ⇒ Object
- #parse_options ⇒ Object
- #process_line(line) ⇒ Object
- #retweet(status_id, text = nil) ⇒ Object
- #tweet(text) ⇒ Object
- #tweet_loop ⇒ Object
Constructor Details
#initialize(argv) ⇒ Retrobot
Returns a new instance of Retrobot.
18 19 20 |
# File 'lib/retrobot.rb', line 18 def initialize(argv) @argv = argv end |
Instance Method Details
#client ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/retrobot.rb', line 22 def client @client ||= Twitter::Client.new( consumer_key: @config.consumer_key, consumer_secret: @config.consumer_secret, oauth_token: @config.access_token, oauth_token_secret: @config.access_secret ) end |
#csv ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/retrobot.rb', line 39 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 |
#init_configuration ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/retrobot.rb', line 112 def init_configuration = () @config = Config.new config_yml = file_from_candidates( [:config], './retrobot.yml', GEM_ROOT.join('retrobot.yml') ) @config.load_yaml_file!(config_yml) if config_yml @config.merge!() client.current_user # for faster fail (e.g. wrong credentials given) end |
#init_csv ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/retrobot.rb', line 49 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 logger.info "Next update: \"#{csv.last[5]}\" at #{@config.retro_days.since(Time.parse(csv.last[3]))}" end |
#logger ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/retrobot.rb', line 31 def logger @logger ||= begin l = Logger.new($stdout) l.level = @config.debug ? Logger::DEBUG : Logger::INFO l end end |
#main ⇒ Object
142 143 144 145 146 |
# File 'lib/retrobot.rb', line 142 def main init_configuration init_csv tweet_loop end |
#parse_options ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/retrobot.rb', line 127 def = {} opt = OptionParser.new @argv opt. = "Usage: #{$0} [OPTIONS]" opt.on('--debug') { [:debug] = true } opt.on('--dryrun') { [:dryrun] = true } opt.on('--config file') {|v| [:config] = v } opt.on('--retro-days days') {|v| [:retro_days] = v } opt.on('--tweets-csv path') {|v| [:tweets_csv] = v } opt.parse! end |
#process_line(line) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/retrobot.rb', line 75 def process_line(line) tweet_id, in_reply_to_status_id, in_reply_to_user_id, , source, text, retweeted_status_id, retweeted_status_user_id, , * = line = Time.parse().localtime return false if > @config.retro_days.ago if retweeted_status_id.present? retweet retweeted_status_id.to_i, text return true end tweet CGI.unescape_html(text.gsub('@', '')) true rescue Twitter::Error logger.error "#{$!} (#{$!.class})\n #{$@.join("\n ")}" true end |
#retweet(status_id, text = nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/retrobot.rb', line 96 def retweet(status_id, text=nil) logger.info "retweet: #{status_id} \"#{text}\"" return if @config.dryrun retryable(tries: @config.retry_count, sleep: @config.retry_interval) do client.retweet status_id end end |
#tweet(text) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/retrobot.rb', line 104 def tweet(text) logger.info "tweet: #{text}" return if @config.dryrun retryable(tries: @config.retry_count, sleep: @config.retry_interval) do client.update text end end |
#tweet_loop ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/retrobot.rb', line 63 def tweet_loop logger.info 'start' loop do line = csv.last if process_line(line) csv.pop end sleep @config.loop_interval logger.debug '.' end end |