api = TwitterAPI(auth=oauth, secure=True)
-stream_api = TwitterAPI(host="stream.twitter.com", auth=oauth, stream=True,
+stream_api = TwitterAPI(domain="stream.twitter.com", auth=oauth, stream=True,
secure=True)
-def test_can_get_public_tweet_json():
- updates = get('statuses/public_timeline')
- assert updates
- assert updates[0]['text']
+#def test_can_get_public_tweet_json():
+# updates = get('statuses/public_timeline')
+# assert updates
+# assert updates[0]['text']
def test_can_get_specific_status():
- update = get('statuses/show/:id', id=230400277440770048)
+ update = api.get('statuses/show/:id', id=230400277440770048)
assert update
def test_can_perform_a_search():
- results = TwitterAPI(host="search.twitter.com", api_ver=None).get(
+ results = TwitterAPI(domain="search.twitter.com", api_version=None).get(
'search', q="hello")
assert results
def test_post_a_new_tweet():
- api.post("statuses/update", status=get_random_str() + "ïõ")
+ api.post("statuses/update", status=get_random_str() + u"ïõ")
def test_can_get_raw_response():
twitter11 = Twitter(domain='api.twitter.com',
auth=oauth,
api_version='1.1')
-twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1')
+twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1')
AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
else:
raise TwitterHTTPError(e, uri, self.format, arg_data)
-class Twitter(TwitterCall):
+class OldTwitter(TwitterCall):
"""
Get RESTful data by accessing members of this class. The result
is decoded python objects (lists and dicts).
callable_cls=TwitterCall,
secure=secure, uriparts=uriparts)
-class TwitterData(object):
- def __init__(
- self, format="json",
- domain="api.twitter.com", secure=True, auth=None,
- api_version=_DEFAULT):
-
- if not auth:
- auth = NoAuth()
-
- if (format not in ("json", "xml", "")):
- raise ValueError("Unknown data format '%s'" %(format))
-
- if api_version is _DEFAULT:
- if domain == 'api.twitter.com':
- api_version = '1.1'
- else:
- api_version = None
-
- self.auth = auth
- self.format = format
- self.domain = domain
- self.api_version = api_version
- self.secure = secure
+from api2 import TwitterAPI
class TwitterExtender(object):
- def __init__(self, data, urlparts):
- self.data = data
+ def __init__(self, api, urlparts):
+ self.api = api
self.urlparts = urlparts
def __getattr__(self, k):
try:
return object.__getattr__(self, k)
except AttributeError:
- return TwitterExtender(self.data, urlparts + (k,))
+ return TwitterExtender(self.api, self.urlparts + (k,))
+
+ def __call__(self, **kwargs):
+ meth = (self.api.post
+ if kwargs.pop('_method', 'GET').lower() == 'post'
+ or (self.urlparts and self.urlparts[-1] in POST_ACTIONS)
+ else self.api.get)
+ id = kwargs.pop('_id', None)
+ if id:
+ kwargs['id'] = id
+
+ urlparts = list(self.urlparts)
+ for arg in list(kwargs.keys()):
+ if arg.startswith('_') and arg in urlparts:
+ urlparts[urlparts.index(arg)] = str(kwargs.pop(arg))
+ uri = "/".join(urlparts)
+
+ res = meth(uri, **kwargs)
+ return wrap_response(json.loads(res.content), res.headers)
+
+ def _(self, arg):
+ return getattr(self, str(arg))
+
+
+def Twitter(**kwargs):
+ if kwargs.get('domain') == 'search.twitter.com' and kwargs.get('api_version') is None:
+ kwargs['api_version'] = None
+ api = TwitterAPI(return_raw_response=True, **kwargs)
+ return TwitterExtender(api, ())
+Twitter.__doc__ = OldTwitter.__doc__
__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]
class TwitterAPI(object):
- def __init__(self, host='api.twitter.com', api_ver='1', auth=NoAuth(),
+ def __init__(self, domain='api.twitter.com', api_version='1.1', auth=NoAuth(),
secure=True, stream=False, return_raw_response=False):
"""
`host` host to connect to (api.twitter.com)
`api_ver` API version
"""
- self.host = host
- self.api_ver = api_ver
+ self.domain = domain
+ self.api_ver = api_version
self.auth = auth
self.secure = secure
self.stream = stream
def get(self, path, **kwargs):
- url, remaining_params = make_url(self.secure, self.host, self.api_ver,
+ url, remaining_params = make_url(self.secure, self.domain, self.api_ver,
path, kwargs)
data = self.auth.encode_params(url, 'GET', remaining_params)
headers = self.auth.generate_headers()
def post(self, path, **kwargs):
- url, remaining_params = make_url(self.secure, self.host, self.api_ver,
+ url, remaining_params = make_url(self.secure, self.domain, self.api_ver,
path, kwargs)
data = self.auth.encode_params(url, 'POST', remaining_params)
headers = self.auth.generate_headers()
get = _default_api.get
-_search_api = TwitterAPI(host="search.twitter.com", api_ver=None)
+_search_api = TwitterAPI(domain="search.twitter.com", api_version=None)
def search(q, **kwargs):
return _search_api.get("search", q=q, **kwargs)
else:
result = res.json
return result
+