Sunday Dec 06, 2009

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)

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