Skip to content

Instantly share code, notes, and snippets.

@eLafo
Last active March 29, 2017 16:05
Show Gist options
  • Save eLafo/03f3e7ac36399918deab78c309b6cbd8 to your computer and use it in GitHub Desktop.
Save eLafo/03f3e7ac36399918deab78c309b6cbd8 to your computer and use it in GitHub Desktop.
require 'twitter'
require 'logger'
TWITTER_LIST_SLUG = 'aspgems-developers'
TRACKED_WORDS = %W(aspgems_dev #dev)
logger = Logger.new('log/aspgemsdev.log')
logger.level = Logger::INFO
def initialize_client(config)
config.consumer_key = 'your_consumer_key'
config.consumer_secret = 'your_consumer_secret'
config.access_token = 'your_access_token'
config.access_token_secret = 'your_access_token_secret'
end
def should_retweet?(object)
TRACKED_WORDS.any? {|w| object.text.include? w} && object.is_a?(Twitter::Tweet) && !object.retweeted_tweet?
end
rest_client = Twitter::REST::Client.new do |config|
logger.info("Initializing Rest Client")
initialize_client config
end
stream_client = Twitter::Streaming::Client.new do |config|
logger.info("Initializing Streaming Client")
initialize_client config
end
logger.info "Loading #{TWITTER_LIST_SLUG} list"
list = rest_client.lists(slug: TWITTER_LIST_SLUG)
users_to_follow = rest_client.list_members(slug: TWITTER_LIST_SLUG)
logger.info("Following these users:\n" + users_to_follow.map{|m| "#{m.screen_name} - #{m.id}"}.join("\n"))
users_to_follow = users_to_follow.map(&:id)
begin
stream_client.filter(follow: users_to_follow.join(',')) do |object|
logger.info(object.inspect)
if should_retweet?(object)
rest_client.retweet(object)
logger.info("RT #{object.id}")
end
logger.warn("Stall warning!!!") if object.is_a?(Twitter::Streaming::StallWarning)
end
rescue Twitter::Error::TooManyRequests
sleep 1.hour
retry
rescue => e
logger.error(e)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment