#!/usr/bin/python # Copyright 2007 Michael Stevens mstevens@etla.org # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or ( # at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 # USA import urllib, urllib2 from sys import stdin, stdout, stderr from optparse import OptionParser parser = OptionParser(version="%prog 1.0") parser.add_option("-u", "--username", dest="username", help="Your twitter account username or email") parser.add_option("-p", "--password", dest="password", help="Your twitter account password") (options, args) = parser.parse_args() email = options.username password = options.password class MessageError(Exception): pass if email == None: parser.error("-u is a required option") if password == None: parser.error("-p is a required option") body = "" line = stdin.readline() while line: if line == ".\n": break else: body += line line = stdin.readline() if len(body) > 140: raise MessageError, "Message is more than 140 characters." def twitterPost(email, password, message): # Create an OpenerDirector with support for Basic HTTP Authentication... auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password('Twitter API', 'twitter.com', email, password) opener = urllib2.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib2.install_opener(opener) data = urllib.urlencode({"status" : body}) res = urllib2.urlopen("http://twitter.com/statuses/update.xml", data) twitterPost(email, password, body)