#!/usr/bin/env ruby require 'rubygems' require 'twitter_oauth' require 'optparse' require 'json' require 'yaml' output = [] username = nil number_of_tweets = 1 message = nil limit = false debug = false version = 'v2.1.0' help = false options = false irc_nick = nil ops = nil ARGV[2].split(' ').each{|arg| ARGV << arg} if ARGV[2] ARGV.delete_at 2 STDIN.gets.split(' ').each{|arg| ARGV << arg} if ARGV == [] args = ARGV.dup options = OptionParser.new do |opts| 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 use examples' ) { help = true } opts.on( '-o', '--options', 'Display this screen.' ) { options = 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 def post_message account, message puts "Your message is #{message.length} characters." if message.length > 140 response = oauth_client(account, config).update message puts response['error'] ? response['error'] : "Posted tweet." end 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 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 options ops.to_s.split(/\n\s*|\s{2,}/).each{|opt| output << opt} puts output.join(' ') elsif help puts '!tweet MESSAGE (post to intensemarcus), !tweet -u USERNAME -n NUMBER (show the last -n tweets from -u), !tweet -p MESSAGE (post to your account), !tweet -l (remaining api hits), !tweet -v (blatweet verion), !tweet -h (this message), !tweet -o (full options), !tweet -d (debug info).' elsif limit 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 post_message irc_nick, message elsif username client.search("from:#{username}", :rpp => number_of_tweets)['results'].each_with_index{|tweet, index| output << "#{index+1}: #{tweet['text']}"} puts output.join ' ' else post_message 'intense_marcus', ARGV.join(' ') end rescue StandardError => e puts e.message end