Return to Snippet

Revision: 7825
at August 14, 2008 16:43 by nate_smith


Updated Code
#!/usr/bin/python
# tweet.py
# usage:
# tweet.py message
#
# inpired by: 

import sys 
from os import popen

def tweet( message, user, password ):
  print 'posting %s for %s' % (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>"
    sys.exit()

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

  user = raw_input('Username: ')
  password = raw_input('Password: ')

  tweet(message, user, password)

Revision: 7824
at August 14, 2008 09:41 by nate_smith


Updated Code
#!/usr/bin/python
# tweet.py
# usage:
#   ./tweet.py username password message
# or
#   ./tweet.py username message
#
# inpired by: http://snipplr.com/view/6594/post-to-twitter-from-the-shell/

import sys
from os import popen

def tweet( message, user, password=None ):
  if len(message) == 0:
    print "No message given"
    return False

  if password is None:
    password = raw_input('Password for %s: ' % user)

  print 'posting %s for %s' % (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) == 4:
    user = sys.argv[1]
    password = sys.argv[2]
    message = sys.argv[3]
  elif len(sys.argv) == 3:
    user = sys.argv[1]
    message = sys.argv[2]
  else:
    print "Incorrect number of args"
    sys.exit()

  if len(message) > 140:
    print "Message too long"
    sys.exit()

  tweet(message, user, password)

Revision: 7823
at August 14, 2008 09:40 by nate_smith


Updated Code
#!/usr/bin/python
# tweet.py
# usage:
#   ./tweet.py username password message
# or
#   ./tweet.py username message
#
# inpired by: 

import sys
from os import popen

def tweet( message, user, password=None ):
  if len(message) == 0:
    print "No message given"
    return False

  if password is None:
    password = raw_input('Password for %s: ' % user)

  print 'posting %s for %s' % (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) == 4:
    user = sys.argv[1]
    password = sys.argv[2]
    message = sys.argv[3]
  elif len(sys.argv) == 3:
    user = sys.argv[1]
    message = sys.argv[2]
  else:
    print "Incorrect number of args"
    sys.exit()

  if len(message) > 140:
    print "Message too long"
    sys.exit()

  tweet(message, user, password)

Revision: 7822
at August 14, 2008 09:36 by nate_smith


Initial Code
#!/usr/bin/python
# tweet.py
# usage:
#   tweet.py username password message
# or
#   tweet.py username message
#
# inpired by: 

import sys
from os import popen

def tweet( message, user, password=None ):
  if len(message) == 0:
    print "No message given"
    return False

  if password is None:
    password = raw_input('Password for %s: ' % user)

  print 'posting %s for %s' % (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) == 4:
    user = sys.argv[1]
    password = sys.argv[2]
    message = sys.argv[3]
  elif len(sys.argv) == 3:
    user = sys.argv[1]
    message = sys.argv[2]
  else:
    print "Incorrect number of args"
    sys.exit()

  if len(message) > 140:
    print "Message too long"
    sys.exit()

  tweet(message, user, password)

Initial URL
http://snipplr.com/view/2352/twitter-from-the-command-line-using-curl/

Initial Description
inspired by http://snipplr.com/view/6594/post-to-twitter-from-the-shell/ . I just rewrote it in Python because I didn't want to install a Ruby interpreter :)

use `chmod +x tweet.py` to run it as `./tweet.py` instead of `python tweet.py`

Initial Title
Post to Twitter from Shell - Python Version

Initial Tags
python, twitter

Initial Language
Python