Today I’m trying to use Python again and write small twitter-updater for command line interface
You must have a config file $HOME/.twitter.ini for use this script
[auth]
user = twit_user
password = ******
Usage:
./twit.py your message
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib, httplib2, os, ConfigParser
def tweet(msg):
post = urllib.urlencode({ 'status' : msg })
http = httplib2.Http()
http.force_exception_to_status_code = False
user,passwd = getConfig()
http.add_credentials(user,passwd)
resp, content = http.request('http://twitter.com/statuses/update.xml', 'POST', post)
if resp and resp.status == 200:
print "Ok!"
else:
print "Error: ",resp.status
print content
print "============================================="
def getConfig():
filename = os.path.expanduser('~/.twitter.ini')
config = ConfigParser.RawConfigParser()
config.read(filename)
try:
return config.get('auth','user'), config.get('auth','password')
except:
print "Please check configuration file: ",filename
print "Folowing file format required: "
print "+---------------------------+"
print "| [auth] |"
print "| user=twitter_username |"
print "| password=***** |"
print "+---------------------------+"
sys.exit(1)
sys.argv.reverse()
sys.argv.pop()
sys.argv.reverse()
message = " ".join(sys.argv)
tweet(message)



Home