#!/usr/bin/env ruby require 'twitter' require 'optparse' username = nil number_of_tweets = 1 output = [] version = 1.03 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( '-h', '--help', 'Display this screen' ) { puts opts; exit } opts.on( '-l', '--limit', 'Display remaining hits' ) { puts Twitter.rate_limit_status.remaining_hits.to_s + " tweets remaining."; exit } opts.on( '-v', '--version', 'Display program version' ) { puts version } end;options.parse! if args == [] puts options exit(-1) 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 begin if 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