The CLI takes syntactic cues from the Twitter SMS commands, however it offers vastly more commands and capabilities than are available via SMS.
First, make sure you have Ruby installed.
On a Mac, open /Applications/Utilities/Terminal.app
and type:
ruby -v
If the output looks something like this, you're in good shape:
ruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-darwin12.2.1]
If the output looks more like this, you need to [install Ruby][ruby]: [ruby]: http://www.ruby-lang.org/en/downloads/
ruby: command not found
On Windows, you can install Ruby with [RubyInstaller][]. [rubyinstaller]: http://rubyinstaller.org/
Once you've verified that Ruby is installed:
gem install t
To ensure the code you're installing hasn't been tampered with, it's recommended that you verify the signature. To do this, you need to add my public key as a trusted certificate (you only need to do this once):
gem cert --add <(curl -Ls https://gist.github.com/sferik/4701180/raw/public_cert.pem)
Then, install the gem with the high security trust policy:
gem install t -P HighSecurity
Twitter API v1.1 requires OAuth for all of its functionality, so you'll need a registered Twitter application. If you've never registered a Twitter application before, it's easy! Just sign-in using your Twitter account and the fill out the short form at http://dev.twitter.com/apps/new. If you've previously registered a Twitter application, it should be listed at http://dev.twitter.com/apps. Once you've registered an application, make sure to set your application's Access Level to "Read, Write and Access direct messages", otherwise you'll receive an error that looks like this:
Error processing your OAuth request: Read-only application cannot POST
Now, you're ready to authorize a Twitter account with your application. To proceed, type the following command at the prompt and follow the instructions:
t authorize
This command will direct you to a URL where you can sign-in to Twitter,
authorize the application, and then enter the returned PIN back into the
terminal. If you type the PIN correctly, you should now be authorized to use
t
as that user. To authorize multiple accounts, simply repeat the last step,
signing into Twitter as a different user.
You can see a list of all the accounts you've authorized by typing the command:
t accounts
The output of which will be structured like this:
sferik
UDfNTpOz5ZDG4a6w7dIWj
uuP7Xbl2mEfGMiDu1uIyFN
gem
thG9EfWoADtIr6NjbL9ON (active)
Note: One of your authorized accounts (specifically, the last one
authorized) will be set as active. To change the active account, use the set
subcommand, passing either just a username, if it's unambiguous, or a username
and consumer key pair, like this:
t set active sferik UDfNTpOz5ZDG4a6w7dIWj
Account information is stored in a YAML-formatted file located at ~/.trc
.
Note: Anyone with access to this file can impersonate you on Twitter, so
it's important to keep it secure, just as you would treat your SSH private key.
For this reason, the file is hidden and has the permission bits set to 0600
.
Typing t help
will list all the available commands. You can type t help TASK
to get help for a specific command.
t help
t update "I'm tweeting from the command line. Isn't that special?"
Note: If your tweet includes special characters (e.g. !
), make sure to
wrap it in single quotes instead of double quotes, so those characters are not
interpreted by your shell. (However, if you use single quotes, your Tweet
obviously can't contain any apostrophes.)
t whois @sferik
t users -l @sferik @gem
t follow @sferik @gem
t does_follow @ev @sferik
Note: If the first user does not follow the second, t
will exit with a
non-zero exit code. This allows you to execute commands conditionally, for
example, send a user a direct message only if he already follows you:
t does_follow @ev && t dm @ev "What's up, bro?"
t list create following-`date "+%Y-%m-%d"`
t followings | xargs t list add following-`date "+%Y-%m-%d"`
t list members -l following-`date "+%Y-%m-%d"`
t lists -l
t friends -l --sort=followers
t leaders -l --sort=followers
t leaders | xargs t unfollow
t followings -l --sort=tweets | head -10 | awk '{print $1}' | xargs t unfollow
t groupies | shuf | head -1 | xargs t follow
t mentions -n 10 -l | awk '{print $1}' | xargs t favorite
t timeline -n 200 --csv > timeline.csv
t stream timeline
t list members twitter team | wc -l
t search all "query"
t search all "lang:en filter:links linux torrent" -n 1 | grep -o "http://t.co/[0-9A-Za-z]*" | xargs open
t search favorites "query"
t search mentions "query"
t search retweets "query"
t search timeline "query"
t search timeline @sferik "query"
- Deep search: Instead of using the Twitter Search API, which only only goes
back 6-9 days,
t search
fetches up to 3,200 tweets via the REST API and then checks each one against a regular expression. - Multi-threaded: Whenever possible, Twitter API requests are made in parallel, resulting in faster performance for bulk operations.
- Designed for Unix: Output is designed to be piped to other Unix utilities, like grep, comm, cut, awk, bc, wc, and xargs for advanced text processing.
- Generate spreadsheets: Convert the output of any command to CSV format simply
by adding the
--csv
flag. - 95% C0 Code Coverage: Well tested, with a 2.5:1 test-to-code ratio.
@jphpsf wrote a blog post explaining how to use t
to backup
your Twitter account.
t
was also mentioned on an episode of the Ruby 5 podcast.
If you discuss t
in a blog post or podcast, let me know and I'll
link it here.
There is some ambiguity in the terminology used to describe relationships on
Twitter. For example, some people use the term "friends" to mean everyone you
follow. In t
, "friends" refers to just the subset of people who follow you
back (i.e., friendship is bidirectional). Here is the full table of terminology
used by t
:
___________________________________________________
| | |
| YOU FOLLOW THEM | YOU DON'T FOLLOW THEM |
_________________________|_________________________|_________________________|_________________________
| | | | |
| THEY FOLLOW YOU | friends | groupies | followers |
|_________________________|_________________________|_________________________|_________________________|
| | |
| THEY DON'T FOLLOW YOU | leaders |
|_________________________|_________________________|
| |
| followings |
|_________________________|
If you're running Zsh or Bash, you can source one of the bundled completion
files to get shell completion for t
commands, subcommands, and
flags.
Don't run Zsh or Bash? Why not contribute completion support for your favorite shell?
The twitter gem previously contained a command-line interface, up until version 0.5.0, when it was removed. This project is offered as a sucessor to that effort, however it is a clean room implementation that contains none of the original code.
This library aims to support and is tested against the following Ruby implementations:
- Ruby 1.8.7
- Ruby 1.9.2
- Ruby 1.9.3
- Ruby 2.0.0
If something doesn't work on one of these Ruby versions, it's a bug.
This library may inadvertently work (or seem to work) on other Ruby implementations, however support will only be provided for the versions listed above.
If you would like this library to support another Ruby version, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.
If you are running t on a remote computer you can use the flag --display-url during authorize process to display the url instead of opening the web browser.
t authorize --display-url
Copyright (c) 2011-2013 Erik Michaels-Ober. See LICENSE for details. Application icon by @nvk.