#!/usr/bin/env ruby require 'twitter' require 'optparse' output = [] username = nil number_of_tweets = 1 limit = false version = 'v1.0.5' help = false 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.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( '-l', '--limit', 'Display remaining hits' ) { limit = true } opts.on( "-v", "--version", "blatweet version number." ) { puts version; exit } opts.on( '-h', '--help', 'Display this screen.' ) { help = true; ops = opts } end;options.parse! if args == [] puts "Usage: #{File.basename($0)} -h" exit(-1) end begin 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 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." elsif username Twitter.search("from:#{username}", :rpp => number_of_tweets, :result_type => "recent").each_with_index{|tweet, index| output << "#{index+1}: #{tweet.text}"} puts output.join ' ' else Twitter.update ARGV.join ' ' end rescue Twitter::Error => e puts e.message rescue StandardError => e puts e.message end