Tweeting from Command line using Python

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)

Comments:

Thanks. Actually , yours worked on my Windows with cygwin python and curl.

Posted by Katsumi INOUE on December 09, 2009 at 12:53 PM IST #

Oh yes it would if you have Cygwin and Curl on it. But it wont work on a normal Python installation on Windows. :)

Posted by Sandip Dev on December 09, 2009 at 01:22 PM IST #

This was completely COOL Sandip. It's something I always wanted to do...so I took some time (shhh....) and extended it this morning to receive SNMP Traps from our Sun Storage 7000 Appliance and forward them to Twitter :-)

http://blogs.sun.com/pmonday/entry/tweeting_your_sun_storage_7000

Thank you!

Posted by Paul Monday on December 17, 2009 at 04:07 PM IST #

This was completely COOL Sandip. It's something I always wanted to do...so I took some time (shhh....) and extended it this morning to receive SNMP Traps from our Sun Storage 7000 Appliance and forward them to Twitter. It is here:

http://blogs.sun.com/pmonday/entry/tweeting_your_sun_storage_7000

Thank you!

Posted by Paul Monday on December 17, 2009 at 04:11 PM IST #

This was completely COOL Sandip. It's something I always wanted to do...so I took some time (shhh....) and extended it this morning to receive SNMP Traps from our Sun Storage 7000 Appliance and forward them to Twitter. It is at the URL address linked by my name.

Thanks for your post!

Posted by Paul Monday on December 17, 2009 at 04:12 PM IST #

If you want to remove your dependency on curl, and make this script portable and less dependent on the environment, you can use urllib to make the tweet.

See: http://commandline.org.uk/python/scripting-twitter-with-python/#c606

Neat idea, thanks for sharing.

MK

Posted by Michael on January 09, 2010 at 09:56 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Hi. I am Sandip Dev. I am a student of Computer Engineering at SVNIT (NIT-Surat). My passions are coding, listening to music, reading and learning new things. I am also the Chairperson of the ACM Chapter of my college and Sun Campus Ambassador here.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today