diff options
Diffstat (limited to 'blatweet.rb')
-rw-r--r-- | blatweet.rb | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/blatweet.rb b/blatweet.rb index fd9106c..5777126 100644 --- a/blatweet.rb +++ b/blatweet.rb @@ -2,10 +2,14 @@ require 'twitter' require 'optparse' +output = [] + username = nil number_of_tweets = 1 -output = [] -version = 1.03 +limit = false +version = 'v1.0.4' +help = false +ops = nil STDIN.gets.split(' ').each{|arg| ARGV << arg} if ARGV == [] args = ARGV.dup @@ -14,32 +18,40 @@ 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 } + 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 options + puts "Usage: #{File.basename($0)} -h" exit(-1) end -twitter_config = YAML::load( File.open( 'config.yaml' ) ) +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 + 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 + 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 |