Tweeting from Command line using Python
By Sandip Dev on Dec 06, 2009
Woke up at 11 in the morning, brushed my teeth and all...Sat down at my desk to study for the end semester exams tomorrow but did not feel like it...So decided to do something fun..
Twitter is quite popular and people are just dying to join up and tell everyone what they are doing, what they had for breakfast, when they are going shopping and other such 'important' details about their daily life, whether people care to listen or not. Nevertheless its a good service.
So yeah, coming back to the point. Long ago I had seen this shell script to tweet from the command line. And I have been into Python over the last few days, so decided to cook up one of my own in Python. Given below is the result of my last 20 minutes of work.
Caution: Won't work in windows because it does not have curl. Coming up with a Windows version soon.
Save the code in a file say 'tweet.py'.To run this do a chmod +x tweet.py and then just type ./tweet.py "Your message" and it will be posted. But before that, you will need to open up the tweet.py file and put in your Twitter username and password in it. Check out the third and second lines from the bottom. That is where your username and password goes, inside the quotes. And you are all set.
from os import popen
print 'Hold on there %s....Your message %s is getting posted....' % (message, user)
url = 'http://twitter.com/statuses/update.xml'
curl = 'curl -s -u %s:%s -d status="%s" %s' % (user,password,message,url)
pipe = popen(curl, 'r')
if __name__ == '__main__':
if len(sys.argv) != 2:
print "Usage: tweet.py <message>"
message = sys.argv
if len(message) > 140:
print "Message too long"
user = "barneystinson" #put your username inside these quotes
password = "awesomeness" #put your password inside these quotes