Posted By

noah on 06/05/08


Tagged

command Bash post ruby cli twitter textonly quickpost


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

webstic
4rp70x1n
ab1


Post to Twitter from the shell


 / Published in: Ruby
 

URL: http://snipplr.com/view/2352/twitter-from-the-command-line-using-curl/

The Twitter shell script is now hosted on GitHub. This (old) version is left in place for historical purposes.

Put this in a file called tweet.rb, then call as follows: ruby tweet.rb username "my tweet"

Or you can include your password in the command like so ruby twit.rb username:password "my tweet"

(note that this is hugely insecure as your password will wind up in your .history, not to mention being visible to shouldersurfers).

Updated: if a post is over the 140-character limit, the script prints back the part of the tweet that would not fit. This makes it easier to trim down overly long posts.

  1. #!/usr/bin/env ruby
  2. #Usage:
  3. # ruby tweet.rb username:password "status message"
  4.  
  5. def tweet (arguments)
  6. user, twit = arguments
  7. response = `curl -s -u #{user} -d status="#{twit}" http://twitter.com/statuses/update.xml | grep truncated`
  8. unless (response =~ /truncated>false</)
  9. puts "fail: Tweet failed. Check your user name and password."
  10. end
  11. end
  12.  
  13. if (ARGV[1].length > 140)
  14. overlimit = ARGV[1].length - 140
  15. offendingSubstring = ARGV[1][140, ARGV[1].length-1]
  16. puts %Q{FAIL: #{overlimit} characters over the 140-character limit:}
  17. puts %Q{\t"...#{offendingSubstring}"}
  18. elsif (ARGV.length != 2) ||
  19. (ARGV[1] == "")
  20. puts %Q{usage: ruby tweet.rb <username> "This will be tweeted to Twitter."}
  21. else
  22. tweet(ARGV)
  23. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: oddlyzen on February 2, 2009

Awesome, Noah. Thanks.

You need to login to post a comment.