Python script for Twitter

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.

#!/usr/bin/python
import sys 
from os import popen

def tweet(user,password,message):
    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')
    print 'Done...awesome'

if __name__ == '__main__':

    if len(sys.argv) != 2:
    print "Usage: tweet.py <message>"
    sys.exit()

    message = sys.argv[1]
    if len(message) > 140:
    print "Message too long"
    sys.exit()


    user = "barneystinson" #put your username inside these quotes

    password = "awesomeness" #put your password inside these quotes
    tweet(user,password,message) 
Advertisements

10 thoughts on “Python script for Twitter

    • Yeah I tried that. It indented everything and then I just got pissed off…And had no time either..Yes without the proper indenting this script wont run ๐Ÿ™‚

  1. ver2.0 hahah…. dude rather than justing a script to post a status mesage…why dont you write a script to crawl twitter…try it ..its no big deal

    • Thats a good suggestion. I did think about that once. Obviously crawling will have limitation because if I am not wrong twitter only allows downloading of 3000 tweeets per day thru the API. But a small tweet search script could certainly be written

      • dude if you can come with a valid reason…they will give you a developer license.. which allows for much more queries….and yeah even if that is not possible add delays to your queries….

  2. getting an error when I try and run. This file is in a bin directory. Does it need to be any place special on my mac?

    File “tweet.py”, line 1

    ^
    SyntaxError: invalid syntax

    • I have no idea about Macs but I think your python interpreter is either not installed or located in a location other that /usr/bin/python . Pls verify that

Let me know what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s