From a6708d3d25db894b478fe484429da19f5afa8d16 Mon Sep 17 00:00:00 2001 From: wjoe Date: Tue, 7 Feb 2012 13:16:28 +0000 Subject: Historic commit 2.0.0 - Added authentication and posting to other accounts (05-Feb-2012 23:24) --- blatweet.rb | 92 ++++++++++++++++++++++++++++++++++++++++++++------------ config.yaml | 4 +-- persistence.yaml | 5 +++ shoes.yaml | 4 +++ 4 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 persistence.yaml create mode 100644 shoes.yaml diff --git a/blatweet.rb b/blatweet.rb index 2b19016..4ed3a40 100644 --- a/blatweet.rb +++ b/blatweet.rb @@ -1,59 +1,113 @@ #!/usr/bin/env ruby -require 'twitter' +require 'rubygems' +require 'twitter_oauth' require 'optparse' +require 'json' +require 'yaml' output = [] - username = nil number_of_tweets = 1 +message = nil limit = false -version = 'v1.0.5' +debug = false +version = 'v1.1.0' help = false +irc_nick = nil ops = nil STDIN.gets.split(' ').each{|arg| ARGV << arg} if ARGV == [] args = ARGV.dup options = OptionParser.new do |opts| - opts.banner = "Usage: #{File.basename($0)} -u USERNAME -n NO_OF_TWEETS" + opts.banner = "Usage: #{File.basename($0)} -h" opts.on( "-u", "--username USERNAME", "Twitter username to search for." ) { |u| username = u } opts.on( "-n", "--number NO_OF_TWEETS", "Number of tweets to return." ) { |n| number_of_tweets = n } + opts.on( '-p', '--post MESSAGE', 'Post a message to your twitter account.' ) { |m| message = m } opts.on( '-l', '--limit', 'Display remaining hits' ) { limit = true } + opts.on( '-d', '--debug', 'Display debug info.' ) { debug = true } opts.on( "-v", "--version", "blatweet version number." ) { puts version; exit } opts.on( '-h', '--help', 'Display this screen.' ) { help = true; ops = opts } + opts.on( '-a', '--auth NICK', 'IRC nick to auth. Set by BB.' ) { |n| irc_nick = n } end;options.parse! +output << "args: #{args.inspect}" if debug if args == [] puts "Usage: #{File.basename($0)} -h" exit(-1) end -begin +def oauth_client irc_nick, config + shoes = get_shoes irc_nick, config + TwitterOAuth::Client.new(:consumer_key => config['consumer_key'], :consumer_secret => config['consumer_secret'], :token => shoes['token'], :secret => shoes['secret']) +end - twitter_config = YAML::load( File.open( 'config.yaml' ) ) - - Twitter.configure do |config| - config.consumer_key = twitter_config['consumer_key'] - config.consumer_secret = twitter_config['consumer_secret'] - config.oauth_token = twitter_config['oauth_token'] - config.oauth_token_secret = twitter_config['oauth_token_secret'] - end +def get_shoes irc_nick, config + all_shoes = YAML::load( File.open( 'shoes.yaml' ) ) + all_shoes[irc_nick] ? all_shoes[irc_nick] : ask_for_shoes(irc_nick, config) +end + +def ask_for_shoes irc_nick, config + client = TwitterOAuth::Client.new(:consumer_key => config['consumer_key'], :consumer_secret => config['consumer_secret']) + request_token = client.request_token(:oauth_callback => config['callback_url']) + listen_for_shoes irc_nick, config, client, request_token + puts "#{irc_nick}, your shoes are not in the cupboard! Please give me your shoes: #{request_token.authorize_url}" +end + +def listen_for_shoes irc_nick, config, client, request_token + persistence = YAML::load( File.open( 'persistence.yaml' ) ) + + if persistence['listening'] == true + puts "Waiting for #{persistence['irc_nick']} to give me their shoes. Please try again in a minute. Sorry that this is a bit hacky." + else + persistence['listening'] = true + persistence['irc_nick'] = irc_nick + + persistence['pid'] = fork do + $stdout.reopen("/dev/null", "w") + $stderr.reopen("/dev/null", "w") + require 'sinatra' + set :port, config['sinatra_port'] + set :logging, false + + get '/these_are_my_shoes' do + access_token = client.authorize request_token.token, request_token.secret, :oauth_verifier => params[:oauth_verifier] + if client.authorized? + open('shoes.yaml', 'a'){ |f| f.puts "#{irc_nick}:\n token: #{access_token.token}\n secret: #{access_token.secret}" } + end + persistence = YAML::load( File.open( 'persistence.yaml' ) ) + persistence['listening'] = false + Process.kill 'TERM', persistence['pid'] + open('persistence.yaml', 'w'){ |f| f.puts persistence.to_yaml } + "Thanks, #{irc_nick}. Your shoes are now in the cupboard." + end + end + open('persistence.yaml', 'w'){ |f| f.puts persistence.to_yaml } + end +end + +begin + config = YAML::load( File.open( 'config.yaml' ) ) + client = TwitterOAuth::Client.new if help ops.to_s.split(/\n\s*|\s{2,}/).each{|opt| output << opt} puts output.join(' ') elsif limit - remaining_calls = Twitter.rate_limit_status.remaining_hits - puts remaining_calls == 0 ? "Limit reached. Limit resets in #{60 - Time.now.min} minutes." : "Remaining calls this hour: #{remaining_calls}. #{remaining_calls/(60 - Time.now.min)} calls/minute." + rate_limit_status = oauth_client(irc_nick, config).rate_limit_status + remaining_hits = rate_limit_status['remaining_hits'] + reset_time = DateTime.parse rate_limit_status['reset_time'] + puts remaining_hits == 0 ? "Limit reached. Limit resets in #{(60 + reset_time.min) - Time.now.min} minutes." : "Remaining hits this hour: #{remaining_hits}. #{remaining_hits/((60 + reset_time.min) - Time.now.min)} hits/minute." + elsif message + response = oauth_client(irc_nick, config).update message + puts response['error'] elsif username - Twitter.search("from:#{username}", :rpp => number_of_tweets, :result_type => "recent").each_with_index{|tweet, index| output << "#{index+1}: #{tweet.text}"} + client.search("from:#{username}", :rpp => number_of_tweets)['results'].each_with_index{|tweet, index| output << "#{index+1}: #{tweet['text']}"} puts output.join ' ' else - Twitter.update ARGV.join ' ' + oauth_client('intensemarcus', config).update ARGV.join(' ') end -rescue Twitter::Error => e - puts e.message rescue StandardError => e puts e.message end diff --git a/config.yaml b/config.yaml index 4af3639..9e8af0f 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,4 @@ consumer_key: vXaBVIrxiJw2TKVJyI1w consumer_secret: Xrg8ezA461OlLP94951kWxONMs9CkDCVkW25KZICjY8 -oauth_token: 143847685-F9qvpDaAyHMeuE86yMm8sBd6KhDswQAeoUM0hFoJ -oauth_token_secret: PstQH40WKY1ER23MbcdihxoVPGL5vA0KuaCtr9PnE +callback_url: http://tghost.co.uk:4291/these_are_my_shoes +sinatra_port: 4291 diff --git a/persistence.yaml b/persistence.yaml new file mode 100644 index 0000000..da8e832 --- /dev/null +++ b/persistence.yaml @@ -0,0 +1,5 @@ +--- +listening: false +irc_nick: +pid: + diff --git a/shoes.yaml b/shoes.yaml new file mode 100644 index 0000000..fdc3c40 --- /dev/null +++ b/shoes.yaml @@ -0,0 +1,4 @@ +--- +intensemarcus + token: 143847685-F9qvpDaAyHMeuE86yMm8sBd6KhDswQAeoUM0hFoJ + secret: PstQH40WKY1ER23MbcdihxoVPGL5vA0KuaCtr9PnE -- cgit v1.2.3