#!/usr/bin/env python
'''
PWT is a simple command line twitter client

Copyright 2008 Eric Stein
License: GPL2/GPL3, at your option.  For details see LICENSE.

$Id: pwt 1 2008-03-17 19:24:03Z eastein $
'''

import os
import os.path
import sys
import urllib2
import twitter

if len(sys.argv) == 1 :
	print 'Usage: pwt [message]'
	sys.exit(2)

api = None

fn = os.path.join(os.path.expanduser('~'), '.pwt')
if os.path.exists(fn) :
	lns = file(fn).read().split('\n')

	if len(lns) < 2 :
		print 'Invalid formatting.  Expected username on first line and password on the next'
		sys.exit(1)

	api = twitter.Api(lns[0], lns[1])
else :
	print 'Write your twitter username and password on two separate lines to ~/.pwt'
	sys.exit(1)

try:
	message = sys.argv[1]
	for i in range(2, len(sys.argv)) :
		message += ' ' + sys.argv[i]

	api.PostUpdate(message)
	print 'updated'

except urllib2.HTTPError :
	print 'authorization failure'
	sys.exit(3)
