Skip to content

Instantly share code, notes, and snippets.

@hvasconcelos
Last active December 20, 2015 01:39
Show Gist options
  • Save hvasconcelos/6050262 to your computer and use it in GitHub Desktop.
Save hvasconcelos/6050262 to your computer and use it in GitHub Desktop.
Twitter Timeline Rotator
#!/usr/bin/env ruby
require "rubygems"
require "twitter"
require "colorize"
require "oauth"
require "yaml"
class TwitterRotator
attr_writer :tweet_func
AUTH_FILE=File.join(ENV["HOME"],".tweet_rotator")
def initialize( options = {} )
authorize unless File.exists? AUTH_FILE
@auth = YAML.load(File.open(AUTH_FILE))
@options = {
:refresh_rate => 300 ,
:rotate_time => 10 ,
:tweet_count => 30
}.merge(options)
@client = Twitter::Client.new(
:consumer_key => @auth["consumer_key"] ,
:consumer_secret => @auth["consumer_secret"] ,
:oauth_token => @auth["token"] ,
:oauth_token_secret => @auth["token_secret"] )
@tweets = []
@ci = -1
end
def run
@sl = Thread.new do
while 1
@tweets.clear
@tweets = @tweet_func.call( @client ,@options[:tweet_count])
sleep(@options[:refresh_rate])
end
end
@rt = Thread.new do
rotate
end
end
def authorize
auth={}
puts "First, go register a new application at "
puts "https://dev.twitter.com/apps/new"
puts "Enter the consumer key you are assigned:"
auth["consumer_key"] = gets.strip
puts "Enter the consumer secret you are assigned:"
auth["consumer_secret"] = gets.strip
puts "Your application is now set up, but you need to register"
puts "this instance of it with your user account."
consumer=OAuth::Consumer.new auth["consumer_key"], auth["consumer_secret"], {:site=>"https://api.twitter.com"}
request_token = consumer.get_request_token
puts "Visit the following URL, log in if you need to, and authorize the app"
puts request_token.authorize_url
puts "When you've authorized that token, enter the verifier code you are assigned:"
verifier = gets.strip
puts "Converting request token into access token..."
access_token=request_token.get_access_token(:oauth_verifier => verifier)
auth["token"] = access_token.token
auth["token_secret"] = access_token.secret
File.open(AUTH_FILE, 'w') {|f| YAML.dump(auth, f)}
puts "Done. Have a look at #{AUTH_FILE} to see what's there."
end
def stop
@sl.exit
@rt.exit
end
def next_tweet
if @tweets.count > 0
@ci = (@ci < (@tweets.count-1))? @ci+1 : 0
return @tweets[@ci]
end
return nil
end
def rotate
while 1
tweet = next_tweet
if tweet!=nil
puts " "+tweet[:user][:name].colorize(:yellow)
puts " "+tweet[:text]+"\n"*2
end
sleep( @options[:rotate_time] )
end
end
end
rotator = TwitterRotator.new
rotator.tweet_func = -> ( client, tweet_count ) {
client.home_timeline :count => tweet_count
}
rotator.run
while 1
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment