from django.utils import simplejson import base64, httplib, urllib #From: http://avinashv.net/2008/04/python-and-twitter/ class API(object): def __init__(self, username, password, debug = False): self.authentication = { "Authorization": "Basic %s" % base64.encodestring("%s:%s" % (username, password)).strip() } self.debug = debug if not self.debug: self.connection = httplib.HTTPConnection("twitter.com", 80) else: self.connection = None def _do_get(self, path, data): if self.debug: return simplejson.loads("{ }") self.connection.request("POST", path, urllib.urlencode(data), self.authentication) reply = self.connection.getresponse() return simplejson.loads(reply.read()) def update_status(self, status): reply = self._do_get("/statuses/update.json", {"status": status}) return reply def send_dm(self, user, message): reply = self._do_get("/direct_messages/new.json", {"user": user, "text": message}) return reply def kill_friendship(self, user): path = "/friendships/destroy/%s.json" % (user) data = {"id": user} reply = self._do_get(path, data) return reply def create_friendship(self, user): path = "/friendships/create/%s.json" % (user) data = {} reply = self._do_get(path, data) return reply