X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/21e3bd236aefebc9d8ce60b4352a966537a4268f..4a6070c8236f2954d7a1d8f804e3e08adeb963f9:/twitter/api.py diff --git a/twitter/api.py b/twitter/api.py index c59d121..d2df61b 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -6,6 +6,15 @@ import httplib from exceptions import Exception +def _py26OrGreater(): + import sys + return sys.hexversion > 0x20600f0 + +if _py26OrGreater(): + import json +else: + import simplejson as json + class TwitterError(Exception): """ Exception thrown by the Twitter object when there is an @@ -14,48 +23,64 @@ class TwitterError(Exception): pass class TwitterCall(object): - def __init__(self, username, password, format, uri=""): + def __init__(self, username, password, format, uri="", agent="twitter.py"): self.username = username self.password = password self.format = format self.uri = uri + self.agent = agent def __getattr__(self, k): try: return object.__getattr__(self, k) except AttributeError: return TwitterCall( self.username, self.password, self.format, - self.uri + "/" + k) + self.uri + "/" + k, self.agent + ) def __call__(self, **kwargs): method = "GET" if (self.uri.endswith('new') or self.uri.endswith('update') - or self.uri.endswith('create')): + or self.uri.endswith('create') + or self.uri.endswith('destroy')): method = "POST" + + if (self.agent and self.uri.endswith('update')): + kwargs["source"] = self.agent + + encoded_kwargs = urlencode(kwargs.items()) argStr = "" - if kwargs: - argStr = "?" + urlencode(kwargs.items()) + if kwargs and (method == "GET"): + argStr = "?" + encoded_kwargs + + headers = {} + if (self.agent): + headers["X-Twitter-Client"] = self.agent + if (self.username): + headers["Authorization"] = "Basic " + b64encode("%s:%s" %( + self.username, self.password)) + if method == "POST": + headers["Content-type"] = "application/x-www-form-urlencoded" + headers["Content-length"] = len(encoded_kwargs) + c = httplib.HTTPConnection("twitter.com") try: - c.putrequest(method, "/%s.%s%s" %( + c.putrequest(method, "%s.%s%s" %( self.uri, self.format, argStr)) - if (self.username): - c.putheader( - "Authorization", "Basic " + b64encode("%s:%s" %( - self.username, self.password))) - if (method == "POST"): - # TODO specify charset - pass + for item in headers.iteritems(): + c.putheader(*item) c.endheaders() + if method == "POST": + c.send(encoded_kwargs) r = c.getresponse() + if (r.status == 304): return [] elif (r.status != 200): raise TwitterError("Twitter sent status %i: %s" %( r.status, r.read())) - if ("json" == self.format): - import simplejson - return simplejson.loads(r.read()) + if "json" == self.format: + return json.loads(r.read()) else: return r.read() finally: @@ -120,12 +145,6 @@ class Twitter(TwitterCall): """ if (format not in ("json", "xml")): raise TwitterError("Unknown data format '%s'" %(format)) - if (format == "json"): - try: - import simplejson - except ImportError: - raise TwitterError( - "format not available: simplejson is not installed") TwitterCall.__init__(self, email, password, format) -__all__ = ["Twitter"] +__all__ = ["Twitter", "TwitterError"]