Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Fixed #99 #100

Merged
merged 2 commits into from
Nov 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Mark Hammond (OAuth support in API)
Prashant Pawar (IRC bot multi-channel support)
David Bittencourt (python 2.3 support)
Bryan Clark (HTTP encoding bugfix, improved exception logging)
Irfan Ahmad <http://twitter.com/erfaan> (Fixed #91 rate limit headers and #99 twitter API timeouts)
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ Examples::
# into the middle of a call. You can also use replacement:
t.user.list.members(user="tamtar", list="things-that-are-rad")

# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reasone
t.users.lookup(screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1)


Searching Twitter::

Expand Down
20 changes: 17 additions & 3 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def __call__(self, **kwargs):
_id = kwargs.pop('_id', None)
if _id:
kwargs['id'] = _id

# If an _timeout is specified in kwargs, use it
_timeout = kwargs.pop('_timeout', None)

secure_str = ''
if self.secure:
Expand All @@ -188,11 +191,14 @@ def __call__(self, **kwargs):
body = arg_data.encode('utf8')

req = urllib_request.Request(uriBase, body, headers)
return self._handle_response(req, uri, arg_data)
return self._handle_response(req, uri, arg_data, _timeout)

def _handle_response(self, req, uri, arg_data):
def _handle_response(self, req, uri, arg_data, _timeout=None):
kwargs = {}
if _timeout:
kwargs['timeout'] = _timeout
try:
handle = urllib_request.urlopen(req)
handle = urllib_request.urlopen(req, **kwargs)
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
return handle
elif handle.info().get('Content-Encoding') == 'gzip':
Expand Down Expand Up @@ -256,6 +262,14 @@ class Twitter(TwitterCall):
# Note how the magic `_` method can be used to insert data
# into the middle of a call. You can also use replacement:
t.user.list.members(user="tamtar", list="things-that-are-rad")

# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reasone
t.users.lookup(
screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), \
_timeout=1)



Searching Twitter::
Expand Down