Skip to content

Instantly share code, notes, and snippets.

@hotgazpacho
Created January 3, 2013 03:35
Show Gist options
  • Save hotgazpacho/4440552 to your computer and use it in GitHub Desktop.
Save hotgazpacho/4440552 to your computer and use it in GitHub Desktop.
tweet a message with a cute baby animal image
require 'optparse'
require 'google-search'
require 'twitter'
options = {}
optparse = OptionParser.new do |opts|
opts.on('-t', '--tweet MESSAGE', "Message to tweet") do |f|
options[:tweet] = f
end
opts.on('-k', '--consumer-key TWITTER_CONSUMER_KEY', "Twitter Consumer Key") do |f|
options[:consumer_key] = f
end
opts.on('-s', '--consumer-secret TWITTER_CONSUMER_SECRET', "Twitter Consumer Secret") do |f|
options[:consumer_secret] = f
end
opts.on('-o', '--oauth-token TWITTER_OAUTH_TOKEN', "Your Twitter OAuth Token") do |f|
options[:oauth_token] = f
end
opts.on('-a', '--oauth-token-secret TWITTER_OAUTH_TOKEN_SECRET', "Your Twitter OAuth Token Secret") do |f|
options[:oauth_token_secret] = f
end
end
begin
optparse.parse!
options[:consumer_key] ||= ENV['TWITTER_CONSUMER_KEY']
options[:consumer_secret] ||= ENV['TWITTER_CONSUMER_SECRET']
options[:oauth_token] ||= ENV['TWITTER_OAUTH_TOKEN']
options[:oauth_token_secret] ||= ENV['TWITTER_OAUTH_TOKEN_SECRET']
mandatory = [:tweet, :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts optparse
exit
end
g = Google::Search::Image.new(:query => 'cute baby animals')
image = g.entries.sample
tweet = "#{options[:tweet]} #{image.uri}"
puts "Tweeting: #{tweet}"
twitter = Twitter::Client.new(options)
twitter.update(tweet)
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment