Skip to content

Instantly share code, notes, and snippets.

@saivenkat
Created November 5, 2009 17:23
Show Gist options
  • Save saivenkat/227215 to your computer and use it in GitHub Desktop.
Save saivenkat/227215 to your computer and use it in GitHub Desktop.
Update twitter status
from httplib2 import Http
from urllib import urlencode
class TwitterClient:
def __init__(self, username, password):
self.username = username
self.password = password
def update_status(self, message):
client = Http()
twitter_update_url = "http://twitter.com/statuses/update.xml"
client.add_credentials(username, password)
encoded_data = urlencode(dict(status=message))
response, content = client.request(twitter_update_url, "POST", encoded_data)
_fail_if_response_is_not_success(response)
def _fail_if_response_is_not_success(self, response):
success_status_code = 200
if response['status'] != success_status_code:
raise Exception, "Unable to post message to twitter"
if __name__ == '__main__':
client = TwitterClient('user','pass')
client.update_status("I am happy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment