How to post to twitter from the command line
Here's how to write a tiny program that posts to twitter from the command-line.
1) Make sure you have virtualenv and virtualenv wrapper. This is so you can install modules specific to this project without messing up your python environment. Newcoder.io has a good page on installating virtualenv.
$ cd project/couch-tweeting
mkvirtualenv couch-tweeting
You'll see (couch-tweeting) in front of your command-prompt
3. Install the python-twitter and its dependencies from http://code.google.com/p/python-twitter/
Untar in your couch-tweeting directory.
$ python setup.py install
Install the dependencies.
4. Make sure it's imported, start the python command-line interpreter and import the module.
If you get no errors then it imported properly.
5. Now you need to set up some auth stuff. This is so your "app" (really, any 3rd party program that wants to connect to twitter) can connect to twitter and ask it for information.
Go to the twitter dev site and get this stuff at https://dev.twitter.com
You'll need to log in with your existing twitter account. Then under 'My applications', click 'Create a new application', and enter the info.
You need to make sure your app has read-write access so it can write to your timeline. The default is for apps to be read-only.
Go to 'Settings', then 'Application Type' and click "Read-Write" and then click save/update.
To be able to log in to twitter, you need OAuth credentials, basically saying you're authorized to do things on twitter. You can find those under Details >> OAuth settings. You'll need these as parameters to python-twitter.
6. You can test it out from the CLI:
>> api = twitter.Api(consumer_key='your key',
consumer_secret='your_secret', access_token_key='yourtoken',
access_token_secret='yoursecret')
All this info you can get from the Details >> OAuth settings in the page for your twitter app. Note that if you change something important, like if your app is read-only, or read-write, you have to regenerate the access token.
Then you can try posting something!
>> message = api.PostUpdate("Hello world")
Go check on your twitter timeline, the message should have posted!
7. At this point you're all set to write a program or you can play with twitter from the command-line some more.
8. So now if we want to be able to do
$ 'couch-tweeter "my message woo"
as a program by itself instead of having to fire up the python interpreter, we have to write an actual program. We could just put the above few lines in a file, but it would be annoying to have to edit the file each time we want to write a new message. So we want to pass the message/tweet as a command-line argument. Python has an amazing parseArgs module that handles parsing arguments, errors, help messages, all for us.
Then our main function just looks like this:
# here we get the arguments
opts = parse_args()
# parsargs puts its results in opts (I'll show you how to tell it to put the message in opts.status in one second)
message = api.PostUpdate(opts.status)
print message
if __name__ == '__main__':
main()
10. The parseargs function looks like this:
# this says "this is what this program does"
parser = argparse.ArgumentParser(description="Post something to twitter!")
# this says "watch out for this argument, it's the first one, let's call it 'status'
parser.add_argument('status', metavar='status',
help='What do you want to say?')
# get the args and put them into opts
opts = parser.parse_args()
# it knows that opts should have a status, if not, it's an error!
if not (opts.status):
parser.error("You have to specify a message to post!")
return opts
(I took this and modified it from newcoder.io.) This could probably be explained better, let me know if you have any questions.
Make sure to include the parseargs module at the top of your program
import twitter
import argparse
See the complete tiny program here:
https://github.com/almosteverywhere/couch-tweeting