From d2630e79f87be6d29bedd0aaff03d76b03365c98 Mon Sep 17 00:00:00 2001 From: klea Date: Sun, 14 Dec 2025 00:20:28 +0000 Subject: [PATCH] Remove TwitterTweeter cog I left the option in because if someone's running AB with the option, and they update, this change would make it not start up, whilst now it'll just send to stderr that it's a deprecated option --- cogs/start.rb | 14 +---- cogs/twitter_conf.json | 7 --- cogs/twitter_tweeter.rb | 130 ---------------------------------------- 3 files changed, 3 insertions(+), 148 deletions(-) delete mode 100644 cogs/twitter_conf.json delete mode 100644 cogs/twitter_tweeter.rb diff --git a/cogs/start.rb b/cogs/start.rb index 198ef26b..75fc013c 100644 --- a/cogs/start.rb +++ b/cogs/start.rb @@ -8,7 +8,6 @@ require File.expand_path('../../lib/redis_subscriber', __FILE__) require File.expand_path('../../lib/shared_config', __FILE__) require File.expand_path('../reaper', __FILE__) -require File.expand_path('../twitter_tweeter', __FILE__) opts = Trollop.options do opt :redis, 'URL of Redis server', :default => ENV['REDIS_URL'] || 'redis://localhost:6379/0' @@ -16,7 +15,7 @@ opt :db_credentials, 'Credentials for history database (USERNAME:PASSWORD)', :type => String, :default => nil opt :log_db, 'URL of CouchDB log database', :default => ENV['LOGDB_URL'] || 'http://localhost:5984/archivebot_logs' opt :log_db_credentials, 'Credentials for log database (USERNAME:PASSWORD)', :type => String, :default => nil - opt :twitter_config, 'Filename containing Twitter key config', :type => String, :default => nil + opt :twitter_config, 'Deprecated option', :type => String, :default => nil # if not used anymore, can be removed end class Broadcaster < RedisSubscriber @@ -25,15 +24,12 @@ def on_receive(ident) return unless job job.freeze - - Celluloid::Actor[:twitter_tweeter].async.process(job) end end db_uri = URI(opts[:db]) Reaper.supervise_as :reaper, opts[:redis] -TwitterTweeter.supervise_as :twitter_tweeter, opts[:redis], opts[:twitter_config] ignore_patterns_path = File.expand_path('../../db/ignore_patterns', __FILE__) @@ -45,16 +41,12 @@ def on_receive(ident) UserAgentUpdater.supervise_as :user_agent_updater, user_agents_path, db_uri, opts[:db_credentials] -# This should be started after all actors it references have started, which is -# why it's the last actor to start up. + if opts[:twitter_config] - Broadcaster.supervise_as :broadcaster, opts[:redis], SharedConfig.log_channel + STDERR.puts "twitter_config option is now deprecated" end at_exit do - if opts[:twitter_config] - Celluloid::Actor[:broadcaster].stop - end Celluloid::Actor[:ignore_pattern_updater].stop Celluloid::Actor[:user_agent_updater].stop end diff --git a/cogs/twitter_conf.json b/cogs/twitter_conf.json deleted file mode 100644 index d0a88107..00000000 --- a/cogs/twitter_conf.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "username": "YOUR_TWITTER_HANDLE", - "consumer_key": "YOUR_CONSUMER_KEY", - "consumer_secret": "YOUR_CONSUMER_SECRET", - "access_token": "YOUR_ACCESS_TOKEN", - "access_token_secret": "YOUR_ACCESS_SECRET" -} diff --git a/cogs/twitter_tweeter.rb b/cogs/twitter_tweeter.rb deleted file mode 100644 index ab47edd7..00000000 --- a/cogs/twitter_tweeter.rb +++ /dev/null @@ -1,130 +0,0 @@ -require 'twitter' -require 'json' - -# Updates a Twitter timeline. It uses Redis as the persistent store to keep -# track of posted Tweets. The queue is a ordered set that simply holds the -# messages to be posted. The done set uses a ordered set with the timestamp as -# the score so really old Tweets can be cleaned out. - -class TwitterTweeter - include Celluloid - include Celluloid::Logger - - REDIS_KEY_DONE = 'tweets:done' - REDIS_KEY_QUEUE = 'tweets:queue' - REMOVE_OLD_INTERVAL = 86400 - # Adjust times carefully. Because we are posting URLs, we need to watch - # out for auto account suspensions! (If that happens, sign in and fill out - # the you've-been-bad captcha to get the account back.) - PROCESS_QUEUE_INTERVAL = 120 - TWEET_DELAY = 120 - def initialize(redis, twitter_config_filename) - return if !twitter_config_filename - - @redis = ::Redis.new(:url => redis) - twitter_config = JSON.load(File.open(twitter_config_filename)) - - authenticate_account(twitter_config) - - @configured = true - @processing = false - @remove_old_timer = every(REMOVE_OLD_INTERVAL) { remove_old_messages } - @processing_timer = every(PROCESS_QUEUE_INTERVAL) { process_queue } - - async.process_queue - async.remove_old_messages - end - - def process(job) - return unless @configured - - message = nil - - if job.in_progress? - message = "Archiving #{job.url}" - end - - if message and !message_queued?(message) and !message_posted?(message) - queue_message(message) - end - end - - private - - def authenticate_account(config) - username = config.delete('username') - @client = Twitter::REST::Client.new(config) - - @client.user(username) - - info "Twitter authentication success." - end - - def queue_message(message) - debug "Queue message: #{message}" - @redis.zadd(REDIS_KEY_QUEUE, Time.now.to_i, message) - end - - def message_queued?(message) - score = @redis.zscore(REDIS_KEY_QUEUE, message) - return !score.nil? - end - - def message_posted?(message) - score = @redis.zscore(REDIS_KEY_DONE, message) - return !score.nil? - end - - def remove_old_messages - time = Time.now.to_i - 2592000 - - debug "Removing old messages older than #{time}." - @redis.zremrangebyscore(REDIS_KEY_DONE, 0, time) - end - - def process_queue - return if @processing - - @processing = true - - while true - message_array = @redis.zrange(REDIS_KEY_QUEUE, 0, 1) - - break if message_array.length == 0 - - message = message_array[0] - - if !message_posted?(message) - post_message(message) - sleep(TWEET_DELAY) - end - - @redis.zrem(REDIS_KEY_QUEUE, message) - end - - @processing = false - end - - def post_message(message) - tries_left = 20 - - begin - debug "Attempt to Tweet: #{message}." - @client.update(message) - debug "Tweet OK." - rescue Twitter::Error => error - error "Tweet went wrong: #{error}." - - tries_left -= 1 - - if tries_left > 0 - sleep(30) - retry - else - warn "Gave up attempt to Tweet, discarding!" - end - end - - @redis.zadd(REDIS_KEY_DONE, Time.now.to_i, message) - end -end