+from exceptions import Exception
+
+from twitter.twitter_globals import POST_ACTIONS
+from twitter.auth import NoAuth
+
+def _py26OrGreater():
+ import sys
+ return sys.hexversion > 0x20600f0
+
+if _py26OrGreater():
+ import json
+else:
+ import simplejson as json
+
+class TwitterError(Exception):
+ """
+ Base Exception thrown by the Twitter object when there is a
+ general error interacting with the API.
+ """
+ pass
+
+class TwitterHTTPError(TwitterError):
+ """
+ Exception thrown by the Twitter object when there is an
+ HTTP error interacting with twitter.com.
+ """
+ def __init__(self, e, uri, format, uriparts):
+ self.e = e
+ self.uri = uri
+ self.format = format
+ self.uriparts = uriparts
+
+ def __str__(self):
+ return (
+ "Twitter sent status %i for URL: %s.%s using parameters: "
+ "(%s)\ndetails: %s" %(
+ self.e.code, self.uri, self.format, self.uriparts,
+ self.e.fp.read()))
+
+class TwitterResponse(object):
+ """
+ Response from a twitter request. Behaves like a list or a string
+ (depending on requested format) but it has a few other interesting
+ attributes.
+
+ `headers` gives you access to the response headers as an
+ httplib.HTTPHeaders instance. You can do
+ `response.headers.getheader('h')` to retrieve a header.
+ """
+ def __init__(self, headers):
+ self.headers = headers
+
+ @property
+ def rate_limit_remaining(self):
+ """
+ Remaining requests in the current rate-limit.
+ """
+ return int(self.headers.getheader('X-RateLimit-Remaining'))
+
+ @property
+ def rate_limit_reset(self):
+ """
+ Time in UTC epoch seconds when the rate limit will reset.
+ """
+ return int(self.headers.getheader('X-RateLimit-Reset'))
+
+
+# Multiple inheritance makes my inner Java nerd cry. Why can't I just
+# add arbitrary attributes to list or str objects?! Guido, we need to
+# talk.
+class TwitterJsonListResponse(TwitterResponse, list):
+ __doc__ = """Twitter JSON Response
+ """ + TwitterResponse.__doc__
+ def __init__(self, lst, headers):
+ TwitterResponse.__init__(self, headers)
+ list.__init__(self, lst)
+class TwitterJsonDictResponse(TwitterResponse, dict):
+ __doc__ = """Twitter JSON Response
+ """ + TwitterResponse.__doc__
+ def __init__(self, d, headers):
+ TwitterResponse.__init__(self, headers)
+ dict.__init__(self, d)
+
+class TwitterXmlResponse(TwitterResponse, str):
+ __doc__ = """Twitter XML Response
+ """ + TwitterResponse.__doc__