Skip to content

Instantly share code, notes, and snippets.

@pewniak747
Created December 7, 2009 19:20
Show Gist options
  • Save pewniak747/251028 to your computer and use it in GitHub Desktop.
Save pewniak747/251028 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# tweet is a script that allows you to update
# your twitter status using shell
# written by Tomasz 'pewniak747' Pewiński
# pewniak747@gmail.com
def exp_value input
input[input.index("=")+1, input.size].strip
end
# set defaults
options = {}
options[:user] = 'twitt'
options[:password] = 'twitt'
message = String.new
#read from .tweetrc
if !File.exists?("#{ENV['HOME']}/.tweetrc")
rcfile = File.open("#{ENV['HOME']}/.tweetrc", "w")
puts "This the first time you run tweet script."
puts "Please provide your twittername and password."
puts "Can be changed later in #{ENV['HOME']}/.tweetrc"
print "[username:] "
rcfile << "user="+STDIN.gets
print "[password:] "
rcfile << "password="+STDIN.gets
rcfile.close
end
rcfile = File.open("#{ENV['HOME']}/.tweetrc")
rcfile.each do |line|
case line
when /^user/
options[:user] = exp_value line
when /^password/
options[:password] = exp_value line
end
end
rcfile.close
#parse arguments
ARGV.each_index do |idx|
case ARGV[idx]
when "-h", "--help"
options[:help] = true;
when /^--user=/
options[:user] = exp_value ARGV[idx]
puts options[:user]
when /^--password=/
options[:password] = exp_value ARGV[idx]
else
if idx != ARGV.size-1
puts "wrong argument #{ARGV[idx]}"
puts "see tweet --help for available options"
Process.exit
else
message = ARGV.last
end
end
end
# outputs help
if options[:help]
puts "Tweet - send twitter status update from shell!"
puts "Usage: tweet [OPTIONS] \"Tweeting from bash:)\""
puts "Available options:"
puts " --user=name set user to name"
puts " --password=pass set password to pass"
puts " -h, --help display this message"
Process.exit
end
# validation
errors = false
if message.nil?
puts "No message specified."
errors = true
elsif message.size > 140
puts "Tweet size is over 140 characters."
errors = true
elsif message.size < 1
puts "Tweet not specified."
errors = true
end
Process.exit if errors
system("curl --basic --show-error --stderr - --user #{options[:user]}:#{options[:password]} --data status=\"#{message}\" http://twitter.com/statuses/update.xml > $HOME/.tweetlog")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment